Example usage for com.badlogic.gdx.physics.box2d Body getAngularVelocity

List of usage examples for com.badlogic.gdx.physics.box2d Body getAngularVelocity

Introduction

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

Prototype

public float getAngularVelocity() 

Source Link

Document

Get the angular velocity.

Usage

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

License:Open Source License

@Override
public void act(float delta) {
    if (!mActive) {
        return;/*from w ww.  j a v a  2  s.c  o  m*/
    }
    Body body = mVehicle.getBody();

    // Slow down
    body.applyLinearImpulse(body.getLinearVelocity().nor().scl(-body.getMass()), body.getWorldCenter(), true);

    // Spin
    float nextAngle = body.getAngle() + body.getAngularVelocity() * GameWorld.BOX2D_TIME_STEP;
    if (nextAngle > mTargetBodyAngle) {
        stopSpinning();
        return;
    }

    float totalRotation = mTargetBodyAngle - nextAngle;
    float desiredAngularVelocity = totalRotation / GameWorld.BOX2D_TIME_STEP;
    if (desiredAngularVelocity < 0) {
        desiredAngularVelocity = MathUtils.clamp(desiredAngularVelocity, -MAX_ANGULAR_VELOCITY,
                -MIN_ANGULAR_VELOCITY);
    } else {
        desiredAngularVelocity = MathUtils.clamp(desiredAngularVelocity, MIN_ANGULAR_VELOCITY,
                MAX_ANGULAR_VELOCITY);
    }
    float impulse = body.getInertia() * (desiredAngularVelocity - body.getAngularVelocity());
    body.applyAngularImpulse(impulse, true);
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Autopilot.java

License:Apache License

/** @param wake if the body should be woken up if its sleeping
 *  @see #calculateTorque(Vector2, float, float, float, float, float) */
public void rotate(Body body, Vector2 target, float angle, float force, float delta, boolean wake) {
    body.applyTorque(// w  ww .  j  av a 2 s .c  o  m
            calculateTorque(rotateRelative ? target : vec2_0.set(positionAccessor.apply(body)).sub(target),
                    body.getTransform().getRotation() + angle, body.getAngularVelocity(), body.getInertia(),
                    adaptForceToMass ? force * body.getMass() : force, delta),
            wake);
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DUtils.java

License:Apache License

/** @param body the body for which to setup a new {@link BodyDef}
 *  @return a new {@link BodyDef} instance that can be used to clone the given body */
public static BodyDef createDef(Body body) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.active = body.isActive();//from   ww  w  .j av  a 2 s . c o m
    bodyDef.allowSleep = body.isSleepingAllowed();
    bodyDef.angle = body.getAngle();
    bodyDef.angularDamping = body.getAngularDamping();
    bodyDef.angularVelocity = body.getAngularVelocity();
    bodyDef.awake = body.isAwake();
    bodyDef.bullet = body.isBullet();
    bodyDef.fixedRotation = body.isFixedRotation();
    bodyDef.gravityScale = body.getGravityScale();
    bodyDef.linearDamping = body.getLinearDamping();
    bodyDef.linearVelocity.set(body.getLinearVelocity());
    bodyDef.position.set(body.getPosition());
    bodyDef.type = body.getType();
    return bodyDef;
}

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

License:Apache License

private boolean applyInput(PtmGame cmp, float shipAngle, Pilot provider, Body body, Vector2 spd,
        boolean controlsEnabled, float mass) {
    boolean spdOk = PtmMath.canAccelerate(shipAngle, spd);
    boolean working = controlsEnabled && provider.isUp() && spdOk;

    Engine e = myItem;/*  ww  w . ja v  a 2 s . c om*/
    if (working) {
        Vector2 v = PtmMath.fromAl(shipAngle, mass * e.getAcc());
        body.applyForceToCenter(v, true);
        PtmMath.free(v);
    }

    float ts = cmp.getTimeStep();
    float rotSpd = body.getAngularVelocity() * PtmMath.radDeg;
    float desiredRotSpd = 0;
    float rotAcc = e.getRotAcc();
    boolean l = controlsEnabled && provider.isLeft();
    boolean r = controlsEnabled && provider.isRight();
    float absRotSpd = PtmMath.abs(rotSpd);
    if (absRotSpd < e.getMaxRotSpd() && l != r) {
        desiredRotSpd = PtmMath.toInt(r) * e.getMaxRotSpd();
        if (absRotSpd < MAX_RECOVER_ROT_SPD) {
            if (myRecoverAwait > 0) {
                myRecoverAwait -= ts;
            }
            if (myRecoverAwait <= 0) {
                rotAcc *= RECOVER_MUL;
            }
        }
    } else {
        myRecoverAwait = RECOVER_AWAIT;
    }
    body.setAngularVelocity(PtmMath.degRad * PtmMath.approach(rotSpd, desiredRotSpd, rotAcc * ts));
    return working;
}

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

License:Open Source License

/**
 * Change the size of an actor, and/or change its position
 *
 * @param x      The new X coordinate of its bottom left corner
 * @param y      The new Y coordinate of its bototm left corner
 * @param width  The new width of the actor
 * @param height The new height of the actor
 *///from   www  .  j  av a 2s. c  o m
public void resize(float x, float y, float width, float height) {
    // To scale a polygon, we'll need a scaling factor, so we can
    // manually scale each point
    float xscale = height / mSize.y;
    float yscale = width / mSize.x;
    // set new height and width
    mSize.x = width;
    mSize.y = height;
    // read old body information
    Body oldBody = mBody;
    // make a new body
    if (mIsCircleBody) {
        Fixture oldFix = oldBody.getFixtureList().get(0);
        setCirclePhysics(oldFix.getDensity(), oldFix.getRestitution(), oldFix.getFriction(), oldBody.getType(),
                oldBody.isBullet(), x, y, (width > height) ? width / 2 : height / 2);
    } else if (mIsBoxBody) {
        Fixture oldFix = oldBody.getFixtureList().get(0);
        setBoxPhysics(oldFix.getDensity(), oldFix.getRestitution(), oldFix.getFriction(), oldBody.getType(),
                oldBody.isBullet(), x, y);
    } else if (mIsPolygonBody) {
        Fixture oldFix = oldBody.getFixtureList().get(0);
        // we need to manually scale all the vertices
        PolygonShape ps = (PolygonShape) oldFix.getShape();
        float[] verts = new float[ps.getVertexCount() * 2];
        for (int i = 0; i < ps.getVertexCount(); ++i) {
            ps.getVertex(i, mTmpVert);
            verts[2 * i] = mTmpVert.x * xscale;
            verts[2 * i + 1] = mTmpVert.y * yscale;
        }
        setPolygonPhysics(oldFix.getDensity(), oldFix.getRestitution(), oldFix.getFriction(), oldBody.getType(),
                oldBody.isBullet(), x, y, verts);
    }
    // clone forces
    mBody.setAngularVelocity(oldBody.getAngularVelocity());
    mBody.setTransform(mBody.getPosition(), oldBody.getAngle());
    mBody.setGravityScale(oldBody.getGravityScale());
    mBody.setLinearDamping(oldBody.getLinearDamping());
    mBody.setLinearVelocity(oldBody.getLinearVelocity());
    // disable the old body
    oldBody.setActive(false);
}

From source file:net.dermetfan.utils.libgdx.box2d.Autopilot.java

License:Apache License

/** @param wake if the body should be woken up if its sleeping
 *  @see #calculateTorque(Vector2, float, float, float, float, float) */
public void rotate(Body body, Vector2 target, float angle, float force, float delta, boolean wake) {
    body.applyTorque(/* ww  w .  ja  v  a2s . com*/
            calculateTorque(rotateRelative ? target : vec2_0.set(positionAccessor.access(body)).sub(target),
                    body.getTransform().getRotation() + angle, body.getAngularVelocity(), body.getInertia(),
                    adaptForceToMass ? force * body.getMass() : force, delta),
            wake);
}

From source file:net.dermetfan.utils.libgdx.box2d.Box2DUtils.java

License:Apache License

/** creates a deep copy of a {@link Body}
 *  @param body the {@link Body} to copy
 *  @param shapes if the {@link Shape Shapes} of the {@link Fixture Fixures} of the given {@code body} should be {@link #copy(Shape) copied} as well
 *  @return a deep copy of the given {@code body} */
public static Body copy(Body body, boolean shapes) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.active = body.isActive();/*from  ww  w .  ja  va2s .  c  o m*/
    bodyDef.allowSleep = body.isSleepingAllowed();
    bodyDef.angle = body.getAngle();
    bodyDef.angularDamping = body.getAngularDamping();
    bodyDef.angularVelocity = body.getAngularVelocity();
    bodyDef.awake = body.isAwake();
    bodyDef.bullet = body.isBullet();
    bodyDef.fixedRotation = body.isFixedRotation();
    bodyDef.gravityScale = body.getGravityScale();
    bodyDef.linearDamping = body.getLinearDamping();
    bodyDef.linearVelocity.set(body.getLinearVelocity());
    bodyDef.position.set(body.getPosition());
    bodyDef.type = body.getType();
    Body copy = body.getWorld().createBody(bodyDef);
    copy.setUserData(body.getUserData());
    for (Fixture fixture : body.getFixtureList())
        copy(fixture, copy, shapes);
    return copy;
}

