Search code examples
androidactionscript-3flashairbox2d

Prismatic Joints for Box2d As3 won't work?


im trying to create a moving platform in box2d using prismatic joints, all that is created is a box and no joints are apparent, what am I doing wrong? Here is my code

        var prismaticJoint:b2PrismaticJoint;
        ...
        {
         ...
        BodyDef.position.Set(0 / RATIO, 0 / RATIO);
        floorDef.SetAsBox(250 / RATIO, 10 / RATIO);

        fixtureDef.shape = floorDef;
        fixtureDef.friction = 0.5;
        fixtureDef.density = 0.5;
        fixtureDef.restitution = 0;

        movePlatformBody = PhysiVals.world.CreateBody(BodyDef);
        movePlatformBody.CreateFixture(fixtureDef);


        var prismaticJointDef:b2PrismaticJointDef = new b2PrismaticJointDef();
        prismaticJointDef.Initialize(body, movePlatformBody, movePlatformBody.GetWorldCenter(), new b2Vec2(0,1));
        prismaticJointDef.enableLimit = true;
        prismaticJointDef.enableMotor = true;
        prismaticJointDef.lowerTranslation = 0;
        prismaticJointDef.maxMotorForce = 100;
        prismaticJointDef.motorSpeed = 1;
        prismaticJointDef.upperTranslation = 270 / PhysiVals.RATIO;
        PhysiVals.world.CreateJoint(prismaticJointDef);
        //prismaticJoint = PhysiVals.world.CreateJoint(prismaticJointDef as b2JointDef) as b2PrismaticJoint;

I tried 2 ways of creating it in the world, I dont know which one to use. How do I create a moving platform in box2d? Thank you for taking the time to read this.


Solution

  • Try modifying your initialize statement. Set the first parameter to the body that is going to move (the platform), set the second parameter to the static body (the world), and the third parameter will be the platforms centre.

    I wrote a pretty popular tutorial on Box2D joints with an example of Prismatic Joints with source code so have a look at that too if you are still having problems Tutorial.