3
votes

Folks,

I'm trying to implement a certain behavior to a collision where a ball hits a wall at an angle. I want the ball to maintain full velocity, but I would like for the angle of reflection to be somewhat muted, so that it bounces back less from the direction it came.

I've played around with friction, damping, and restitution, but nothing seems to make a difference in my return bounce angle.

Does anyone know of a way I can get box2d to do what I'm wanting it to do?

Ball angle of reflection

imagehttp://i.stack.imgur.com/lMwLN.png

Thanks for the help,! ken

1
I don't know anything about box2d, but from a physics standpoint, there are two things going on to affect the bounce: (1) the loss of kinetic energy from hitting the wall will slow the ball down in the left-most direction, and (2) the downward friction on the ball will slow the ball down in the downward direction. I'm not sure, but in some cases those effects will mask each other, giving you roughly the same angle of reflection as if there weren't any friction or loss of KE.Chris Gregg
Yes, I realize that what I'm asking Box2D to do is change it's physics behavior. In my case, I'm using a frictionless ball hitting a frictionless wall so that my velocity will not decrease.Ken Strickland
Probably the best thing to do is add an impulse of your own after the collision has been processed. In PostSolve you can get the collision normal and tangent vectors, an impulse made from some combination of these should do the trick but you're on your own with calculating the right amounts. Keep in mind PostSolve can be called over many time steps for one collision but you might find that you should only add this once, eg. in EndContact. I'm curious about what you're making and how 'somewhat muted' is defined...iforce2d
Okay, I figured out a way to handle this. iforce2d got me thinking on the right path, but I handled it a little differently. Instead of detecting a collision, I look compare the LinearVelocity before and after a Step. If the LinearVelocity has changed by more than 5 degrees, then I setLinearVelocity to half the changed angle. It's probably not perfect, but it's fine for what I'm doing.Ken Strickland
Since you asked what I'm doing, I'm writing a game where a bug walks around a track. I walk the bug by sending a "leaderCircle" in front of him which just bounces around the track at a constant speed, and I just walk the sprite toward the circle. This gives a fairly realistic walking patter for a bug.Ken Strickland

1 Answers

2
votes

Firstly,you can set a contactListener in your world, and then ,find out the exactly collision between the ball and the wall. Secondly,find out the collision point. Last, calculate the angle between collision point and body center.

such as

void contactListener::BeginContact(b2Contact *contact)
{
    //find out the collision between the ball and the wall.
    ....

    //find out the collision point
    b2WorldManifold worldManifold;
    contact->GetWorldManifold(&worldManifold);
    b2Vec2 collisionPoint = worldManifold.points[0];

    //calculate the angle between collision point and body center.
    b2Vec2 bodyCenter = body->GetWorldCenter;
    ...
}

I hope you can understand what I mean