Box2dweb - 碰撞接触点

She*_*har 8 javascript html5 canvas box2d

我用box2dweb.我正在努力开发一款游戏.在某些时候,我需要找出"圆圈"和"盒子"之间的接触点.我所知道的是它可以使用b2ContactListener完成.我们可以通过使用Post-Solve Event实现b2ContactListener来接收联系人数据.请帮忙!

Gar*_*ary 16

您正走在正确的轨道上,您可以使用b2ContactListener连接各种事件:

var b2Listener = Box2D.Dynamics.b2ContactListener;

//Add listeners for contact
var listener = new b2Listener;

listener.BeginContact = function(contact) {
    //console.log(contact.GetFixtureA().GetBody().GetUserData());
}

listener.EndContact = function(contact) {
    // console.log(contact.GetFixtureA().GetBody().GetUserData());
}

listener.PostSolve = function(contact, impulse) {
    if (contact.GetFixtureA().GetBody().GetUserData() == 'ball' || contact.GetFixtureB().GetBody().GetUserData() == 'ball') {
        var impulse = impulse.normalImpulses[0];
        if (impulse < 0.2) return; //threshold ignore small impacts
        world.ball.impulse = impulse > 0.6 ? 0.5 : impulse;
        console.log(world.ball.impulse);
    }
}

listener.PreSolve = function(contact, oldManifold) {
    // PreSolve
}

this.world.SetContactListener(listener);
Run Code Online (Sandbox Code Playgroud)

只需删除postSolve代码,并根据您需要执行的操作挂钩到相应的事件中.

Seth ladd在他的博客上有一些关于碰撞/对他们做出反应的好文章.这是我拿起这些位的地方,所以他可以获得全部功劳.

我希望这有帮助.

谢谢,加里