Breakable joints

Official forum for the Chipmunk2D Physics Library.
Post Reply
tom
Posts: 1
Joined: Thu Sep 04, 2008 10:45 am
Contact:

Breakable joints

Post by tom »

Hello,

Is there a way to do breakable joints using chipmunk?

I need to connect multiple girders which should break apart if a certain force is applied.

tom
User avatar
tartley
Posts: 37
Joined: Thu Jun 12, 2008 5:01 pm
Location: London, England
Contact:

Re: Breakable joints

Post by tartley »

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.
[color=#808080]Tartley - Jonathan Hartley, [url]http://tartley.com[/url]
Using Pymunk (Chipmunk's Python bindings) for a game project:
[url]http://code.google.com/p/sole-scion[/url][/color]
User avatar
tartley
Posts: 37
Joined: Thu Jun 12, 2008 5:01 pm
Location: London, England
Contact:

Re: Breakable joints

Post by tartley »

Hey. I wrote an awful lot about joints that break when they rotate into a particular position, but just realised last night that you are possibly thinking about joints that break when too much force is exerted on them instead. I guess that would be different, huh? :-)
[color=#808080]Tartley - Jonathan Hartley, [url]http://tartley.com[/url]
Using Pymunk (Chipmunk's Python bindings) for a game project:
[url]http://code.google.com/p/sole-scion[/url][/color]
supertommy
Posts: 56
Joined: Tue Sep 11, 2007 2:30 pm
Contact:

Re: Breakable joints

Post by supertommy »

I would think it would be possible to modify or inherit from the cpPivotJoint to make a joint that breaks when the forces required to keep it intact exceed a defined limit.

Scott:
You mention a function to examine "the impulse magnitude applied to the joint" in this post:

http://www.slembcke.net/forums/viewtopi ... kable#p421

Is this function easy to write now that you've introduced the joint classes? Is it as easy as looking at the jAcc or jBias vectors?
User avatar
slembcke
Site Admin
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Breakable joints

Post by slembcke »

supertommy wrote:Scott:
You mention a function to examine "the impulse magnitude applied to the joint" in this post:

http://www.slembcke.net/forums/viewtopi ... kable#p421

Is this function easy to write now that you've introduced the joint classes? Is it as easy as looking at the jAcc or jBias vectors?
I had completely forgot about that.

Yeah, now that I've added the joint classes I could pretty easily add the functions to return the impulse magnitude. I'll take a look at that tonight maybe.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests