Sign in to follow this  
dustrat

Collision respons problem / Physics

Recommended Posts

dustrat    122
Hello there! I am about to implement a simple physics simulation and I need to know how to handle collisions properly. I have a very simple object, a solid sphere, which is colliding with the polygons of a static world. The collision detection part work fine and I get a correct sliding plane normal and intersection point. Each frame I need to determine what acceleration to apply to the sphere. If its in the air, I apply gravitational acceleration, and if its on the ground I apply acceleration according to the formula "a = 5/7 * g sin theta", where theta is the angle of inclination and g is the gravitational constant. Also, I calculate the direction of the acceleration on the ground with a simple plane projection: "A = G -(G dot N)* N", where G is direction of gravity and N is the collision normal. My problem is, each time the sphere collide, I reflect the velocity vector about the collision normal and apply damping to it. Next frame I look if there was a collision the previous frame, and if it was, I calculate the ground acceleration. This doesn't really work the way I want it to. As long as the sphere have I high enough velocity it bounces around very realistic, but when speed slows down and the sphere lands on a triangle with an inclination angle large enough to make it roll, it just bounce down with very small bounces and very low speed, almost no acceleration at all. What's wrong with my thinking here? I know it's not anything wrong with my code, so I don't include it here. It does whats described above, and obviously, it doesn't really work. I don't really want to implement a rigid body engine because of very restricted memory resources and processing power. (I'm doing all this crap in Java...) If anyone have a suggestion it would make me very happy =) /Dustrat

Share this post


Link to post
Share on other sites
minionx    133
so lets say your sphere is falling, right, and a few frames later, it collides with a sloping polygon. You compute the bounce/dampening, etc, (which likely causes the sphere to move out from the polygon a bit due to the bounce?) and go to the next frame. On this new frame, it says, ok, last frame there was a collision, so I must be on a surface, therefore I will apply the ground acceleration, not the gravitational acceleration. However, if this ground acceleration is not enough to bring the sphere back to the surface of the plane, and cause another collision, then NEXT frame, the ball will think its falling again, apply the gravitational acceleration, (which would be greater than the ground acceleration) and cause another collision with the plane. The whole thing would then repeat.

Does this sound like that might be whats happenning? (its just a guess after all, maybe you've handled this situation in your code)

Share this post


Link to post
Share on other sites
minionx    133
Maybe if instead of checking to see if there was a collision last frame, you could check to see if there was a collision OR if you applied ground acceleration

like:

if(bCollisionLastFrame || bUsedGroundAccelLastFrame) {
//use ground accel again
}else {
//use grav accel
}

Share this post


Link to post
Share on other sites
HellCreator    133
I am writing 3d engine in java using gl4java
since my world contents 95% static objects I found that it is verry expensive to check colision each time and aply the Gravity
so since we know how to interpolate we just can do some like this an this saves a tone of proccessor time like triple buffering tecnique

int min_count_of_frames_to_check_colision=5;
//check each 5 frames you can change this at run time :)
//if you render at 40 fps then collision will be checked each 125 msec :)
int frames_counted_since_last_checked=0;//initialy 0

void Render(GLFunc gl)
{
if(frames_counted_since_last_checked<min_count_of_frames_to_check_colision)
{
frames_counted_since_last_checked++;
}else
{
//***
1)get your interpolation values
2)do colision detections
3)...
//***
frames_counted_since_last_checked=0;
}
//***
1)here just render
//***
}

Share this post


Link to post
Share on other sites
dustrat    122
Quote:
Original post by minionx
so lets say your sphere is falling, right, and a few frames later, it collides with a sloping polygon. You compute the bounce/dampening, etc, (which likely causes the sphere to move out from the polygon a bit due to the bounce?) and go to the next frame. On this new frame, it says, ok, last frame there was a collision, so I must be on a surface, therefore I will apply the ground acceleration, not the gravitational acceleration. However, if this ground acceleration is not enough to bring the sphere back to the surface of the plane, and cause another collision, then NEXT frame, the ball will think its falling again, apply the gravitational acceleration, (which would be greater than the ground acceleration) and cause another collision with the plane. The whole thing would then repeat.

Does this sound like that might be whats happenning? (its just a guess after all, maybe you've handled this situation in your code)


Well, the problem is when I should go from "bounce mode" to "roll mode". Because each time a bounce occurs, the velocity is damped. And if the sphere bounce and roll every other frame it will never accelerate because of the damping from the bounce. So if a collision occurs frame 1 and I store the collision normal and reflect the velocity vector, I perform a ground acceleration frame 2. But in frame 3, gravitation makes the sphere touch the ground again and reduce the velocity. When I again try to apply the ground acceleration in frame 4, the sphere have the same velocity as it had in frame 2. This is the problem. I think to somehow switch to "roll mode" and only apply ground acceleration until a collision occurs in the direction of the velocity should do the trick.

But I think the problem with this solution is that when the sphere is rolling down a slope and suddenly rolls of the ground and is in the the "air", still no collision has occured, but gravity should be applied anyway. You know what I mean?

Any suggestions to this one?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
mm, I see what you mean...

I think you're onto something though with the state machine concept. "roll mode" "bounce mode" or whatever...

Ok, how about this:
I might do away with the special ground acceleration, and just always apply gravitational accel downwards. I think if you do the sliding/bounce/friction on the planes right, it should still look nice.

As you say, in frame 1, a collision occurs, and the velocity is damped. You should be able to check the magnitude of the reflected velocity vector. If this vector is very large, then you should probably do a few more bounces, before the ball starts rolling.

When the reflected velocity is negligible, you switch to "roll mode". In roll mode, when a collision occurs, you handle it as if there were zero bounce. i.e. you project the velocity onto the surface of the plane, producing an angle of incidence of X, and an angle of reflection of 90 degrees. no bounce. Of course, you can damp this resultant velocity a bit if you like as if there were friction.

Now, you also want to detect when this state ends. Because you are always applying accel downwards, you get a little "course correction" every frame from the poly you are sliding down. i.e. you calculate your new velocity vector, and add it to the position of the ball to find out where the ball would "like" to go. After performing collisions, you will find that the ball has collided with the poly you're sliding down, and has been "bumped" a little higher. As long as you keep getting these LITTLE bumps, you are still in slide mode.

However, if the bump is a large one (maybe you were rolling down a slope, and suddenly hit a wall at the end of the slope) you should probably go to bounce mode. by "large bump" I mean you're desired destination was serisouly embedded into the polygon.

The other option is that you suddenly stop getting these "course correction" bumps, and that must mean that there is not longer anything to roll on, and you should go to bounce mode (i.e. start falling.)

Well, that was quite a ramble, but maybe theres something in there that could help.

Share this post


Link to post
Share on other sites
dustrat    122
I'm getting closer to a solution, and it seems to me that the best method really is to implement rigid body physics. I'm currently testing impulse based methods that so far works pretty good and display a really nice result. Rolling and bouncing works almost magically now. And because I use a sphere only, the maths involved arent to heavy either.

The general way to do it is to apply an impulse whenever a collision is bound to occur in the next timestep. So instead of using the collision detection to adjust the spheres position I apply an impulse of certain magnitude and direction to drastically change the direction of the velocity. It's really cool. But I'm sorry to say that I can't show anything since my work is confidential (there's only sprites and wireframes anyway).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this