From source file:org.destinationsol.game.ship.ShipEngine.java

License:Apache License

private boolean applyInput(SolGame cmp, float shipAngle, Pilot provider, Body body, Vector2 spd,
        boolean controlsEnabled, float mass) {
    boolean spdOk = SolMath.canAccelerate(shipAngle, spd);
    boolean working = controlsEnabled && provider.isUp() && spdOk;

    EngineItem e = myItem;//  w ww  .  j  av  a 2  s. c  o  m
    if (working) {
        Vector2 v = SolMath.fromAl(shipAngle, mass * e.getAcc());
        body.applyForceToCenter(v, true);
        SolMath.free(v);
    }

    float ts = cmp.getTimeStep();
    float rotSpd = body.getAngularVelocity() * SolMath.radDeg;
    float desiredRotSpd = 0;
    float rotAcc = e.getRotAcc();
    boolean l = controlsEnabled && provider.isLeft();
    boolean r = controlsEnabled && provider.isRight();
    float absRotSpd = SolMath.abs(rotSpd);
    if (absRotSpd < e.getMaxRotSpd() && l != r) {
        desiredRotSpd = SolMath.toInt(r) * e.getMaxRotSpd();
        if (absRotSpd < MAX_RECOVER_ROT_SPD) {
            if (myRecoverAwait > 0)
                myRecoverAwait -= ts;
            if (myRecoverAwait <= 0)
                rotAcc *= RECOVER_MUL;
        }
    } else {
        myRecoverAwait = RECOVER_AWAIT;
    }
    body.setAngularVelocity(SolMath.degRad * SolMath.approach(rotSpd, desiredRotSpd, rotAcc * ts));
    return working;
}