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

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

Introduction

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

Prototype

public RevoluteJointDef() 

Source Link

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);//from ww w. jav a 2s.c o  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;/*from  www .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.Bridge.java

License:Apache License

@Override
protected void createWorld(World world) {
    Body ground;//from ww  w.j  ava2  s  . co  m
    {
        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;//w  ww. j  ava 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.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.cafeitvn.myballgame.screen.Box2DMapObjectParser.java

License:Apache License

/**
 * creates a {@link Joint} from a {@link MapObject}
 * @param mapObject the {@link Joint} which to parse
 * @return the parsed {@link Joint}//w w  w  .  j  a v a  2  s .  com
 */
public Joint createJoint(MapObject mapObject) {
    MapProperties properties = mapObject.getProperties();

    JointDef jointDef = null;

    String type = properties.get("type", String.class);
    if (!type.equals(aliases.joint))
        throw new IllegalArgumentException(
                "type of " + mapObject + " is  \"" + type + "\" instead of \"" + aliases.joint + "\"");

    String jointType = properties.get(aliases.jointType, String.class);

    // get all possible values
    if (jointType.equals(aliases.distanceJoint)) {
        DistanceJointDef distanceJointDef = new DistanceJointDef();
        distanceJointDef.dampingRatio = (Float) getProperty(properties, aliases.dampingRatio,
                distanceJointDef.dampingRatio, Float.class);
        distanceJointDef.frequencyHz = (Float) getProperty(properties, aliases.frequencyHz,
                distanceJointDef.frequencyHz, Float.class);
        distanceJointDef.length = (Float) getProperty(properties, aliases.length, distanceJointDef.length,
                Float.class) * (tileWidth + tileHeight) / 2 * unitScale;
        distanceJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, distanceJointDef.localAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, distanceJointDef.localAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        distanceJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, distanceJointDef.localAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, distanceJointDef.localAnchorB.y,
                        Float.class) * tileHeight * unitScale);

        jointDef = distanceJointDef;
    } else if (jointType.equals(aliases.frictionJoint)) {
        FrictionJointDef frictionJointDef = new FrictionJointDef();
        frictionJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, frictionJointDef.localAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, frictionJointDef.localAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        frictionJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, frictionJointDef.localAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, frictionJointDef.localAnchorB.y,
                        Float.class) * tileHeight * unitScale);
        frictionJointDef.maxForce = (Float) getProperty(properties, aliases.maxForce, frictionJointDef.maxForce,
                Float.class);
        frictionJointDef.maxTorque = (Float) getProperty(properties, aliases.maxTorque,
                frictionJointDef.maxTorque, Float.class);

        jointDef = frictionJointDef;
    } else if (jointType.equals(aliases.gearJoint)) {
        GearJointDef gearJointDef = new GearJointDef();
        gearJointDef.joint1 = joints.get(properties.get(aliases.joint1, String.class));
        gearJointDef.joint2 = joints.get(properties.get(aliases.joint2, String.class));
        gearJointDef.ratio = (Float) getProperty(properties, aliases.ratio, gearJointDef.ratio, Float.class);

        jointDef = gearJointDef;
    } else if (jointType.equals(aliases.mouseJoint)) {
        MouseJointDef mouseJointDef = new MouseJointDef();
        mouseJointDef.dampingRatio = (Float) getProperty(properties, aliases.dampingRatio,
                mouseJointDef.dampingRatio, Float.class);
        mouseJointDef.frequencyHz = (Float) getProperty(properties, aliases.frequencyHz,
                mouseJointDef.frequencyHz, Float.class);
        mouseJointDef.maxForce = (Float) getProperty(properties, aliases.maxForce, mouseJointDef.maxForce,
                Float.class);
        mouseJointDef.target.set(
                (Float) getProperty(properties, aliases.targetX, mouseJointDef.target.x, Float.class)
                        * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.targetY, mouseJointDef.target.y, Float.class)
                        * tileHeight * unitScale);

        jointDef = mouseJointDef;
    } else if (jointType.equals(aliases.prismaticJoint)) {
        PrismaticJointDef prismaticJointDef = new PrismaticJointDef();
        prismaticJointDef.enableLimit = (Boolean) getProperty(properties, aliases.enableLimit,
                prismaticJointDef.enableLimit, Boolean.class);
        prismaticJointDef.enableMotor = (Boolean) getProperty(properties, aliases.enableMotor,
                prismaticJointDef.enableMotor, Boolean.class);
        prismaticJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, prismaticJointDef.localAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, prismaticJointDef.localAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        prismaticJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, prismaticJointDef.localAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, prismaticJointDef.localAnchorB.y,
                        Float.class) * tileHeight * unitScale);
        prismaticJointDef.localAxisA.set(
                (Float) getProperty(properties, aliases.localAxisAX, prismaticJointDef.localAxisA.x,
                        Float.class),
                (Float) getProperty(properties, aliases.localAxisAY, prismaticJointDef.localAxisA.y,
                        Float.class));
        prismaticJointDef.lowerTranslation = (Float) getProperty(properties, aliases.lowerTranslation,
                prismaticJointDef.lowerTranslation, Float.class) * (tileWidth + tileHeight) / 2 * unitScale;
        prismaticJointDef.maxMotorForce = (Float) getProperty(properties, aliases.maxMotorForce,
                prismaticJointDef.maxMotorForce, Float.class);
        prismaticJointDef.motorSpeed = (Float) getProperty(properties, aliases.motorSpeed,
                prismaticJointDef.motorSpeed, Float.class);
        prismaticJointDef.referenceAngle = (Float) getProperty(properties, aliases.referenceAngle,
                prismaticJointDef.referenceAngle, Float.class);
        prismaticJointDef.upperTranslation = (Float) getProperty(properties, aliases.upperTranslation,
                prismaticJointDef.upperTranslation, Float.class) * (tileWidth + tileHeight) / 2 * unitScale;

        jointDef = prismaticJointDef;
    } else if (jointType.equals(aliases.pulleyJoint)) {
        PulleyJointDef pulleyJointDef = new PulleyJointDef();
        pulleyJointDef.groundAnchorA.set(
                (Float) getProperty(properties, aliases.groundAnchorAX, pulleyJointDef.groundAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.groundAnchorAY, pulleyJointDef.groundAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        pulleyJointDef.groundAnchorB.set(
                (Float) getProperty(properties, aliases.groundAnchorBX, pulleyJointDef.groundAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.groundAnchorBY, pulleyJointDef.groundAnchorB.y,
                        Float.class) * tileHeight * unitScale);
        pulleyJointDef.lengthA = (Float) getProperty(properties, aliases.lengthA, pulleyJointDef.lengthA,
                Float.class) * (tileWidth + tileHeight) / 2 * unitScale;
        pulleyJointDef.lengthB = (Float) getProperty(properties, aliases.lengthB, pulleyJointDef.lengthB,
                Float.class) * (tileWidth + tileHeight) / 2 * unitScale;
        pulleyJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, pulleyJointDef.localAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, pulleyJointDef.localAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        pulleyJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, pulleyJointDef.localAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, pulleyJointDef.localAnchorB.y,
                        Float.class) * tileHeight * unitScale);
        pulleyJointDef.ratio = (Float) getProperty(properties, aliases.ratio, pulleyJointDef.ratio,
                Float.class);

        jointDef = pulleyJointDef;
    } else if (jointType.equals(aliases.revoluteJoint)) {
        RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
        revoluteJointDef.enableLimit = (Boolean) getProperty(properties, aliases.enableLimit,
                revoluteJointDef.enableLimit, Boolean.class);
        revoluteJointDef.enableMotor = (Boolean) getProperty(properties, aliases.enableMotor,
                revoluteJointDef.enableMotor, Boolean.class);
        revoluteJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, revoluteJointDef.localAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, revoluteJointDef.localAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        revoluteJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, revoluteJointDef.localAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, revoluteJointDef.localAnchorB.y,
                        Float.class) * tileHeight * unitScale);
        revoluteJointDef.lowerAngle = (Float) getProperty(properties, aliases.lowerAngle,
                revoluteJointDef.lowerAngle, Float.class);
        revoluteJointDef.maxMotorTorque = (Float) getProperty(properties, aliases.maxMotorTorque,
                revoluteJointDef.maxMotorTorque, Float.class);
        revoluteJointDef.motorSpeed = (Float) getProperty(properties, aliases.motorSpeed,
                revoluteJointDef.motorSpeed, Float.class);
        revoluteJointDef.referenceAngle = (Float) getProperty(properties, aliases.referenceAngle,
                revoluteJointDef.referenceAngle, Float.class);
        revoluteJointDef.upperAngle = (Float) getProperty(properties, aliases.upperAngle,
                revoluteJointDef.upperAngle, Float.class);

        jointDef = revoluteJointDef;
    } else if (jointType.equals(aliases.ropeJoint)) {
        RopeJointDef ropeJointDef = new RopeJointDef();
        ropeJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, ropeJointDef.localAnchorA.x, Float.class)
                        * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, ropeJointDef.localAnchorA.y, Float.class)
                        * tileHeight * unitScale);
        ropeJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, ropeJointDef.localAnchorB.x, Float.class)
                        * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, ropeJointDef.localAnchorB.y, Float.class)
                        * tileHeight * unitScale);
        ropeJointDef.maxLength = (Float) getProperty(properties, aliases.maxLength, ropeJointDef.maxLength,
                Float.class) * (tileWidth + tileHeight) / 2 * unitScale;

        jointDef = ropeJointDef;
    } else if (jointType.equals(aliases.weldJoint)) {
        WeldJointDef weldJointDef = new WeldJointDef();
        weldJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, weldJointDef.localAnchorA.x, Float.class)
                        * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, weldJointDef.localAnchorA.y, Float.class)
                        * tileHeight * unitScale);
        weldJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, weldJointDef.localAnchorB.x, Float.class)
                        * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, weldJointDef.localAnchorB.y, Float.class)
                        * tileHeight * unitScale);
        weldJointDef.referenceAngle = (Float) getProperty(properties, aliases.referenceAngle,
                weldJointDef.referenceAngle, Float.class);

        jointDef = weldJointDef;
    } else if (jointType.equals(aliases.wheelJoint)) {
        WheelJointDef wheelJointDef = new WheelJointDef();
        wheelJointDef.dampingRatio = (Float) getProperty(properties, aliases.dampingRatio,
                wheelJointDef.dampingRatio, Float.class);
        wheelJointDef.enableMotor = (Boolean) getProperty(properties, aliases.enableMotor,
                wheelJointDef.enableMotor, Boolean.class);
        wheelJointDef.frequencyHz = (Float) getProperty(properties, aliases.frequencyHz,
                wheelJointDef.frequencyHz, Float.class);
        wheelJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, wheelJointDef.localAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, wheelJointDef.localAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        wheelJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, wheelJointDef.localAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, wheelJointDef.localAnchorB.y,
                        Float.class) * tileHeight * unitScale);
        wheelJointDef.localAxisA.set(
                (Float) getProperty(properties, aliases.localAxisAX, wheelJointDef.localAxisA.x, Float.class),
                (Float) getProperty(properties, aliases.localAxisAY, wheelJointDef.localAxisA.y, Float.class));
        wheelJointDef.maxMotorTorque = (Float) getProperty(properties, aliases.maxMotorTorque,
                wheelJointDef.maxMotorTorque, Float.class);
        wheelJointDef.motorSpeed = (Float) getProperty(properties, aliases.motorSpeed, wheelJointDef.motorSpeed,
                Float.class);

        jointDef = wheelJointDef;
    }

    jointDef.bodyA = bodies.get(properties.get(aliases.bodyA, String.class));
    jointDef.bodyB = bodies.get(properties.get(aliases.bodyB, String.class));
    jointDef.collideConnected = (Boolean) getProperty(properties, aliases.collideConnected,
            jointDef.collideConnected, Boolean.class);

    Joint joint = jointDef.bodyA.getWorld().createJoint(jointDef);

    String name = mapObject.getName();
    if (joints.containsKey(name)) {
        int duplicate = 1;
        while (joints.containsKey(name + duplicate))
            duplicate++;
        name += duplicate;
    }

    joints.put(name, joint);

    return joint;
}

