Example usage for com.badlogic.gdx.ai.steer SteeringAcceleration isZero

List of usage examples for com.badlogic.gdx.ai.steer SteeringAcceleration isZero

Introduction

In this page you can find the example usage for com.badlogic.gdx.ai.steer SteeringAcceleration isZero.

Prototype

public boolean isZero() 

Source Link

Document

Returns true if both linear and angular components of this steering acceleration are zero; false otherwise.

Usage

From source file:com.mygdx.game.steerers.FollowPathSteerer.java

License:Apache License

@Override
public boolean processSteering(SteeringAcceleration<Vector3> steering) {

    // Check if steering target path segment changed.
    LinePathParam pathParam = followPathSB.getPathParam();
    int traversedSegment = pathParam.getSegmentIndex();
    if (traversedSegment > currentSegmentIndex) {
        currentSegmentIndex = traversedSegment;
    }//ww w .j  a  va  2  s . c o  m

    if (prioritySteering.getSelectedBehaviorIndex() == 0) {
        /*
         * Collision avoidance management
         */
        float pr = proximity.getRadius() * 1.5f;
        if (linePath.getEndPoint().dst2(steerableBody.getPosition()) <= pr * pr) {
            // Disable collision avoidance near the end of the path since the obstacle
            // will likely prevent the entity from reaching the target.
            collisionAvoidanceSB.setEnabled(false);
            deadlockDetectionStartTime = Float.POSITIVE_INFINITY;
        } else if (deadlockDetection) {
            // Accumulate collision time during deadlock detection
            collisionDuration += GdxAI.getTimepiece().getDeltaTime();

            if (GdxAI.getTimepiece().getTime() - deadlockDetectionStartTime > DEADLOCK_TIME
                    && collisionDuration > DEADLOCK_TIME * .6f) {
                // Disable collision avoidance since most of the deadlock detection period has been spent on collision avoidance
                collisionAvoidanceSB.setEnabled(false);
            }
        } else {
            // Start deadlock detection
            deadlockDetectionStartTime = GdxAI.getTimepiece().getTime();
            collisionDuration = 0;
            deadlockDetection = true;
        }
        return true;
    }

    /*
     * Path following management
     */
    float dst2FromPathEnd = steerableBody.getPosition().dst2(linePath.getEndPoint());

    // Check to see if the entity has reached the end of the path
    if (steering.isZero()
            && dst2FromPathEnd < followPathSB.getArrivalTolerance() * followPathSB.getArrivalTolerance()) {
        return false;
    }

    // Check if collision avoidance must be re-enabled
    if (deadlockDetection && !collisionAvoidanceSB.isEnabled()
            && GdxAI.getTimepiece().getTime() - deadlockDetectionStartTime > MAX_NO_COLLISION_TIME) {
        collisionAvoidanceSB.setEnabled(true);
        deadlockDetection = false;
    }

    // If linear speed is very low and the entity is colliding something at his feet, like a step of the stairs
    // for instance, we have to increase the acceleration to make him go upstairs. 
    float minVel = .2f;
    if (steerableBody.getLinearVelocity().len2() > minVel * minVel) {
        stationarityRayColor = null;
    } else {
        steerableBody.getGroundPosition(stationarityRayLow.origin).add(0, 0.05f, 0);
        steerableBody.getDirection(stationarityRayLow.direction).scl(1f, 0f, 1f).nor();
        stationarityRayLength = steerableBody.getBoundingRadius() + 0.4f;
        Entity hitEntityLow = GameScreen.screen.engine.rayTest(stationarityRayLow, null, GameEngine.ALL_FLAG,
                GameEngine.PC_FLAG, stationarityRayLength, null);
        if (hitEntityLow instanceof GameObject) {
            stationarityRayColor = Color.RED;
            stationarityRayHigh.set(stationarityRayLow);
            stationarityRayHigh.origin.add(0, .8f, 0);
            Entity hitEntityHigh = GameScreen.screen.engine.rayTest(stationarityRayHigh, null,
                    GameEngine.ALL_FLAG, GameEngine.PC_FLAG, stationarityRayLength, null);
            if (hitEntityHigh == null) {
                // The entity is touching a small obstacle with his feet like a step of the stairs.
                // Increase the acceleration to make him go upstairs.
                steering.linear.scl(8);
            } else if (hitEntityHigh instanceof GameObject) {
                // The entity is touching a higher obstacle like a tree, a column or something.
                // Here we should invent something to circumvent this kind of obstacles :)
                //steering.linear.rotateRad(Constants.V3_UP, Constants.PI0_25);
            }
        } else {
            stationarityRayColor = Color.BLUE;
        }
    }

    return true;
}

From source file:toniarts.openkeeper.world.creature.steering.AbstractCreatureSteeringControl.java

License:Open Source License

protected void applySteering(SteeringAcceleration<Vector2> steering, float tpf) {

    // Update position and linear velocity. Velocity is trimmed to maximum speed
    position.add(linearVelocity.x * tpf, linearVelocity.y * tpf);
    linearVelocity.mulAdd(steering.linear, tpf).limit(getMaxLinearSpeed());

    // We are done
    // TODO: Call function?
    if (steering.isZero()) {
        steeringBehavior = null;//w ww . j  a v a  2 s. co m
    }

    // Update orientation and angular velocity
    if (independentFacing) {
        setOrientation(getOrientation() + (angularVelocity * tpf));
        angularVelocity += steering.angular * tpf;
    } else {
        // If we haven't got any velocity, then we can do nothing.
        if (!linearVelocity.isZero(getZeroLinearSpeedThreshold())) {
            float newOrientation = vectorToAngle(linearVelocity);
            angularVelocity = (newOrientation - getOrientation()) * tpf; // this is superfluous if independentFacing is always true
            setOrientation(newOrientation);
        }
    }
}