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

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

Introduction

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

Prototype

public void initialize(Body body1, Body body2, Vector2 anchor) 

Source Link

Usage

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

License:Apache License

@Override
protected void createWorld(World world) {
    Body ground;/*  www .  ja  va2s  .c  om*/
    {
        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(0.5f, 0.125f);

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

        WeldJointDef jd = new WeldJointDef();

        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 * i, 5.0f);
            jd.initialize(prevBody, body, anchor);
            world.createJoint(jd);
            prevBody = body;
        }

        shape.dispose();
    }

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

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

        WeldJointDef jd = new WeldJointDef();

        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, 15.0f);
            bd.gravityScale = 10.0f;
            Body body = world.createBody(bd);
            body.createFixture(fd);

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

            prevBody = body;
        }

        shape.dispose();
    }

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

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

        WeldJointDef jd = new WeldJointDef();

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

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

            prevBody = body;
        }

        shape.dispose();
    }

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

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

        WeldJointDef jd = new WeldJointDef();

        Body prevBody = ground;
        for (int i = 0; i < e_count; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;
            bd.position.set(5.5f + 1.0f * i, 10.0f);
            bd.gravityScale = 10.0f;
            Body body = world.createBody(bd);
            body.createFixture(fd);

            if (i > 0) {
                Vector2 anchor = new Vector2(5.0f + 1.0f * i, 10.0f);
                jd.initialize(prevBody, body, anchor);
                world.createJoint(jd);
            }

            prevBody = body;
        }

        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 < 2; 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.laex.cg2d.render.util.ProtoBufTypeConversionUtil.java

License:Open Source License

/**
 * As weld joint def./*from   w w w.  j  a  va  2 s  .  c  o  m*/
 *
 * @param bodyA the body a
 * @param bodyB the body b
 * @param _j the _j
 * @return the weld joint def
 */
public static WeldJointDef asWeldJointDef(Body bodyA, Body bodyB, CGJoint _j) {
    WeldJointDef jdef = new WeldJointDef();
    jdef.collideConnected = _j.getWeldJointDef().getCollideConnected();
    jdef.initialize(bodyA, bodyB, bodyB.getWorldCenter());

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

    return jdef;
}

From source file:edu.lehigh.cse.lol.Physics.java

License:Open Source License

/**
 * When a hero collides with a "sticky" obstacle, this is the code we run to
 * figure out what to do//from  w  w w  .  j a  v  a 2 s . co  m
 *
 * @param sticky  The sticky actor... it should always be an obstacle for now
 * @param other   The other actor... it should always be a hero for now
 * @param contact A description of the contact event
 */
static void handleSticky(final Actor sticky, final Actor other, Contact contact) {
    // don't create a joint if we've already got one
    if (other.mDJoint != null)
        return;
    // don't create a joint if we're supposed to wait
    if (System.currentTimeMillis() < other.mStickyDelay)
        return;
    // handle sticky obstacles... only do something if we're hitting the
    // obstacle from the correct direction
    if ((sticky.mIsSticky[0] && other.getYPosition() >= sticky.getYPosition() + sticky.mSize.y)
            || (sticky.mIsSticky[1] && other.getXPosition() + other.mSize.x <= sticky.getXPosition())
            || (sticky.mIsSticky[3] && other.getXPosition() >= sticky.getXPosition() + sticky.mSize.x)
            || (sticky.mIsSticky[2] && other.getYPosition() + other.mSize.y <= sticky.getYPosition())) {
        // create distance and weld joints... somehow, the combination is
        // needed to get this to work. Note that this function runs during
        // the box2d step, so we need to make the joint in a callback that
        // runs later
        final Vector2 v = contact.getWorldManifold().getPoints()[0];
        Lol.sGame.mCurrentLevel.mOneTimeEvents.add(new LolAction() {
            @Override
            public void go() {
                other.mBody.setLinearVelocity(0, 0);
                DistanceJointDef d = new DistanceJointDef();
                d.initialize(sticky.mBody, other.mBody, v, v);
                d.collideConnected = true;
                other.mDJoint = (DistanceJoint) Lol.sGame.mCurrentLevel.mWorld.createJoint(d);
                WeldJointDef w = new WeldJointDef();
                w.initialize(sticky.mBody, other.mBody, v);
                w.collideConnected = true;
                other.mWJoint = (WeldJoint) Lol.sGame.mCurrentLevel.mWorld.createJoint(w);
            }
        });
    }
}