我正在使用Box-2d&和引擎。我有一个动态的身体,比如说一个平面。我想以给定的速度不断地移动它。所以,我设置了一个线性速度。我有屏幕边界(地面,屋顶,左墙和右墙)。那些是静止的身体。当我的飞机与边界碰撞一两次时,它就停止了。我并没有对平面施加重力。为什么飞机虽然有固定的速度却停了下来?问题:碰撞后,如何以连续的速度和相应的方向移动平面?之后我想把它移到用户在屏幕上触摸的地方?
代码:创建平面
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
提前感谢。
你能试着把墙壁的弹性设置为1.0f吗?为了经常接触,可以去除摩擦,
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0,
0.5f, 0.0f);
收件人,
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0,
1.0f, 0.0f);
跳这个有帮助。