pythonsimulationphysicspymunk

A few questions about pymunk's accuracy


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.

  1. I have a set torque on the main rod. I would assume that the system left alone should slowly speed up over time. I am not seeing that.
  2. The secondary rod does not seem to be feeling a centripetal acceleration. I would think that as it is swung around , it would want to point outwards. I think this has to do with how the simulated mass is distributed. Would I need to turn the secondary rod into a series of masses, or is there an easier way to move the center of mass?
  3. I can change the mass ratio of the main rod vs the secondary rod. I would assume when M1>>M2 that the system would just spin in circles as if there were no secondary rod. But I am not seeing that, instead the main rod oscillates as if it were transferring a substantial amount of angular momentum to the secondary rod. Am I missing something?
  4. Is pymunk even supposed to be a reliable physics simulator? or is it just approximating something to physics, just good enough to be part of a game?
  5. Is there a python based physics simulator that you would recommend for a system like this one. Eventually I would like to be able to model the oscillations through variable torque and with drag, all while recording the forces on the system.

Solution

    1. 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.

    2. 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).

    3. 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 Shapes attached (see docs).

    4. 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.

    5. 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.