Page 1 of 1

Windings

Posted: Wed Nov 12, 2008 10:13 pm
by singpolyma
I'm having a bit of a problem wrapping my head around windings. I'm using the Ruby bindings. I have a rectangle with x, y, width, height, where 0,0 is top-left. I am using this:

mass = 5.0
vertices = [CP::Vec2.new(0,0), CP::Vec2.new(0, height, CP::Vec2.new(width, height), CP::Vec2.new(width, 0)]
body = CP::Body.new(mass, CP::moment_for_poly(mass, vertices, CP::Vec2.new(0, 0)))
shape = CP::Shape::Poly.new(body, vertices, CP::Vec2.new(0,0))
body.p = CP::Vec2.new(x,y)


is this right? I ask, because I have a floor as well, and sometimes when the object is bouncing off the floor it rotates (which is fine) but then falls through (not so fine). If I set a line width on the floor to 100 or so I can see that the object is not falling all the way through, just down a bit. It does not fall more than this initial bit. Odd.

Re: Windings

Posted: Thu Nov 13, 2008 12:42 am
by slembcke
Well, the first weird bit that you might be running into is that you are defining a box with the center of gravity in one of the corners. Probably not what you meant. The origin of the poly (0,0) is attached to the body at the given offset from the center of gravity. The winding is otherwise correct I think.

I think I might need to see a screenshot (or video if you have the capability) to get a better idea of the problem. Chipmunk does allow a small amount of constant overlap on objects that are in contact. You can change the amount of allowed overlap using CP.collision_slop=(). I believe it defaults to 0.1.

FYI: There is a global vec2() method that you can use instead of CP::Vec2.new().

Re: Windings

Posted: Fri Nov 14, 2008 9:40 am
by singpolyma
slembcke wrote:Well, the first weird bit that you might be running into is that you are defining a box with the center of gravity in one of the corners. Probably not what you meant. The origin of the poly (0,0) is attached to the body at the given offset from the center of gravity.
So, should I change the vertex coordinates, the "offset from centre of gravity" parameter to CP::Shape::Poly.new, or both?
slembcke wrote:I think I might need to see a screenshot (or video if you have the capability) to get a better idea of the problem. Chipmunk does allow a small amount of constant overlap on objects that are in contact. You can change the amount of allowed overlap using CP.collision_slop=(). I believe it defaults to 0.1.
I'll see what I can do :)
slembcke wrote:FYI: There is a global vec2() method that you can use instead of CP::Vec2.new().
Interesting. Is there an RDoc somewhere?

Re: Windings

Posted: Fri Nov 14, 2008 11:41 pm
by singpolyma
I tried changing to this:

[CP::Vec2.new(width/-2, height/-2), CP::Vec2.new(width/-2, height/2), CP::Vec2.new(width/2, height/2), CP::Vec2.new(width/2, height/-2)]

but now the character just falls halfway into the ground to begin with

Re: Windings

Posted: Sat Nov 15, 2008 5:53 pm
by slembcke
After shifting the collision polygon, are you sure that you are drawing in the correct place?

Re: Windings

Posted: Sun Nov 16, 2008 12:20 am
by singpolyma
No idea. I'm drawing at the position of my rectangle (which is set to shape.body.p every frame) and the width and height of the rectangle.

Re: Windings

Posted: Fri Nov 21, 2008 8:27 pm
by singpolyma
So, I've got it working. Suggestion for docs:

Mention that centre of gravity is 0,0 and that it's a good idea to create polygons with the origin in the centre.
The origin is now in the centre, that means your top-left corner for drawing is shape.body.p - height/2