Shihab Uddin
Shihab Uddin

Reputation: 6931

set collisions between different body type

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

    }

Upvotes: 2

Views: 1029

Answers (1)

Pavel
Pavel

Reputation: 2554

I see two ways:

  1. Use dynamic body instead of kinematic. To move body you can set gravityScale to zero and move by setting velocity, or use b2MotorJoint, b2MouseJoint or smth else.
  2. If you really need kinematic body, and you want to catch contacts with a ground (by setting contact listener for example), then you should attach to this kinematic body another dynamic body via b2WeldJoint. Then, you can listen contacts of this dynamic body, and react as you wish.

Upvotes: 5

Related Questions