Two rigid bodies are stuck together

Official forum for the Chipmunk2D Physics Library.
Post Reply
Dalin
Posts: 2
Joined: Sat Feb 12, 2011 7:59 pm
Contact:

Two rigid bodies are stuck together

Post by Dalin »

I have two rigid bodies with a single segment shape attached to each. The first is a small 4x4 square with a friction of 0.7 attached to a rigid body with an infinite MoI and mass of 10. The second is a 4x18 rectangle shape attached to a rigid body with infinite MoI and mass of 81. The rectangle shape represents the player and the square represents a generic crate. To jump, I check to see that the velocity on the Y-axis is 0 and if it is I apply an impulse of 6000 to the player’s rigid body at the center of the rigid body. This will shoot the rectangle up just above the crate. At which point I can maneuver left and right by applying a force of 25000 in the direction on the X axis I wish to go at the center of the player’s rigid body.

If I land on the ground, velocity pretty much becomes 0 and I can instantly jump again. If I land on top of the crate however, velocity on the Y-axis does become 0 but applying an impulse does very little to the velocity. It will shoot up to about 0.3 then slowly back down to 0. Applying the impulse over and over again (after the velocity has reached 0 once more) does the same thing but the player’s rigid body doesn't move. I can however, move horizontally along the crate using forces which slowly moves the player off the crate but also pulls the crate with the player until they are no longer touching.

Currently the elasticity for all the shapes in the Space is set to 0.0f. Elasticity values between 0 and 0.9 allow the velocity to reach 0 however values 1.0 and above cause the rigid body to bounce off and when it comes to "rest" on the crate the velocity never reaches 0 but fluctuates between two numbers. The gravity in the Space is set to -200 and damping is set to 0.9, iterations set at 10 and the step is 1.0f/6.0f. The ground segments are attached to the static body of the Space.

The desired behavior is that the player should be able to jump onto crate and jump off of it (i.e. crates being stacked will allow the player to jump onto each crate getting higher and higher). The crate moving along with the player on the x-axis is I suspect due to the low mass of the crate and friction of the ground which is set to 0.5.

I've tried messing around with the elasticity, friction, mass and magnitude of the impulse but as soon as the player rigid body comes to rest on the crate they are inseparable in the vertical axis. When the player's rigid body comes to rest on top of the crate, there is pretty much just a pixel of overlap with the top of the crate, but I would have figured that an impulse of that magnitude (I've gone up to 18000) would seperate the two bodies.

What am I missing?

Edit: I tried applying a force instead of an impulse on the Y-axis and the rigid bodies no longer get stuck on eachother. So I guess the problem lies in my understanding of what an impulse is. Isn't an Impulse a force applied over time? So an impulse of 1000 might be a force of 500 applied over 2 seconds or a force of 200 applied over 5 seconds? At any rate, an impulse seems to be greater than a force so why can't it break the "bonds" between the two rigid bodies?

Edit #2: I spoke too soon. I can seperate the two rigid bodies with a force or an impulse but only if I remove the restriction of being able to apply said force/impulse when the velocity in the Y-axis is zero. So, a continous force/impulse is always able to seperate the two bodies but a single force/impulse (doesn't seem to matter at what magnitude) cannot seperate them.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Two rigid bodies are stuck together

Post by slembcke »

Well, generally impulses are very large forces applied over very short amounts of time. Impulses in Chipmunk are instantaneous, so it's really just momentum that you are adding directly to the body's velocity without integrating it over time.

You said "I can seperate the two rigid bodies with a force or an impulse but only if I remove the restriction of being able to apply said force/impulse when the velocity in the Y-axis is zero." Are you absolutely sure that the impulse is being applied? I've done similar things to what you are describing without problems. The velocity will probably never reach exactly 0 due to problems with floating point error. Are you doing a straight if(body->v.y == 0), because that could be very problematic. You should always use a threshold like if(fabs(body->v.y) < 0.1) or something similar.

It sounds like maybe detecting when the player is standing on the ground that is the problem? Currently the best way to detect if an object is grounded in Chipmunk is to store a boolean on your gameobject somewhere. Set it to false before calling cpSpaceStep() and set it to true if you detect a collision with a normal that points upwards in the pre-solve collision handler. I've got a better system on the way in Chipmunk 6, but that is a while off yet.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Dalin
Posts: 2
Joined: Sat Feb 12, 2011 7:59 pm
Contact:

Re: Two rigid bodies are stuck together

Post by Dalin »

I was afraid you might mention collision handlers. I took a look at the player demo and can't seem to follow how arbiters work. It also appears that you can only have one callback per state (preSolve, etc.) per Space which might make things tricky to handle a large amount of game objects (possibly unknown types) that will respond to collisions differently. And none of objects that store the physics space aren't static so static callbacks might make things impossible, but I'm rambling here.

The checking the velocity in a range seems to be working now though. Before it would trigger while in air at the peak of a jump, which is why I was checking just a velocity of 0. There is still a noticeable difference between landing on a crate and being able to jump off of it though unfortunately, but that might just require tweaking. I guess collision handlers are my best bet for something that looks half decent. Thanks.

What kind of new system were you thinking of for Chipmunk 6?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Two rigid bodies are stuck together

Post by slembcke »

Chipmunk 6 keeps a graph of all the colliding and jointed objects in order to improve the sleeping algorithm a little bit. This will make it pretty easy to write a function that checks for grounding by just looping over the shapes touching a particular shape and checking the collision normal of them.

The problem with checking the velocity of the player as you've found out is that it lets you jump again at the top of a jump (maybe that is an unintended gameplay bonus?).
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