[go] Polygon-weirdness

Official forum for the Chipmunk2D Physics Library.
Post Reply
PiMaker
Posts: 3
Joined: Wed Feb 24, 2016 11:57 am
Contact:

[go] Polygon-weirdness

Post by PiMaker »

Hello everyone,

I'm using chipmunk2d for a pretty basic simulation written in golang. I use the go implementation as found here: https://github.com/vova616/chipmunk.

So far I have created segments as a floor and boxes/circles falling down on it, which works perfectly. Then I created a polygon...

The polygon itself is as simple as it gets, just three vertices ({0,0},{0,60},{200,30}) which form a simple triangle. But when it falls on the ground it partly clips through and randomly starts to turn and flip around.

Here is my code:

Code: Select all

verts := []vect.Vect {
        vect.Vect{vect.Float(0), 0},
        vect.Vect{vect.Float(0), 60},
        vect.Vect{vect.Float(200), 30}}
shape = chipmunk.NewPolygon(verts, vect.Vect{-100,-30})
and

Code: Select all

polybody := chipmunk.NewBody(vect.Float(10), vect.Float(100))
polybody.SetPosition(vect.Vect{vect.Float(500), vect.Float(10)})
polybody.AddShape(polyshape)
space.AddBody(polybody)
where polyshape is the shape from the code above.

I read somewhere that there should be a method to recalculate some value (centroid I think?) but that doesn't seem to exist in the go port... Any other (possibly simple, can even be hacky ;) ) solution?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: [go] Polygon-weirdness

Post by slembcke »

Magic numbers for the body's moment of inertia is almost always a bad idea. Off the top of my head, I would expect it should be at least 10k for a polygon that size/mass. Either set it to INFINITY (safe, but your object won't rotate) or use one of the helper functions to calculate it.

If the Go port is based on Chipmunk 7, there are some automatic facilities to help calculate moments/masses.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
PiMaker
Posts: 3
Joined: Wed Feb 24, 2016 11:57 am
Contact:

Re: [go] Polygon-weirdness

Post by PiMaker »

Thank you, increasing the moment improved the simulation a lot. I did not find those facilities you were talking about, also I'm confused on how changing the moment of inertia drastically improved collision detection, but whatever, glad it works now!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: [go] Polygon-weirdness

Post by slembcke »

Mass is how difficult it is to make an object move. The moment of inertia is how difficult it is to make it rotate. If the moment is very low in relation to the mass, then the high mass will create a large impulse and the low moment will make it too easy to rotate. In your case, the low moment is probably several orders of magnitude to low. Instead of a slight correction to the rotation, it creates a *huge* one. This causes the rotation twitch back and forth erratically as the contacts change, almost completely changing from frame to frame.

The moment functions are not too complicated, and can be found here:
https://github.com/slembcke/Chipmunk2D/ ... munk.c#L92

If there isn't a wrapper/port for them, it shouldn't be hard to do it yourself.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests