我已经在 Node.js 服务器中实现了 Box2dWeb,除了冲突之外一切正常。当有 2 个玩家连接时,他们的身体不会发生碰撞 - 只是互相穿过。这是玩家身体创建代码:
//World creation
var world = new b2World(new b2Vec2(0, 0));
//Body+Fixture creation
var bodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.x = 100;
bodyDef.position.y = 100;
bodyDef.linearDamping = 5;
bodyDef.angularDamping = 1;
var fixDef = new b2FixtureDef();
fixDef.density = 0.001;
fixDef.friction = 1;
fixDef.restitution = 1;
fixDef.shape = new b2CircleShape;
fixDef.shape.SetRadius(8);
var body = world.CreateBody(bodyDef);
body.CreateFixture(fixDef);
很感谢任何形式的帮助!
编辑:我添加了这段代码:
var listener = new Box2D.Dynamics.b2ContactListener;
listener.BeginContact = function(contact) {
console.log(contact.GetFixtureA().GetBody().GetUserData());
}
world.SetContactListener(listener);
一旦身体发生碰撞,我会在控制台中得到“null”。