3

As the title says, How can I join/constraint 2 pymunk bodies/shapes so that they don't they act as one single object??
For example, in this case, I have a cricket bat, made up of 2 separate bodies and polys.
I want to join the bat's "handle" to my bat's "blade", So that I get a bat-like object.

My code:

### BAT n Co. ###
# body format: [vertices, offset, position, mass]
bat_bodies_v = [
# bat
    [[[0, 34], [4, 34], [4, 0], [0, 0]],(-2,-20),(103,190),20], # handle
    [[[6, 90] , [0, 32] , [0, 17], [6, 0] , [10, 0], [10, 90]],(-5,-20),(100,100),1100] # blade
]

bat_bodies = []
for vertices, offset, pos, mass in bat_bodies_v:
    moment = pm.moment_for_poly(mass,vertices,offset)
    b = pm.Body(mass,moment)
    b.position = pos

    poly = pm.Poly(b, vertices,offset)
    poly.friction = 0.9

    bat_bodies.append(poly)
    space.add(b,poly)

# the closest I got.
j1 = pm.constraint.PinJoint(bat_bodies[0].body,bat_bodies[1].body)
j2 = pm.constraint.RotaryLimitJoint(bat_bodies[0].body,bat_bodies[1].body,0,0)
space.add(j1,j2)

This ============= becomes ================> This StartEnd
I have a function that drew those green circles at the body positions

pradyunsg
  • 18,287
  • 11
  • 43
  • 96

1 Answers1

3

The best way to build a complex shape in pymunk is simply to attach the shapes to the same body. So unless you have a good reason why you want them separate I suggest you try and add both shapes to the same body.

However, sometimes you might want to do something else, for example be able to break the objects. I havent really implmented anything myself, but Scott (of Chipmunk) writes in this post http://chipmunk-physics.net/forum/viewtopic.php?f=1&t=2420&p=10644&hilit=breakable#p10644 that using a PivotJoint and a GearJoint might be a good idea to keep the two bodies together.

viblo
  • 4,159
  • 4
  • 20
  • 28
  • Thx again. But if I do need to make 2 shapes with separate bodies, then what should I do? Like for example, a breakable plank, that will break if there is more than x force on the joint, giving me 2 separate shapes. How can this be implemented? – pradyunsg Mar 19 '13 at 05:52
  • I was thinking that too. I wanted destruct-able objects. You can handle collision for each shape individually and if you deam that shape broken you determine its center. remove the old one and add the same shape onto the new body. – Kaliber64 Jul 31 '13 at 06:35
  • Do you have to recalculate the moment after adding two shapes to a body or does Pymunk do it automatically? – skrx May 03 '17 at 09:35
  • 2
    @skrx It recalculates it. Note however that they need to be added to a space, otherwise it wont do any calculation. You can try it yourself, create a space, a body with 0 mass/moment and two shapes on the body. Add body and shapes to the space and then change the mass of one of the shapes and then verify that body.mass / body.moment changed. – viblo May 04 '17 at 10:33
  • Thanks. Could you maybe add a short code example to the answer? I'm not sure if I'm doing it correctly. I think the last time I tried to create a body with 0 mass and moment I got an error message. – skrx May 04 '17 at 10:38
  • I just created it with `self.body = pm.Body(0, 0)`. And got the following traceback: File "C:\Users\Name\AppData\Local\Programs\Python\Python35\lib\site-packages\pymunk\body.py", line 85, in __init__ assert mass != 0 and moment != 0, "dynamic body with 0 mass and/or moment" AssertionError: dynamic body with 0 mass and/or moment – skrx May 04 '17 at 10:50
  • 1
    Do you use the latest version of pymunk? If not then that is the problem, calculating mass/moment from shapes were added very recently. – viblo May 04 '17 at 13:28
  • 1
    (Maybe this should be broken out to a separate question. Then I can make a full answer, this comment box is a bit limited) – viblo May 04 '17 at 13:29