Example usage for com.badlogic.gdx.ai.steer Steerable getPosition

List of usage examples for com.badlogic.gdx.ai.steer Steerable getPosition

Introduction

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

Prototype

public T getPosition();

Source Link

Document

Returns the vector indicating the position of this Steerable.

Usage

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

License:Apache License

@Override
protected boolean accept(Steerable<Vector2> steerable) {
    toSteerable.set(steerable.getPosition()).sub(owner.getPosition());

    // The bounding radius of the current body is taken into account
    // by adding it to the radius proximity
    float range = detectionRadius + steerable.getBoundingRadius();

    float toSteerableLen2 = toSteerable.len2();

    // Make sure the steerable is within the range.
    // Notice we're working in distance-squared space to avoid square root.
    if (toSteerableLen2 < range * range) {

        // Accept the steerable if it is within the field of view of the owner.
        return (ownerOrientation.dot(toSteerable) > coneThreshold);
    }//from   w ww  . j  av  a  2  s .c  o  m

    // Reject the steerable
    return false;
}

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

License:Apache License

@Override
protected boolean accept(Steerable<Vector2> steerable) {
    // The bounding radius of the current body is taken into account
    // by adding it to the radius proximity
    float range = detectionRadius + steerable.getBoundingRadius();

    // Make sure the current body is within the range.
    // Notice we're working in distance-squared space to avoid square root.
    float distanceSquare = steerable.getPosition().dst2(owner.getPosition());

    return distanceSquare <= range * range;
}

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

License:Apache License

@Override
public boolean reportNeighbor(Steerable<T> neighbor) {
    // Calculate the time to collision
    relativePosition.set(neighbor.getPosition()).sub(owner.getPosition());
    relativeVelocity.set(neighbor.getLinearVelocity()).sub(owner.getLinearVelocity());
    float relativeSpeed2 = relativeVelocity.len2();
    float timeToCollision = -relativePosition.dot(relativeVelocity) / relativeSpeed2;

    if (Float.isNaN(timeToCollision)) {
        timeToCollision = 0.0f;/* w  w  w . j a  v  a  2 s  .co m*/
    }

    // If timeToCollision is negative, i.e. the owner is already moving away from the the neighbor,
    // or it's not the most imminent collision then no action needs to be taken.
    if (timeToCollision <= 0 || timeToCollision >= shortestTime)
        return false;

    // Check if it is going to be a collision at all
    float distance = relativePosition.len();
    float minSeparation = distance - (float) Math.sqrt(relativeSpeed2) * timeToCollision /* shortestTime */;
    if (minSeparation > owner.getBoundingRadius() + neighbor.getBoundingRadius())
        return false;

    // Store most imminent collision data
    shortestTime = timeToCollision;
    firstNeighbor = neighbor;
    firstMinSeparation = minSeparation;
    firstDistance = distance;
    firstRelativePosition.set(relativePosition);
    firstRelativeVelocity.set(relativeVelocity);

    return true;
}

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

License:Apache License

@Override
public boolean reportNeighbor(Steerable<T> neighbor) {

    toAgent.set(owner.getPosition()).sub(neighbor.getPosition());
    float distanceSqr = toAgent.len2();
    float maxAcceleration = getActualLimiter().getMaxLinearAcceleration();

    // Calculate the strength of repulsion through inverse square law decay
    float strength = getDecayCoefficient() / distanceSqr;
    if (strength > maxAcceleration)
        strength = maxAcceleration;/*from www .  j  a  va  2s .  c o m*/

    // Add the acceleration
    // Optimized code for linear.mulAdd(toAgent.nor(), strength);
    linear.mulAdd(toAgent.nor(), strength / Gdx.graphics.getDeltaTime());

    return true;
}

From source file:com.starsailor.util.box2d.Box2dRadiusProximity.java

License:Apache License

@Override
protected boolean accept(Steerable<Vector2> steerable) {
    // The bounding radius of the current body is taken into account
    // by adding it to the radius proximity
    float range = detectionRadius + steerable.getBoundingRadius();

    // Make sure the current body is within the range.
    // Notice we're working in distance-squared space to avoid square root.
    float distanceSquare = steerable.getPosition().dst2(owner.getPosition());

    Entity entity = (Entity) ((SteerableComponent) steerable).getBody().getUserData();
    if (entity instanceof Bullet) {
        return false;
    }// w w  w.  j a v  a2s  .co m

    return distanceSquare <= range * range;
}