Example usage for com.badlogic.gdx.physics.box2d.joints PrismaticJointDef initialize

List of usage examples for com.badlogic.gdx.physics.box2d.joints PrismaticJointDef initialize

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d.joints PrismaticJointDef initialize.

Prototype

public void initialize(Body bodyA, Body bodyB, Vector2 anchor, Vector2 axis) 

Source Link

Document

Initialize the bodies, anchors, axis, and reference angle using the world anchor and world axis.

Usage

From source file:com.badlogic.gdx.tests.box2d.BodyTypes.java

License:Apache License

@Override
protected void createWorld(World world) {
    Body ground;//from   ww  w  .j a  v  a  2 s. c  o  m

    {
        BodyDef bd = new BodyDef();
        ground = world.createBody(bd);

        EdgeShape shape = new EdgeShape();
        shape.set(new Vector2(-20, 0), new Vector2(20, 0));

        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        ground.createFixture(fd);
        shape.dispose();
    }

    {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.position.set(0, 3.0f);
        m_attachment = world.createBody(bd);

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 2.0f);
        m_attachment.createFixture(shape, 2.0f);
        shape.dispose();
    }

    {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.position.set(-4.0f, 5.0f);
        m_platform = world.createBody(bd);

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 4.0f, new Vector2(4.0f, 0), 0.5f * (float) Math.PI);

        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.friction = 0.6f;
        fd.density = 2.0f;

        m_platform.createFixture(fd);
        shape.dispose();

        RevoluteJointDef rjd = new RevoluteJointDef();
        rjd.initialize(m_attachment, m_platform, new Vector2(0, 5.0f));
        rjd.maxMotorTorque = 50.0f;
        rjd.enableMotor = true;
        world.createJoint(rjd);

        PrismaticJointDef pjd = new PrismaticJointDef();
        pjd.initialize(ground, m_platform, new Vector2(0, 5.0f), new Vector2(1, 0));

        pjd.maxMotorForce = 1000.0f;
        pjd.enableMotor = true;
        pjd.lowerTranslation = -10f;
        pjd.upperTranslation = 10.0f;
        pjd.enableLimit = true;

        world.createJoint(pjd);

        m_speed = 3.0f;
    }

    {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.position.set(0, 8.0f);
        Body body = world.createBody(bd);

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.75f, 0.75f);

        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.friction = 0.6f;
        fd.density = 2.0f;

        body.createFixture(fd);
        shape.dispose();
    }
}

From source file:com.badlogic.gdx.tests.box2d.Prismatic.java

License:Apache License

@Override
protected void createWorld(World world) {
    Body ground;//  ww w. j  a  va  2  s  .c o m

    {
        BodyDef bd = new BodyDef();
        ground = world.createBody(bd);
        EdgeShape shape = new EdgeShape();
        shape.set(new Vector2(-40, 0), new Vector2(40, 0));
        ground.createFixture(shape, 0);
        shape.dispose();
    }

    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(2, 5);

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.position.set(-10, 10);
        bd.angle = 0.5f * (float) Math.PI;
        bd.allowSleep = false;

        Body body = world.createBody(bd);
        body.createFixture(shape, 5.0f);

        PrismaticJointDef pjd = new PrismaticJointDef();

        Vector2 axis = new Vector2(2, 1);
        axis.nor();
        pjd.initialize(ground, body, new Vector2(0, 0), axis);

        pjd.motorSpeed = 10.0f;
        pjd.maxMotorForce = 10000.0f;
        pjd.enableMotor = true;
        pjd.lowerTranslation = 0;
        pjd.upperTranslation = 20.0f;
        pjd.enableLimit = true;

        m_joint = (PrismaticJoint) world.createJoint(pjd);
    }
}

From source file:com.laex.cg2d.render.util.ProtoBufTypeConversionUtil.java

License:Open Source License

/**
 * As primastic joint def.//from   ww w  . j  a v a2  s  . co  m
 *
 * @param bodyA the body a
 * @param bodyB the body b
 * @param _j the _j
 * @return the prismatic joint def
 */
public static PrismaticJointDef asPrimasticJointDef(Body bodyA, Body bodyB, CGJoint _j) {
    PrismaticJointDef jdef = new PrismaticJointDef();
    jdef.collideConnected = _j.getPrismaticJointDef().getCollideConnected();
    jdef.enableLimit = _j.getPrismaticJointDef().getEnableLimit();
    jdef.enableMotor = _j.getPrismaticJointDef().getEnableMotor();
    jdef.referenceAngle = _j.getPrismaticJointDef().getReferenceAngle();
    jdef.lowerTranslation = _j.getPrismaticJointDef().getLowerTranslation();
    jdef.maxMotorForce = _j.getPrismaticJointDef().getMaxMotorForce();
    jdef.upperTranslation = _j.getPrismaticJointDef().getUpperTranslation();
    jdef.motorSpeed = _j.getPrismaticJointDef().getMotorSpeed();

    jdef.initialize(bodyA, bodyB, ProtoBufTypeConversionUtil.asVector2(_j.getPrismaticJointDef().getAnchor()),
            ProtoBufTypeConversionUtil.asVector2(_j.getPrismaticJointDef().getAxis()));

    if (_j.getUseLocalAnchors()) {
        jdef.localAnchorA.set(Vector2Adapter.asVector2(_j.getLocalAnchorA()));
        jdef.localAnchorB.set(Vector2Adapter.asVector2(_j.getLocalAnchorB()));
    }

    return jdef;
}

From source file:com.tnf.ptm.entities.ship.ShipBuilder.java

License:Apache License

private PrismaticJoint createDoorJoint(Body shipBody, World w, Vector2 shipPos, Vector2 doorRelPos,
        float shipAngle) {
    Body doorBody = createDoorBody(w, shipPos, doorRelPos, shipAngle);
    PrismaticJointDef jd = new PrismaticJointDef();
    jd.initialize(shipBody, doorBody, shipPos, Vector2.Zero);
    jd.localAxisA.set(1, 0);//from   w w  w  . jav  a2 s  .com
    jd.collideConnected = false;
    jd.enableLimit = true;
    jd.enableMotor = true;
    jd.lowerTranslation = 0;
    jd.upperTranslation = Door.DOOR_LEN;
    jd.maxMotorForce = 2;
    return (PrismaticJoint) w.createJoint(jd);
}

From source file:org.zapylaev.game.truetennis.core.model.Box2dStick.java

License:Open Source License

void setPrismaticJointEnabled(boolean enabled) {
    if (enabled) {
        PrismaticJointDef prismaticJointDef = new PrismaticJointDef();
        prismaticJointDef.initialize(mWorld.createBody(new BodyDef()), mBody, new Vector2(0, 0),
                new Vector2(0, 1));
        prismaticJointDef.enableLimit = true;
        prismaticJointDef.upperTranslation = Constants.SCREEN_HEIGHT / 2 - Box2dStick.HEIGHT / 2
                - PRISMATIC_JOINT_OFFSET;
        prismaticJointDef.lowerTranslation = -Constants.SCREEN_HEIGHT / 2 + Box2dStick.HEIGHT / 2
                + PRISMATIC_JOINT_OFFSET;

        mPrismaticJoint = (PrismaticJoint) mWorld.createJoint(prismaticJointDef);
    } else {//from ww w  . ja  v a  2s .com
        if (mPrismaticJoint != null) {
            mWorld.destroyJoint(mPrismaticJoint);
        }
    }
}