Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing the angle of reflection using Box2D

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

image https://i.sstatic.net/lMwLN.png

Thanks for the help,! ken

like image 479
Ken Strickland Avatar asked Sep 13 '25 06:09

Ken Strickland


1 Answers

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

like image 112
Ringo_D Avatar answered Sep 15 '25 01:09

Ringo_D