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

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:com.agateau.pixelwheels.racer.Vehicle.java

License:Open Source License

public WheelInfo addWheel(TextureRegion region, float x, float y, float angle) {
    WheelInfo info = new WheelInfo();
    info.wheel = new Wheel(mGameWorld, this, region, getX() + x, getY() + y, angle);
    mWheels.add(info);//  w w  w  . j a va 2s . co  m

    Body body = info.wheel.getBody();
    body.setUserData(mBody.getUserData());

    RevoluteJointDef jointDef = new RevoluteJointDef();
    // Call initialize() instead of defining bodies and anchors manually. Defining anchors manually
    // causes Box2D to move the car a bit while it solves the constraints defined by the joints
    jointDef.initialize(mBody, body, body.getPosition());
    jointDef.lowerAngle = 0;
    jointDef.upperAngle = 0;
    jointDef.enableLimit = true;
    info.joint = (RevoluteJoint) mGameWorld.getBox2DWorld().createJoint(jointDef);

    return info;
}

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

License:Apache License

@Override
protected void createWorld(World world) {
    Body ground;/*  w ww  .j a va  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.Bridge.java

License:Apache License

@Override
protected void createWorld(World world) {
    Body ground;//from ww  w  .j  a va2  s .  com
    {
        BodyDef bd = new BodyDef();
        ground = world.createBody(bd);

        EdgeShape shape = new EdgeShape();
        shape.set(new Vector2(-40, 0), new Vector2(40.0f, 0));

        ground.createFixture(shape, 0);
        shape.dispose();
    }

    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 0.125f);
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 20.0f;
        fd.friction = 0.2f;

        RevoluteJointDef jd = new RevoluteJointDef();

        Body prevBody = ground;

        for (int i = 0; i < e_count; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;
            bd.position.set(-14.5f + 1.0f * i, 5.0f);
            Body body = world.createBody(bd);
            body.createFixture(fd);

            Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 5.0f);
            jd.initialize(prevBody, body, anchor);
            world.createJoint(jd);
            prevBody = body;
        }

        Vector2 anchor = new Vector2(-15.0f + 1.0f * e_count, 5.0f);
        jd.initialize(prevBody, ground, anchor);
        world.createJoint(jd);
        shape.dispose();
    }

    for (int i = 0; i < 2; i++) {
        Vector2[] vertices = new Vector2[3];
        vertices[0] = new Vector2(-0.5f, 0);
        vertices[1] = new Vector2(0.5f, 0);
        vertices[2] = new Vector2(0, 1.5f);

        PolygonShape shape = new PolygonShape();
        shape.set(vertices);

        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 1.0f;

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.position.set(-8.0f + 8.0f * i, 12.0f);
        Body body = world.createBody(bd);
        body.createFixture(fd);

        shape.dispose();
    }

    for (int i = 0; i < 3; i++) {
        CircleShape shape = new CircleShape();
        shape.setRadius(0.5f);

        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 1.0f;

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.position.set(-6.0f + 6.0f * i, 10.0f);

        Body body = world.createBody(bd);
        body.createFixture(fd);

        shape.dispose();
    }
}

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

License:Apache License

@Override
protected void createWorld(World world) {
    Body ground;/*ww w . ja v  a  2s  . co  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.0f);
        shape.dispose();
    }

    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.6f, 0.125f);

        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 20.0f;
        fd.friction = 0.2f;

        RevoluteJointDef jd = new RevoluteJointDef();
        jd.collideConnected = false;

        float y = 25.0f;
        Body prevBody = ground;

        for (int i = 0; i < 30; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;
            bd.position.set(0.5f + i, y);
            Body body = world.createBody(bd);
            body.createFixture(fd);

            Vector2 anchor = new Vector2(i, y);
            jd.initialize(prevBody, body, anchor);
            world.createJoint(jd);
            prevBody = body;
        }

        shape.dispose();
    }
}

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

License:Open Source License

/**
 * As revolute joint./* w  ww.j  a va 2s.  co  m*/
 *
 * @param bodyA the body a
 * @param bodyB the body b
 * @param _j the _j
 * @return the revolute joint def
 */
public static RevoluteJointDef asRevoluteJoint(Body bodyA, Body bodyB, CGJoint _j) {
    RevoluteJointDef jdef = new RevoluteJointDef();
    jdef.collideConnected = _j.getRevoluteJointDef().getCollideConnected();
    jdef.enableLimit = _j.getRevoluteJointDef().getEnableLimit();
    jdef.enableMotor = _j.getRevoluteJointDef().getEnableMotor();
    jdef.lowerAngle = _j.getRevoluteJointDef().getLowerAngle();
    jdef.maxMotorTorque = _j.getRevoluteJointDef().getMaxMotorTorque();
    jdef.motorSpeed = _j.getRevoluteJointDef().getMotorSpeed();
    jdef.referenceAngle = _j.getRevoluteJointDef().getReferenceAngle();
    jdef.upperAngle = _j.getRevoluteJointDef().getUpperAngle();

    jdef.initialize(bodyA, bodyB, bodyA.getWorldCenter());

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

}

From source file:com.netthreads.gdx.app.layer.SimulationLayer.java

License:Apache License

/**
 * Create model in view.// w w  w.  j a  v  a  2 s  .c  om
 * 
 */
private void createCentralModel() {
    // Create a FixtureAtlas which will automatically load the fixture
    // list for every body defined with the editor.
    FixtureAtlas atlas = new FixtureAtlas(Gdx.files.internal("data/drawing.bin"));

    // DEFINE CENTRAL BODY
    BodyDef centralBodyDef = new BodyDef();

    // PIN BODY IN CENTRE
    centralBodyDef.type = BodyType.StaticBody;
    float centreX = (getWidth() / pixelsPerMetre) / 2;
    float centreY = (getHeight() / pixelsPerMetre) / 2;
    centralBodyDef.position.set(centreX, centreY);
    centralBodyDef.fixedRotation = false;
    fixedBody = world.createBody(centralBodyDef);

    // PIN FIXTURE
    CircleShape pivot = new CircleShape();
    pivot.setRadius(CENTRE_BODY_SIZE.x / 2 - 0.9f);
    fixedBody.createFixture(pivot, 0.1f);
    pivot.dispose();

    // BODY WHICH WE WILL CONNECT TO CENTRAL PIN BODY
    centralBodyDef.type = BodyType.DynamicBody;
    float x = centreX - CENTRE_BODY_SIZE.x / 2;
    float y = centreY - CENTRE_BODY_SIZE.y / 2;
    centralBodyDef.position.set(x, y);
    centralBodyDef.fixedRotation = false;
    centreBody = world.createBody(centralBodyDef);

    // Fixture definition is required to make joint work.
    FixtureDef fd = new FixtureDef();
    fd.friction = 0.6f;
    fd.density = 2.0f;

    // CENTRAL FIXTURE
    atlas.createFixtures(centreBody, "cogA.png", CENTRE_BODY_SIZE.x, CENTRE_BODY_SIZE.y, fd);

    // JOINT
    RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
    revoluteJointDef.initialize(fixedBody, centreBody, fixedBody.getWorldCenter());

    revoluteJointDef.enableMotor = true;
    revoluteJointDef.motorSpeed = 1.0f;
    revoluteJointDef.maxMotorTorque = 500;

    world.createJoint(revoluteJointDef);
}

From source file:es.eucm.ead.engine.gameobjects.effects.PhysicsEffectGO.java

License:Open Source License

@Override
public void initialize() {
    super.initialize();

    // doStep true = not simulate inactive bodies
    world = new World(new Vector2(0.0f, 10.0f), true);
    world.setContinuousPhysics(true);//from   ww  w .j  a va2  s .  c o  m
    world.setWarmStarting(true);
    world.setAutoClearForces(true);
    ValueMap valueMap = game.getGameState();
    valueMap.setValue((String) null, VAR_PH_WORLD, world);

    velocityIterations = 24;
    positionIterations = 8;

    for (SceneElement e : effect.getElements()) {
        createBody(world, e, valueMap);
    }

    for (SceneElement e : effect.getJoints()) {
        createBody(world, e, valueMap);
    }

    RevoluteJointDef jd = new RevoluteJointDef();
    jd.collideConnected = false;

    for (int i = 0; i < effect.getJoints().size() - 1; i += 2) {
        SceneElement e1 = effect.getJoints().get(i);
        SceneElement e2 = effect.getJoints().get(i + 1);
        Body b1 = (Body) valueMap.getValue(e1.getId(), VAR_PH_BODY, null);
        Body b2 = (Body) valueMap.getValue(e2.getId(), VAR_PH_BODY, null);
        jd.initialize(b2, b1, new Vector2(b1.getPosition().x, b1.getPosition().y));
        world.createJoint(jd);
    }

}

From source file:Helper.WorldUtils.java

public static void createAntelope(World world, Body body, Body backBody) {
    RevoluteJointDef jointDef = new RevoluteJointDef();
    jointDef.initialize(backBody, body, backBody.getWorldCenter());
    jointDef.collideConnected = true;//from w  ww  .  ja v  a2 s . c  o m
    world.createJoint(jointDef);
}

From source file:releasethekraken.entity.seacreature.kraken.EntityKrakenGripper.java

@Override
protected void spawnInWorld(float x, float y, float xVel, float yVel) {
    //Set up hitbox shape - Defines the hitbox
    PolygonShape hitbox = new PolygonShape();
    //hitbox.setAsBox(1.5F, 0.5F, new Vector2(-0.5F, 0), 0);
    hitbox.set(new float[] { -1.5F, -0.5F, //bottom left
            -1.5F, 0.5F, //top left
            0.0F, 0.5F, //top middle
            1.25F, 0.0F, //pointy part
            0.0F, -0.5F //bottom middle
    });//from w ww .  j  a v a  2s .com

    //Set up body definition - Defines the type of physics body that this is
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(x, y);

    //Set up physics body - Defines the actual physics body
    this.physBody = this.world.getPhysWorld().createBody(bodyDef);
    this.physBody.setUserData(this); //Store this object into the body so that it isn't lost

    //Set up physics fixture - Defines physical properties
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = hitbox;
    fixtureDef.density = 100.0F; //About 1 g/cm^2 (2D), which is the density of water, which is roughly the density of humans.
    fixtureDef.friction = 0.1F; //friction with other objects
    fixtureDef.restitution = 0.0F; //Bouncyness

    //Set which collision type this object is
    fixtureDef.filter.categoryBits = COL_SEA_CREATURE;
    //Set which collision types this object collides with
    fixtureDef.filter.maskBits = COL_ALL ^ COL_SEA_PROJECTILE; //Collide with everything except sea creature projectiles

    this.physBody.createFixture(fixtureDef);

    //Set up the physics joint
    RevoluteJointDef jointDef = new RevoluteJointDef();
    jointDef.initialize(this.parent.getPhysBody(), this.physBody, new Vector2(x - 1.5F, y));
    jointDef.collideConnected = false;

    jointDef.enableLimit = true;
    float jointRange = 240 * MathUtils.degreesToRadians;

    jointDef.lowerAngle = 0 - (jointRange / 2);
    jointDef.upperAngle = (jointRange / 2);

    this.physBody.getWorld().createJoint(jointDef); //Create the physics joint

    //Set the linear damping
    this.physBody.setLinearDamping(5F);

    //Set the angular damping
    this.physBody.setAngularDamping(2.5F);

    //Apply impulse
    this.physBody.applyLinearImpulse(xVel, yVel, x, y, true);

    //Dispose of the hitbox shape, which is no longer needed
    hitbox.dispose();
}

From source file:releasethekraken.entity.seacreature.kraken.EntityKrakenTenticle.java

@Override
protected void spawnInWorld(float x, float y, float xVel, float yVel) {
    //******************** Main Segment ********************

    //Set up hitbox shape - Defines the hitbox
    PolygonShape hitbox = new PolygonShape();
    hitbox.setAsBox(2.0F, 0.5F, new Vector2(0, 0), 0);

    //Set up body definition - Defines the type of physics body that this is
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(x, y);//from w  w w .  j a  v a 2  s .  c  o  m

    //Set up physics body - Defines the actual physics body
    this.physBody = this.world.getPhysWorld().createBody(bodyDef);
    this.physBody.setUserData(this); //Store this object into the body so that it isn't lost

    //Set up physics fixture - Defines physical properties
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = hitbox;
    fixtureDef.density = 100.0F; //About 1 g/cm^2 (2D), which is the density of water, which is roughly the density of humans.
    fixtureDef.friction = 0.05F; //friction with other objects
    fixtureDef.restitution = 0.0F; //Bouncyness

    //Set which collision type this object is
    fixtureDef.filter.categoryBits = COL_SEA_CREATURE;
    //Set which collision types this object collides with
    fixtureDef.filter.maskBits = COL_ALL ^ COL_SEA_PROJECTILE; //Collide with everything except sea creature projectiles

    this.physBody.createFixture(fixtureDef);

    //Set up the physics joint
    RevoluteJointDef jointDef = new RevoluteJointDef();
    jointDef.initialize(this.parent.getPhysBody(), this.physBody, new Vector2(x - 1.5F, y));
    jointDef.collideConnected = false;

    jointDef.enableLimit = true;
    float jointRange = 220 * MathUtils.degreesToRadians;

    jointDef.lowerAngle = 0 - (jointRange / 2);
    jointDef.upperAngle = (jointRange / 2);

    this.physBody.getWorld().createJoint(jointDef); //Create the physics joint

    //Set the linear damping
    this.physBody.setLinearDamping(5F);

    //Set the angular damping
    this.physBody.setAngularDamping(2.5F);

    //Apply impulse
    this.physBody.applyLinearImpulse(xVel, yVel, x, y, true);

    //Dispose of the hitbox shape, which is no longer needed
    hitbox.dispose();
}