I don't think Chipmunk will handle this automatically. You'll have to write code to do it yourself. I suspect you will need two things:
1) The most fundamental thing will obviously be to detect if the joint is in a 'broken' position, and if so, break it (ie remove the joint)
For example , for a pivot joint, you might detect the angle between the two bodies it joins (b1 and b2). One wrinkle is that a body's angle will keep going up and up as you rotate the body round and round. We want to limit 'angle' to being between something like -pi and +pi (angles are measured in radians). Then, if the resulting angle is in a 'broken' position, remove the joint. Something like (my code is in Python, but I'm sure you can convert it to C):
Code: Select all
# calculate the angle
angle = b1.a - b2.a
while angle > pi:
angle -= 2 * pi
# test if the joint is in a broken position
if angle < -2 or angle > +2:
# remove the joint here. I'm not sure how to do this. Anyone?
2) The above will work fine, but your joints might prove very fragile, since it will be easy to rotate the bodies into a position so that the joints break. It will happen all the time as the bodies get pushed around. To make the joint less easy to break, you will need to add a force or torque that the joint applies to its bodies to straighten the joint out, rotating the bodies to pushing the joint away from its breaking position. Something like:
Code: Select all
# calculate the angle same as above
angle = b1.a - b2.a
while angle > pi:
angle -= 2 * pi
# apply a straightening force or torque
torque = -angle * 1000
# torques or forces are always applied in equal and opposite pairs
b1.t = +torque
b2.t = -torque
This is roughly right. I may have reversed the +torque and -torque in the last two lines. Also, you'll have to tune the '1000' to be the right size for the masses on the joint. Also, especially if the 1000 is too high, this will produce oscillation, where the force straightening out the joint will rotate the bodys one way, then overcompensate, and push them back the other way. To get rid of oscillations like this is a big topic in engineering (see
http://en.wikipedia.org/wiki/Control_theory), but I have had success by modifying the value of the torque, reducing it as the angular velocity of the bodies increases.
# apply a straightening force or torque
torque = -angle * 1000
torque -= (b1.w - b2.w) * 500
# torques or forces are always applied in equal and opposite pairs
b1.t = torque
b2.t = -torque[/code]
Again, you'll have to tune the '500' to an appropriate value, and I may have reversed the sign of the value subtracted from torque.
Good luck! Let us know how you get on.