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

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

Introduction

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

Prototype

public void initialize(Body bodyA, Body bodyB, Vector2 anchorA, Vector2 anchorB) 

Source Link

Document

Initialize the bodies, anchors, and length using the world anchors.

Usage

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

License:Open Source License

/**
 * As distance joint def.//from   w  w w .  j  av a2s  . c o m
 *
 * @param bodyA the body a
 * @param bodyB the body b
 * @param _j the _j
 * @return the distance joint def
 */
public static DistanceJointDef asDistanceJointDef(Body bodyA, Body bodyB, CGJoint _j) {
    DistanceJointDef jdef = new DistanceJointDef();
    jdef.collideConnected = _j.getDistanceJointDef().getCollideConnected();
    jdef.dampingRatio = _j.getDistanceJointDef().getDampingRatio();
    jdef.frequencyHz = _j.getDistanceJointDef().getFreqencyHz();
    jdef.initialize(bodyA, bodyB, bodyA.getWorldCenter(), 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   ww  w.  jav  a 2  s . c  o  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);
            }
        });
    }
}