我正在使用Box-2d和AndEngine。我有动态的身体,可以说是Plane。我想以给定的速度连续移动它。所以,我给它设置了一个线性速度。我也有屏幕边界(地面、屋顶、左右墙)。那些是静态的身体。当我的飞机与边界相撞一两次时,它就会停止。我没有对飞机施加重力。为什么飞机虽然速度固定却停止了? 问题:碰撞后如何以连续的速度和相应的方向移动飞机?之后我想将它移动到用户在屏幕上触摸的位置?
代码:创建平面
FixtureDef FIXTURE_DEF = PhysicsFactory
.createFixtureDef(1, .5f, 0.5f);
aPlane = new Plane(222, 333, pilotTexures, vbom) {
@Override
protected void onManagedUpdate(float pSecondsElapsed) {
super.onManagedUpdate(pSecondsElapsed);
}
};
planeBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, aPlane,
BodyType.DynamicBody, FIXTURE_DEF);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(
aPlane, planeBody, true, true));
planeBody.setLinearVelocity(DEMO_VELOCITY_X, DEMO_VELOCITY_Y);
attachChild(aPlane);
planeBody.setGravityScale(0);
planeBody.setUserData(PLANE);
代码:创建边界墙
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);
Body roofBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof,
BodyType.StaticBody, wallFixtureDef);
roofBody.setUserData(ROOF);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left,
BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right,
BodyType.StaticBody, wallFixtureDef);
attachChild(ground);
attachChild(roof);
attachChild(left);
attachChild(right);
我还创建了ContactListener,可以在它们碰撞时收听。要在手指触摸点上移动,我还实现了 IOnSceneTouchListener
预先感谢。