做一个连续移动的身体

时间:2013-07-18 06:58:32

标签: android box2d andengine

我正在使用 Box-2d &的 AndEngine 即可。我有动态的身体让我们说一个飞机。我想以给定的速度连续移动它。所以,我设置了一个线性速度。我也有屏幕边界(地面,屋顶,左边和右边墙)。那些是静止的身体。当我的飞机与边界碰撞一两次时,它就会停止。我没有重力应用于飞机。为什么飞机虽然有固定的速度但是停了? 的问题: 如何在碰撞后以连续速度和相应方向移动飞机?之后,我想将其移动到用户触摸屏幕的位置?

代码:创建平面

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 个答案:

答案 0 :(得分:1)

您可以尝试将墙壁的弹性设置为1.0f,并定期接触以消除摩擦,

final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0,
    0.5f, 0.0f);

要,

final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0,
    1.0f, 0.0f);

跳这有帮助。