开发者

Reducing the angle of reflection using Box2D

开发者 https://www.devze.com 2023-03-24 14:18 出处:网络
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 s

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

Reducing the angle of reflection using Box2D

开发者_如何转开发 http://i.stack.imgur.com/lMwLN.png

Thanks for the help,! ken


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号