Box2dweb - 碰撞接触点 [英] Box2dweb - Collision Contact Point

查看:494
本文介绍了Box2dweb - 碰撞接触点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用box2dweb。我试图开发一个游戏。在某些时候,我需要找出一个圆和盒子之间的接触点。所有我知道的是它可以使用b2ContactListener。我们可以通过使用后解决事件实现b2ContactListener来接收联系人数据。请帮助!

解决方案

您可以使用b2ContactListener来处理各种事件:

  var b2Listener = Box2D.Dynamics.b2ContactListener; 

//为联系人添加侦听器
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:脉冲;
console.log(world.ball.impulse);
}
}

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

.world.SetContactListener(listener);

只需删除postSolve代码,这取决于你需要挂钩到相应的事件。 p>

Seth ladd在他的博客上有一些关于碰撞/反应的伟大文章。



我希望这会有所帮助。



谢谢,
Gary


I use box2dweb. I am trying to develop a game. At some point I need to find out the contact point between a "Circle" and "Box". All I know is it can be done using b2ContactListener. We can receive contact data by implementing b2ContactListener using Post-Solve Event. Please help!

解决方案

You are on the right track there are various events you can hook into with the 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);

Just remove the postSolve code and depending on what you need to do hook into the appropriate events.

Seth ladd has some great articles on his blog about collision/reacting to them. This is where I picked up these bits so full credit goes to him.

I hope this helps.

Thanks, Gary

这篇关于Box2dweb - 碰撞接触点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