Example usage for com.badlogic.gdx.math Vector2 isZero

List of usage examples for com.badlogic.gdx.math Vector2 isZero

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Vector2 isZero.

Prototype

@Override
    public boolean isZero(final float margin) 

Source Link

Usage

From source file:com.badlogic.gdx.ai.tests.steer.box2d.Box2dSteeringEntity.java

License:Apache License

protected void applySteering(SteeringAcceleration<Vector2> steering, float deltaTime) {
    boolean anyAccelerations = false;

    // Update position and linear velocity.
    if (!steeringOutput.linear.isZero()) {
        Vector2 force = steeringOutput.linear.scl(deltaTime);
        body.applyForceToCenter(force, true);
        anyAccelerations = true;/* w w  w. j  av  a2s.  co m*/
    }

    // Update orientation and angular velocity
    if (isIndependentFacing()) {
        if (steeringOutput.angular != 0) {
            body.applyTorque(steeringOutput.angular * deltaTime, true);
            anyAccelerations = true;
        }
    } else {
        // If we haven't got any velocity, then we can do nothing.
        Vector2 linVel = getLinearVelocity();
        if (!linVel.isZero(MathUtils.FLOAT_ROUNDING_ERROR)) {
            float newOrientation = vectorToAngle(linVel);
            body.setAngularVelocity((newOrientation - getAngularVelocity()) * deltaTime); // this is superfluous if independentFacing is always true
            body.setTransform(body.getPosition(), newOrientation);
        }
    }

    if (anyAccelerations) {
        // body.activate();

        // TODO:
        // Looks like truncating speeds here after applying forces doesn't work as expected.
        // We should likely cap speeds form inside an InternalTickCallback, see
        // http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Simulation_Tick_Callbacks

        // Cap the linear speed
        Vector2 velocity = body.getLinearVelocity();
        float currentSpeedSquare = velocity.len2();
        float maxLinearSpeed = getMaxLinearSpeed();
        if (currentSpeedSquare > maxLinearSpeed * maxLinearSpeed) {
            body.setLinearVelocity(velocity.scl(maxLinearSpeed / (float) Math.sqrt(currentSpeedSquare)));
        }

        // Cap the angular speed
        float maxAngVelocity = getMaxAngularSpeed();
        if (body.getAngularVelocity() > maxAngVelocity) {
            body.setAngularVelocity(maxAngVelocity);
        }
    }
}

From source file:com.mygdx.game.Box2dSteeringEntity.java

License:Apache License

protected void applySteering(SteeringAcceleration<Vector2> steering, float deltaTime) {
    boolean anyAccelerations = false;

    // Update position and linear velocity.
    if (!steeringOutput.linear.isZero()) {
        // this method internally scales the force by deltaTime
        body.applyForceToCenter(steeringOutput.linear, true);
        anyAccelerations = true;/*from w  w w  .  j  a v  a  2 s  . c  o  m*/
    }

    // Update orientation and angular velocity
    if (isIndependentFacing()) {
        if (steeringOutput.angular != 0) {
            // this method internally scales the torque by deltaTime
            body.applyTorque(steeringOutput.angular, true);
            anyAccelerations = true;
        }
    } else {
        // If we haven't got any velocity, then we can do nothing.
        Vector2 linVel = getLinearVelocity();
        if (!linVel.isZero(getZeroLinearSpeedThreshold())) {
            float newOrientation = vectorToAngle(linVel);
            body.setAngularVelocity((newOrientation - getAngularVelocity()) * deltaTime); // this is superfluous if independentFacing is always true
            body.setTransform(body.getPosition(), newOrientation);
        }
    }

    if (anyAccelerations) {
        // body.activate();

        // TODO:
        // Looks like truncating speeds here after applying forces doesn't work as expected.
        // We should likely cap speeds form inside an InternalTickCallback, see
        // http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Simulation_Tick_Callbacks

        // Cap the linear speed
        Vector2 velocity = body.getLinearVelocity();
        float currentSpeedSquare = velocity.len2();
        float maxLinearSpeed = getMaxLinearSpeed();
        if (currentSpeedSquare > maxLinearSpeed * maxLinearSpeed) {
            body.setLinearVelocity(velocity.scl(maxLinearSpeed / (float) Math.sqrt(currentSpeedSquare)));
        }

        // Cap the angular speed
        float maxAngVelocity = getMaxAngularSpeed();
        if (body.getAngularVelocity() > maxAngVelocity) {
            body.setAngularVelocity(maxAngVelocity);
        }
    }
}