2

I create a Kinematic Body type plane sprite which will be moved continuously. So, I set a linear velocity to the body and that's why it moves continuously. But I have screen boundary roof, ground, left wall ,right wall .All of those are static body. When the plane moves it don't collide with the any boundary walls. After, studying on Box-2d manuals, I found a Kinematic Body never collides with other Kinematic Body & Static Body. So,either I set walls to Kinematic or Static Body still it doesn't collide with plane. When I set walls to Dynamic it falls down due to gravity. So, What should I do to set collisions between my plane and walls?

Here's the code:

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);

    }
4

1 回答 1

5

我看到两种方法:

  1. 使用动态体而不是运动学。要移动身体,您可以将gravityScale 设置为零并通过设置速度来移动,或者使用b2MotorJoint、b2MouseJoint 或其他。
  2. 如果你真的需要运动体,并且你想捕捉与地面的接触(例如通过设置接触监听器),那么你应该通过 b2WeldJoint 将另一个动态体附加到这个运动体上。然后,你可以聆听这个动态身体的接触,并随心所欲地做出反应。
于 2013-07-17T12:40:22.090 回答