How to make moving Kinematic platform with Box2d / LibGdx

62 Views Asked by At

I am currently working on a project and im trying to implement a moving platform that goes up and down continuously from A to B. I've tried some stuff and i can't get it to move. It appears as a box on the screen but it is static. This is my current code:

 public void createMovingPlatform(PolygonMapObject polygonMapObject) {

    // Create the body
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.KinematicBody;
    Body body = gameScreen.getWorld().createBody(bodyDef);
    Shape shape = createPolygonShape(polygonMapObject);
    body.createFixture(shape, 1f);
    
    shape.dispose();

    
    // Create the joint
    PrismaticJointDef jointDef = new PrismaticJointDef();
    jointDef.initialize(body, gameScreen.getWorld().createBody(new BodyDef()), body.getWorldCenter(), new Vector2(0f, 1f));
    jointDef.collideConnected = false;
    jointDef.enableLimit = true; // enable the limits to restrict the movement
    jointDef.lowerTranslation = 0; // the lower limit of the joint
    jointDef.upperTranslation = 10000; // the upper limit of the joint
    jointDef.enableMotor = true; // enable the motor to move the body
    jointDef.maxMotorForce = 1000f;
    jointDef.motorSpeed = 5;
    PrismaticJoint joint = (PrismaticJoint) gameScreen.getWorld().createJoint(jointDef);
    
    joint.setMotorSpeed(5);
    }   

and this is the createPolygonShape method:

private Shape createPolygonShape(PolygonMapObject polygonMapObject) {
        float[] vertices = polygonMapObject.getPolygon().getTransformedVertices();
        Vector2[] worldVertices = new Vector2[vertices.length/2];

        for(int i = 0; i < vertices.length / 2; i++){
            Vector2 current = new Vector2(vertices[i*2] / PPM, vertices[i*2+1] / PPM);
            worldVertices[i] = current;

        }

        PolygonShape shape = new PolygonShape();

        shape.set(worldVertices);

        return shape;

    }
0

There are 0 best solutions below