From source file:com.github.unluckyninja.defenseofhuman.model.entity.Rope.java

License:Open Source License

public Rope(GameWorld gameWorld, Body body, Vector2 direction) {
    this.gameWorld = gameWorld;
    world = body.getWorld();//from   w  w w . j  a  va  2  s . c om
    bodyA = body;

    // ???
    Vector2 oriPosition = new Vector2(body.getPosition());
    if (direction.equals(Vector2.Zero)) {
        direction = Vector2.X;
    }
    Vector2 dir = new Vector2(direction);
    dir.nor();
    Vector2 distance = dir.scl(width / 2 * 1.25f); // ,?1/4,?

    // ?body
    bodyDef.angle = (float) Math.toRadians(dir.angle());
    bodyDef.position.set(distance.add(oriPosition));
    bodyDef.linearDamping = 0.3f;
    bodyDef.type = BodyDef.BodyType.DynamicBody;

    Body piece = body.getWorld().createBody(bodyDef);
    piece.setGravityScale(0.1f);

    // ?
    // ??,????
    fixtureDef.isSensor = true;
    fixtureDef.density = 0.001f;

    polygonShape.setAsBox(width / 2, height / 2);

    fixtureDef.shape = polygonShape;

    piece.createFixture(fixtureDef);

    // ?
    RevoluteJointDef rjDef = new RevoluteJointDef();
    rjDef.bodyA = body;
    rjDef.bodyB = piece;

    rjDef.localAnchorA.x = 0;
    rjDef.localAnchorA.y = 0;
    rjDef.localAnchorB.x = -width / 2 * 1.25f;
    rjDef.localAnchorB.y = 0;

    joints.add(body.getWorld().createJoint(rjDef));
    pieces.add(piece);

    piece.setUserData(this);

}

From source file:com.github.unluckyninja.defenseofhuman.model.entity.Rope.java

License:Open Source License

private Rope append(Vector2 direction) {
    //TODO //from   w  w w  .ja v a2s. c o m
    Body body = pieces.get(pieces.size - 1);

    // ???
    oriPosition.set(body.getWorldPoint(temp1.set(width / 2 * 1.25f, 0)));
    if (direction.equals(Vector2.Zero)) {
        direction = Vector2.X;
    }
    dir.set(direction);
    dir.nor();
    dir.scl(width / 2 * 1.25f);

    // ?body
    bodyDef.angle = (float) Math.toRadians(dir.angle());
    bodyDef.position.set(dir.add(oriPosition));

    Body piece = body.getWorld().createBody(bodyDef);
    piece.setGravityScale(0.1f);

    // ?
    fixtureDef.isSensor = true;
    fixtureDef.density = 0.001f;

    polygonShape.setAsBox(width / 2, height / 2);

    fixtureDef.shape = polygonShape;

    piece.createFixture(fixtureDef);

    // ??
    RevoluteJointDef rjDef = new RevoluteJointDef();
    rjDef.bodyA = body;
    rjDef.bodyB = piece;

    rjDef.localAnchorA.x = width / 2 * 1.25f;
    rjDef.localAnchorA.y = 0;
    rjDef.localAnchorB.x = -width / 2 * 1.25f;
    rjDef.localAnchorB.y = 0;

    joints.add(body.getWorld().createJoint(rjDef));
    pieces.add(piece);

    body.setUserData(this);

    return this;
}

From source file:com.johnogel.astrobros.levels.Level.java

protected void attachBodies() {
    /*System.out.println("CONTROLLED BROS SIZE: "+controlled_bros.size+"\nCONTROLLED BODIES SIZE: "+controlled_bodies.size);
    System.out.println("FREE BROS SIZE: "+free_bros.size+"\nFREE BODIES SIZE: "+free_bodies.size);
    System.out.println("BROS SIZE: "+bros.size);*/
    if (to_be_attached.size == to_be_attached_to.size && to_be_attached.size > 0 && controlled_bros.size < 2) {

        for (int i = to_be_attached.size - 1; i < to_be_attached.size; i++) {
            if (Gdx.input.isKeyPressed(Keys.SPACE)) {
                sound_player.playSound(SoundPlayer.STICK_SOUND, .9f);
                RevoluteJointDef joint_def = new RevoluteJointDef();
                joint_def.bodyA = to_be_attached_to.get(i);
                joint_def.bodyB = to_be_attached.get(i);
                joint_def.collideConnected = false;
                //joint_def.localAnchorA.set(0, free_bros.get(i).getRadius()*2);
                float distance = NonPlayer.PUBLIC_RADIUS * 2;

                float a_x = joint_def.bodyA.getPosition().x;
                float a_y = joint_def.bodyA.getPosition().y;
                float b_x = joint_def.bodyB.getPosition().x;
                float b_y = joint_def.bodyB.getPosition().y;

                float angle = MathUtils.atan2(a_y - b_y, a_x - b_x) * MathUtils.radiansToDegrees + 180;

                float x = distance * MathUtils.cosDeg(angle);
                float y = distance * MathUtils.sinDeg(angle);

                joint_def.localAnchorA.set(x, y);

                //angle = MathUtils.atan2(b_y - a_y, b_x - a_x)*MathUtils.radiansToDegrees + 180;

                joint_def.localAnchorB.set(0, 0);

                world.createJoint(joint_def);
                //System.out.println("RADIUS: "+free_bros.get(i).getRadius()*2);
                //controlled_bodies.add(free_bodies.removeIndex(free_bodies.indexOf(to_be_attached.get(i), false)));
                //controlled_bros.add(free_bros.removeIndex(i));

                if (!controlled_bodies.contains(to_be_attached.get(i), false)) {
                    controlled_bodies//from  www . ja va2s  .c o  m
                            .add(free_bodies.removeIndex(free_bodies.indexOf(to_be_attached.get(i), false)));
                    for (AstroBro b : free_bros) {
                        if (b.getBody().equals(joint_def.bodyB)) {
                            controlled_bros.add(free_bros.removeIndex(free_bros.indexOf(b, false)));
                        }
                    }
                }
            }
        }

        //player.playStickSound();
        to_be_attached.clear();
        to_be_attached_to.clear();
    }

}

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

