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

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

Introduction

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

Prototype

public void applyAngularImpulse(float impulse, boolean wake) 

Source Link

Document

Apply an angular impulse.

Usage

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

License:Open Source License

@Override
public void act(float delta) {
    if (!mActive) {
        return;// ww  w  .  j av 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);
}