Page 1 of 1

Resizing cpShape

Posted: Fri Jan 30, 2009 7:34 am
by Ben
Hi all,

I've been happily muddling along with Chipmunk (and cocos2d as well) and have hit a bit of a wall.

In my application, I have a rectangle made of four cpShape segments. I'd like to be able to resize this rectangle (so that it gives the appearance of growing), but can't for the life of me figure out how to do that because I keep getting lost in a maze of shape struct definitions. Does anyone have any suggestions on how best do achieve this?

Re: Resizing cpShape

Posted: Fri Jan 30, 2009 9:35 am
by ker
So far modifying any chipmunk objects in an undocumented way has been discouraged in this forum. I don't think that's gonna change.

A few month ago someone had a similar request (i think with circles). The suggestion was to remove the shape and create a new one every frame (if I remember correctly).

If your rectangle is supposed to be static (not movable by other objects) and if you don't want to resize your rectangle for more than twice it's original size, there's a way to "cheat". You could create two segments for each side of the rectangle (same length, same everything, just not colliding with each other) and move them in opposite directions. That way it will look like that side is growing. Every side-segment would need it's own body then (or you could combine left-top right-top2 right2-bottom bottom2-left2).

If you really really want to mess with the internal data structure I could still give you some tips, but I don't recommend it at all.

Re: Resizing cpShape

Posted: Fri Jan 30, 2009 1:23 pm
by Ben
Thanks for your reply there ker, that's pretty much what i was afraid of. Your reply has actually made me think this through a bit further though.

It's not essential for the rectangle to be static, as the space it's in has no gravity, and it will be set to ignore all collisions. So, probably the next easiest thing to do would be to make the rectangle be a cpPolyShape, and modify the vertexes on each step. I know that's reaching inside the object still, but it's a lot simpler than what I was thinking before. Does this perhaps seem a better way than recreating the object on each step?

Re: Resizing cpShape

Posted: Fri Jan 30, 2009 1:49 pm
by ker
if you don't want collisions, why create a chipmunk-shape at all?

Yea, I'd use a cpPolyShape.. fewer stuff to update. Even thou I think it's harder to manually update than cpSegmentShape is.

You could attempt to reinitialize your shape using cpPolyShapeInit. I'm not sure thou if that will cause memory leaks. You might also destroy some important data set by cpSpaceAddShape. You'd also need to reset your normal cpShape data like elasticity and friction.

If you modify directly, remember that there are values that you'll have to calculate and set (some precalculated data which cpPolyShapeInit generates for your input data).

You could look at the source of cpPolyShapeInit and create something that only initializes the data you want to be initialized. If that works very well others here might be interested in that, too.

Be careful with any of these methods, they might go mad if you update chipmunk to a newer version.

Re: Resizing cpShape

Posted: Fri Jan 30, 2009 2:30 pm
by Ben
ker wrote:if you don't want collisions, why create a chipmunk-shape at all?

Yea, I'd use a cpPolyShape.. fewer stuff to update. Even thou I think it's harder to manually update than cpSegmentShape is.

...

Be careful with any of these methods, they might go mad if you update chipmunk to a newer version.
Thanks, that's pretty useful. To clarify, I want to know if a collision occurs, but not to take any action (objects pass through each other).

Thanks to your help, I have at least three options:

1. Recreate a cpSegmentShape each step
2. Modify a cpPolyShape
3. Use my own hit detection each step

I'll report back with whichever works best.

Re: Resizing cpShape

Posted: Fri Jan 30, 2009 4:06 pm
by slembcke
Yeah, Chipmunk doesn't really let you modify shapes on the fly in a way that would be physically realistic as if the surface was actually moving. This is actually a pretty hard thing to do in general. I'm mostly opposed to changing shapes on the fly in Chipmunk because it can't resolve new overlaps using real physics. Overlaps are solved by moving the objects apart, but not giving them a real velocity. Abusing this can lead to some odd looking behavior.

On the other hand, it often times does look just fine because the use wasn't to have "realistic" physics in the first place. Maybe they only wanted collision detection like you. I've considered adding an "unsafe" API (in a separate header and with a big disclaimer) that would allow people to do things like change shapes on the fly.

I would recommend going the route where you recreate the shape each time it changes. That is at least following the API and won't break if I change the data structures. It won't make the physics be accurate though (which you don't seem to care about anyway).

Re: Resizing cpShape

Posted: Mon Feb 02, 2009 8:24 am
by Ben
Thanks again for the suggestions. The method I've chosen is to recreate a cpPolyShape on each step, it's fast enough and the array of vertices is easy to store (outside the shape) and modify. I'm not sure why you say the physics will not be accurate though, as they seem fine so far.

Re: Resizing cpShape

Posted: Mon Feb 02, 2009 2:25 pm
by slembcke
The physics do not act accurately because changing an object carries no information about the velocity of the surface of the object. Other things will get pushed away because Chipmunk solves overlaps by directly modifying the positions of objects, but does not use a real persistent velocity to do it.

Re: Resizing cpShape

Posted: Wed Mar 31, 2010 1:00 pm
by pabloruiz55
If i use this method of removing / re-adding the shape every step, or at some point, is it possible for the app to crash at a given momment? For example if the shape was removed just when a collision was about to be triggered?

Thanks

Re: Resizing cpShape

Posted: Wed Mar 31, 2010 4:45 pm
by slembcke
Shapes are added and removed outside of the simulation step, so it shouldn't matter.