License:Open Source License

/**
 * As revolute joint.//from www  .  j a va2  s . 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.laex.MyGdxGame.java

License:Open Source License

/**
 * Creates the bodies.//www .j ava 2s .  c om
 */
private void createBodies() {
    // this part of box2d code is taken from Tumbler test from Box2D Source
    {
        BodyDef bodyDef = new BodyDef();
        ground = world.createBody(bodyDef);
    }

    {
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyType.DynamicBody;
        bodyDef.allowSleep = true;
        bodyDef.position.set(0.0f, 0.0f);
        body = world.createBody(bodyDef);

        PolygonShape shape = new PolygonShape();

        shape.setAsBox(0.5f, 10.0f, new Vector2(5.0f, 0.0f), 0.0f);
        body.createFixture(shape, 5.0f);

        shape.setAsBox(0.5f, 10.0f, new Vector2(-5.0f, 0.0f), 0.0f);
        body.createFixture(shape, 5.0f);

        shape.setAsBox(10.0f, 0.5f, new Vector2(0.0f, 5.0f), 0.0f);
        body.createFixture(shape, 5.0f);

        shape.setAsBox(10.0f, 0.5f, new Vector2(0.0f, -5.0f), 0.0f);
        body.createFixture(shape, 5.0f);

        RevoluteJointDef rjd = new RevoluteJointDef();
        rjd.bodyA = ground;
        rjd.bodyB = body;
        rjd.localAnchorA.set(0.0f, 10.0f);
        rjd.localAnchorB.set(0.0f, 0.0f);
        rjd.referenceAngle = 0.0f;
        rjd.motorSpeed = 0.05f * MathUtils.PI;
        rjd.maxMotorTorque = 1e8f;
        rjd.enableMotor = true;
        joint = world.createJoint(rjd);

    }

}