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
cpBody/Shape break on collision
-
- Posts: 17
- Joined: Sun Feb 26, 2012 9:54 pm
- Contact:
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: cpBody/Shape break on collision
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.
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 17
- Joined: Sun Feb 26, 2012 9:54 pm
- Contact:
Re: cpBody/Shape break on collision
Thank you for your answer, I'll give it a try later and will le you know how it goes
-
- Posts: 17
- Joined: Sun Feb 26, 2012 9:54 pm
- Contact:
Re: cpBody/Shape break on collision
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 !
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 !
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: cpBody/Shape break on collision
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
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 17
- Joined: Sun Feb 26, 2012 9:54 pm
- Contact:
Re: cpBody/Shape break on collision
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 !
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 !
-
- Posts: 17
- Joined: Sun Feb 26, 2012 9:54 pm
- Contact:
Re: cpBody/Shape break on collision
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...
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...
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: cpBody/Shape break on collision
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Who is online
Users browsing this forum: No registered users and 30 guests