cpBody/Shape break on collision

Official forum for the Chipmunk2D Physics Library.
Post Reply
kosa_nostra9
Posts: 17
Joined: Sun Feb 26, 2012 9:54 pm
Contact:

cpBody/Shape break on collision

Post by kosa_nostra9 »

Hello all,

I'm working with chipmunk and cocos2d. My question is how to make a body/shape broke on impact. My guess was to identify the point of a collision on the shape, and then, divide the shape into two shapes accordingly in the pre-solve collision callback.
But I'm really not sure, is there any better way to do this ?
And how can I get the collision point coordinates in the shape space coordinate ? Using the cpContact array in the cpArbiter gives me coordinates of collision within the entire space.

Hope I have been clear enough.
Regards,
Kosa
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpBody/Shape break on collision

Post by slembcke »

I've been meaning to make a demo of this because it sounds like a lot of fun. Haven't gotten around to it, or a ton of other things though. :(

Anyway. You can convert the contact points to body coordinates (shapes are always expressed in body coordinates) using the cpBodyWorld2Local() function. The Slice demo shows how to split up a polygon and recalculate the mass/position properties of the new shapes. Also keep in mind that you cannot add/remove shapes in a collision callback, and need to schedule a post-step callback to do it.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
kosa_nostra9
Posts: 17
Joined: Sun Feb 26, 2012 9:54 pm
Contact:

Re: cpBody/Shape break on collision

Post by kosa_nostra9 »

Thank you for your answer, I'll give it a try later and will le you know how it goes
kosa_nostra9
Posts: 17
Joined: Sun Feb 26, 2012 9:54 pm
Contact:

Re: cpBody/Shape break on collision

Post by kosa_nostra9 »

Hey !

So I've implemented something which works pretty well. I have a question though, when the collision occurs, I consider the arb->contacts->p point. But abr->contacts is an array of points, so why an array ? Also, what is numContacts in the cpArbiter structure ?

Thank you !
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpBody/Shape break on collision

Post by slembcke »

It's an array because the collision between two objects often has more than one contact point. When you have a box sitting on the ground, each corner of the box has a contact point.

Also, don't access the members of cpArbiter directly unless you understand that is the private API. (Actually, you should only be able to access that if you specifically enabled private access.) I highly recommend using the public API for that: http://chipmunk-physics.net/release/Chi ... Properties
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
kosa_nostra9
Posts: 17
Joined: Sun Feb 26, 2012 9:54 pm
Contact:

Re: cpBody/Shape break on collision

Post by kosa_nostra9 »

Hey thank you, I start to understand a bit better.
Now I have a question on the depth parameter. In my game, some collision are very violent and I end up with depth penetration of -100 sometimes. My question is how to "remove" this from the collision point in order to get the point on the edge of the body being hit. Do I have to use the normal here ?

Thank you again for your help !
kosa_nostra9
Posts: 17
Joined: Sun Feb 26, 2012 9:54 pm
Contact:

Re: cpBody/Shape break on collision

Post by kosa_nostra9 »

Well, I think I have figured this out even though I don't really understand:

cpBodyWorld2Local(b1->body, cpvadd(p, cpvmult(n, d/2)))

With p, the collision point, n the normal, and d the depth. Not sure why dividing the depth by 2 give me exactly what I was looking for...
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpBody/Shape break on collision

Post by slembcke »

Depth is the amount of overlap, but not necessarily the distance of the collision point from any particular surface. If you want the closest point on the shape, you have to calculate that yourself, sorry. It's fairly trivial to do for circles and segments, for polygons, just take the closest point to any segment on the perimiter.
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 30 guests