[solved] Strange bodies behaviour
-
- Posts: 6
- Joined: Sat Dec 18, 2010 2:57 pm
- Contact:
[solved] Strange bodies behaviour
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]
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Strange bodies behaviour
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 6
- Joined: Sat Dec 18, 2010 2:57 pm
- Contact:
Re: Strange bodies behaviour
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]
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Strange bodies behaviour
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.
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 6
- Joined: Sat Dec 18, 2010 2:57 pm
- Contact:
Re: Strange bodies behaviour
Here's my code for your method (if i got your idea right):
Anyway, this is not a working-right one =)
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));
[url=http://stackoverflow.com/users/330471/shybovycha][img]http://stackoverflow.com/users/flair/330471.png[/img][/url]
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Strange bodies behaviour
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 6
- Joined: Sat Dec 18, 2010 2:57 pm
- Contact:
Re: Strange bodies behaviour
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]
Who is online
Users browsing this forum: No registered users and 2 guests