I am interested in simulating a sort of rod-pendulum system:
from intro import pymunk, space, App
import pygame
from pymunk.vec2d import Vec2d
pygame.init()
b0 = space.static_body #declare a universe
b1 = pymunk.Body(mass=1000, moment=10)
b1.position = (240, 200)
c1 = pymunk.Segment(b1, (0, -40), (0, 40), 6)
c1.elasticity = 0.1
space.add(b1, c1)
b2 = pymunk.Body(mass=.1, moment=1)
b2.position = (240, 100)
c2 = pymunk.Segment(b2, (0, 0), (0, -40), 6)
c2.elasticity = 0.1
space.add(b2, c2)
j1 = pymunk.constraints.PinJoint(b0, b1, (240, 200), (0,0))
j2 = pymunk.constraints.PinJoint(b2, b1, (0, 0),(0,-40))
space.add(j1, j2)
b1.torque = 4000
space.gravity = 0,0 #gx , gy
print(b1.position, b1.velocity)
App().run()
print(b1.position, b1.velocity)
But it is not operating as I would expect.
The torque is reset every step, as written here. Given this I think the expectation is that it should slow down over time. I should also add that usually its a bit tricky to set these kinds of values manually, better would be to use a SimpleMotor
constraint.
Yes, the center of gravity seems to be the issue here just as you write. The center of gravity will be at 0,0 of the Segment
(and similar at 0,0 in case you create a Poly shape). So c1
has the center of gravity in the middle, while c2
has it at one end.
You can either just change the definition of c2
to be (b2, (0,-20), (0,20)...)
and modify the position of the body to compensate, or use the body center_of_gravity
property to adjust its location (see docs).
I think the issue is that the moment you use for the two segment bodies are very extreme. The moment can be calculated with the built in method moment_for_segment()
, e.g.: pymunk.moment_for_segment(1000, (0, -40), (0, 40), 6)
. The calculated moment is 717000, which is quite different from the value in your code. You can also let Pymunk calculate the mass and moment for a body from the Shape
s attached (see docs).
Pymunk uses the C library Chipmunk for the actual simulations. The focus of this library was mainly gaming or other realtime uses, and the same for Pymunk. However, I know that Pymunk has been used for many different purposes, as shown in the list of uses page. If the algorithms inside Pymunk are accurate and comprehensive enough for your use case I do not know.
Not sure.
A final note is that its probably a good idea to use as small dt
in the space.step
function as possible since that will produce better simulations.