[solved] Strange bodies behaviour

Discuss any Chipmunk bugs here.
Post Reply
shybovycha
Posts: 6
Joined: Sat Dec 18, 2010 2:57 pm
Contact:

[solved] Strange bodies behaviour

Post by shybovycha »

Greetings. I've made a simple demo application which allows user to create his own rigid bodies. But when i create a new body and it falls down to the "ground", some forces are applied to this body (and i add just gravity one though) and one start flying around almost randomly. Video and source code are avaliable at ge.tt.
Last edited by shybovycha on Thu Apr 28, 2011 7:01 am, edited 1 time in total.
[url=http://stackoverflow.com/users/330471/shybovycha][img]http://stackoverflow.com/users/flair/330471.png[/img][/url]
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Strange bodies behaviour

Post by slembcke »

It looks like the center of gravity of the bodies are not actually at the shape center so the CoG is swinging somewhere down below the shapes. (0,0) in the shape coordinates is where it attaches to the center of gravity of the rigid body. Chipmunk 5.3.3 has some new utility functions for helping calculate polygon centroids and recenter polygons. Look at the end of this section: http://files.slembcke.net/chipmunk/rele ... s/#cpShape
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
shybovycha
Posts: 6
Joined: Sat Dec 18, 2010 2:57 pm
Contact:

Re: Strange bodies behaviour

Post by shybovycha »

So, now i can calculate my bodies' centroids. And gravity will still be applied to the (0,0) point, isn't it? =) So, the question is: what's the point of your answer / centroid calculation? =)
[url=http://stackoverflow.com/users/330471/shybovycha][img]http://stackoverflow.com/users/flair/330471.png[/img][/url]
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Strange bodies behaviour

Post by slembcke »

No, it's changing the shape so it attaches to the body at the shape's center.

I think your problem is that you are leaving the body at (0,0), then creating the shape in world coordinates. Instead, you should be creating the body where you want the object, and make the shape relative to that.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
shybovycha
Posts: 6
Joined: Sat Dec 18, 2010 2:57 pm
Contact:

Re: Strange bodies behaviour

Post by shybovycha »

Here's my code for your method (if i got your idea right):

Code: Select all

cpVect center = cpCentroidForPoly(i + 1, verts);
cpBody* mybody = cpSpaceAddBody(space, cpBodyNew(100.0f, cpMomentForPoly(1.0f, i + 1, verts, cpvzero)));
mybody->p = center;
cpShape *myshape = cpSpaceAddShape(space, cpPolyShapeNew(mybody, i + 1, verts, cpvzero)); 
Anyway, this is not a working-right one =)
[url=http://stackoverflow.com/users/330471/shybovycha][img]http://stackoverflow.com/users/flair/330471.png[/img][/url]
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Strange bodies behaviour

Post by slembcke »

You are setting the CoG of the body (in absolute coords) to the centroid of the poly which is expressed in local coordinates (relative to the body). Store the centroid in a variable, then call cpRecenterPoly() to make the poly's verts relative to that OR pass the negative centroid as the offset to the moment and shape init function.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
shybovycha
Posts: 6
Joined: Sat Dec 18, 2010 2:57 pm
Contact:

Re: Strange bodies behaviour

Post by shybovycha »

Thanks! This one worked just perfect for me:

Code: Select all

cpVect center = cpCentroidForPoly(i + 1, verts);
cpRecenterPoly(i + 1, verts);
cpBody* mybody = cpSpaceAddBody(space, cpBodyNew(100.0f, cpMomentForPoly(1.0f, i + 1, verts, cpvzero)));
cpShape *myshape = cpSpaceAddShape(space, cpPolyShapeNew(mybody, i + 1, verts, cpvzero)); 
mybody->p = center;
[url=http://stackoverflow.com/users/330471/shybovycha][img]http://stackoverflow.com/users/flair/330471.png[/img][/url]
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests