我创建了一个 Kinematic Body类型的平面精灵,它将被连续移动。所以,我设定了一个线性速度到身体,这就是它连续移动的原因。但我有屏幕边界屋顶,地面,左墙,右墙。所有这些都是静态的。当平面移动时,它不会与任何边界墙碰撞。在研究 Box-2d 手册之后,我发现运动体永远不会与其他运动体& 静态身体。因此,无论是将墙壁设置为运动还是静态,它仍然不会与飞机发生碰撞。当我将墙设置为动态时,它会因重力而下降。那么,我该如何设置我的飞机和墙壁之间的碰撞?
以下是代码:
private void initializePlaneAndBoundary() {
/*
* create wall boundary
*/
final Rectangle ground = new Rectangle(0, camera_Height - 2,
camera_Width, 2, vbom);
final Rectangle roof = new Rectangle(0, 0, camera_Width, 2, vbom);
final Rectangle left = new Rectangle(0, 0, 2, camera_Height, vbom);
final Rectangle right = new Rectangle(camera_Width - 2, 0, 2,
camera_Height, vbom);
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0,
0.5f, 0.5f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground,
BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof,
BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left,
BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right,
BodyType.StaticBody, wallFixtureDef);
attachChild(ground);
attachChild(roof);
attachChild(left);
attachChild(right);
aPilot = new Pilot(222, 333, pilotTexures, vbom) {
@Override
protected void onManagedUpdate(float pSecondsElapsed) {
super.onManagedUpdate(pSecondsElapsed);
}
};
pilotBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, aPilot,
BodyType.KinematicBody, FIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(
aPilot, pilotBody, true, true));
pilotBody.setLinearVelocity(DEMO_VELOCITY_X, DEMO_VELOCITY_Y);
attachChild(aPilot);
}
答案 0 :(得分:5)
我看到两种方式: