Windings

Official forum for the Chipmunk2D Physics Library.
Post Reply
singpolyma
Posts: 16
Joined: Sun Nov 09, 2008 3:33 pm
Contact:

Windings

Post 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.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Windings

Post 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().
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
singpolyma
Posts: 16
Joined: Sun Nov 09, 2008 3:33 pm
Contact:

Re: Windings

Post 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?
singpolyma
Posts: 16
Joined: Sun Nov 09, 2008 3:33 pm
Contact:

Re: Windings

Post 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
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Windings

Post by slembcke »

After shifting the collision polygon, are you sure that you are drawing in the correct place?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
singpolyma
Posts: 16
Joined: Sun Nov 09, 2008 3:33 pm
Contact:

Re: Windings

Post 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.
singpolyma
Posts: 16
Joined: Sun Nov 09, 2008 3:33 pm
Contact:

Re: Windings

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests