a question about moving shapes

Official forum for the Chipmunk2D Physics Library.
Post Reply
jj2010
Posts: 5
Joined: Thu Jun 03, 2010 12:57 am
Contact:

a question about moving shapes

Post by jj2010 »

Hi there, I have a quick question, I am using the chipmunk library to provide the physics for my 2d game, however, I have reached the point where my shapes need animating, (physics as well !). It seems that officially, the only way to move a collision shape (polygon) is to remove and add it again, although this seems rather wasteful to me, especially as all i need to do is move them around a bit, (in relation to the other physics shapes within the same body.) I don't want to do hundreds and hundreds of malloc and free calls each frame, when all i want to do is transform some verts.

What would be the quickest and easiest way to do a one-time transformation of polygon shape vertices, without screwing everything else up too badly? and can you give a code example? I don't care if the physics isnt perfect during these transformations, the overlap solver would probably be adequate as the shapes need to slowly animate over a large number of frames, rather than radically jumping around. I don't care about rotations either. only want to move the shape in x and y axes.

i want to have lots of this kind of shapes, so the most efficient way is preffered to the most realistic...

Thanks!!
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: a question about moving shapes

Post by Tam Toucan »

What do you mean by "needs animating". For instance, can't your objects be made up of multiple shapes and bodies? e.g. Like this simple rag doll. Typically the animation is a sprite and the shapes don't change (or you move/rotate the shapes as per the rag doll). If you want to move the position yourself then just remember to update the velocity as well.

If you are wanting to actually change the shape e.g. you want increase the size of a circle, move the points on a polygon, then obviously things get trickery (it's a physics simulation, how do you simulate a circle suddenly doubling in size?). There are the functions in chipmunk_unsafe.h, you can use these. Caveat Emptor!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: a question about moving shapes

Post by slembcke »

Well, officially there is no way to move or animate shapes without moving the body that they are attached to. As Tam pointed out, there is some functionality in place to modify shapes, but it's all placed in the chipmunk_unsafe.h header which for good reason is not included when you include chipmunk.h. The official way to animate shapes (if you can call it that) is to build a ragdoll and control it with joints or carefully update the velocity and positions of all the bodies. This is very hard to do well, and you'll find that AAA quality games rarely do this. It's simply too complicated and hard to control. You either end up with mushy looking animations or interations or mushy feeling player controls. (or hilarious animations and controls: http://www.youtube.com/watch?v=xUAFCMkDMJo)

Most games stick with using simple collision shapes and simple controls. It makes it possible to provide responsive player controls and creates a more controlled environment for creating good animations. Not that this is a rule, but it's a very good suggestion.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jj2009
Posts: 6
Joined: Wed Jan 13, 2010 10:20 am
Contact:

Re: a question about moving shapes

Post by jj2009 »

well, my 'animation' needs are very simple. for example, over a number of frames, i would want to transform 2 verts to allow a rectangular shape to stretch, as in, for example, a 2-dimensional 'door' opening. The position and orientation of the 'door' are locked in place, as it is artificially glued to the terrain anyway.I looked at the unsafe API and the only function i could see was cpPolyShapeSetVerts (or someting like that) which when i delve deeper, destroys and recreates the memory for the verts every time. This is a bit wasteful for what i need, i was wondering if there was a simpler way to transform the same verts of an existing shape (poly shape that is)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: a question about moving shapes

Post by slembcke »

Again, animating the vertexes is probably not going to give you the effect you want. I'd like to point out that allocating memory is not very expensive. Chipmunk used to allocate at least one block of memory per pair of colliding objects per step. The reason why this was slower was because the memory was spread around instead in a big long chunk. The cost of calling the memory functions was trivial, accounting to less than 1% of the runtime.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jj2010
Posts: 5
Joined: Thu Jun 03, 2010 12:57 am
Contact:

Re: a question about moving shapes

Post by jj2010 »

ok, one last question about this subject then.. in function cpPolyShapeSetVerts in the chipmunk_unsafe header, whats the 'offset' vector for?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: a question about moving shapes

Post by slembcke »

Same thing as when creating the shape initially. It's an offset that is added to each vertex. It's mostly useful when you want to attach the same polygon to a static body in multiple places. Then you don't have to recalculate all the vertexes by hand.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jj2010
Posts: 5
Joined: Thu Jun 03, 2010 12:57 am
Contact:

Re: a question about moving shapes

Post by jj2010 »

oh, well thats good, i can just use this value to do the transformations thanks!!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: a question about moving shapes

Post by slembcke »

If you are just offsetting the whole shape then why not move the body that it's attached to?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jj2010
Posts: 5
Joined: Thu Jun 03, 2010 12:57 am
Contact:

Re: a question about moving shapes

Post by jj2010 »

well, because it would require quite a re-write of the code to make my game objects composed of multiple bodies, which i don't want to do for various reasons. The thing is that i have multiple shapes attached to each body, but not all the shapes will necessarily move at once, and some of the shapes may move in different directions. anyhow, I have used the cpPolyshapesetverts function now, and it does more or less what i wanted for now. (apart from not applying any force to the colliding objects, only seperating them.) i reckon this issue can be handled with a fudge though, since i already have a post solver function, and i know the relative motion of the shape too. when colliding with the shape, when that shape has moved between this frame and the last (animation movement, not body movement), just add a small impulse in the direction of motion. simple!

>> in fact, that's what i have done and it works fine, so im going to leave it like this now!
Post Reply

Who is online

Users browsing this forum: No registered users and 35 guests