@Override protected SteeringAcceleration<T> calculateRealSteering (SteeringAcceleration<T> steering) { // We'll need epsilon squared later. float epsilonSquared = epsilon * epsilon; // Go through the behaviors until one has a large enough acceleration int n = behaviors.size; selectedBehaviorIndex = -1; for (int i = 0; i < n; i++) { selectedBehaviorIndex = i; SteeringBehavior<T> behavior = behaviors.get(i); // Calculate the behavior's steering behavior.calculateSteering(steering); // If we're above the threshold return the current steering if (steering.calculateSquareMagnitude() > epsilonSquared) return steering; } // If we get here, it means that no behavior had a large enough acceleration, // so return the small acceleration from the final behavior or zero if there are // no behaviors in the list. return n > 0 ? steering : steering.setZero(); }
public static SteerLocation getTarget(SteeringBehavior behavior){ SteerLocation targetL = null; if(behavior instanceof Face) targetL = (SteerLocation)((Face) behavior).getTarget(); else if(behavior instanceof Arrive) targetL = (SteerLocation)((Arrive) behavior).getTarget(); else if(behavior instanceof Seek) targetL = (SteerLocation)((Seek) behavior).getTarget(); return targetL; }
public static Entity createSquad (Vector2 position, Faction faction) { Entity entity = builder.createEntity(EntityCategory.SQUAD, position).physicsBody(BodyType.DynamicBody).circleSensor(30.0f) .faction(faction).target().filter(EntityCategory.SQUAD, 0, EntityCategory.SQUAD | EntityCategory.RESOURCE) .steeringBehavior().stateMachine().getWithoutAdding(); SteerableComponent steerable = engine.createComponent(SteerableComponent.class).init( Components.PHYSICS.get(entity).getBody(), 30.0f); SquadComponent squadComp = engine.createComponent(SquadComponent.class).init(steerable); squadComp.targetLocation.getPosition().set(position); entity.add(squadComp); // A good rule of thumb is to make the maximum speed of the formation // around // half that of the members. We also give the anchor point far less // acceleration. steerable.setMaxLinearSpeed(SteerableComponent.MAX_LINEAR_SPEED / 2); steerable.setMaxLinearAcceleration(SteerableComponent.MAX_LINEAR_ACCELERATION / 10); Arrive<Vector2> arriveSB = new Arrive<Vector2>(steerable).setTarget(squadComp.targetLocation).setTimeToTarget(0.001f) .setDecelerationRadius(2f).setArrivalTolerance(0.0001f); SteeringBehavior<Vector2> sb = arriveSB; RadiusProximity<Vector2> proximity = new RadiusProximity<Vector2>(steerable, squadComp.friendlyAgents, 3.0f); Separation<Vector2> separationSB = new Separation<Vector2>(steerable, proximity); BlendedSteering<Vector2> blendedSteering = new BlendedSteering<Vector2>(steerable) // .setLimiter(NullLimiter.NEUTRAL_LIMITER) // .add(separationSB, 10000f) .add(arriveSB, 0.5f); sb = blendedSteering; Components.FSM.get(entity).changeState(SquadComponent.DEFAULT_STATE); Components.STEERING_BEHAVIOR.get(entity).setBehavior(sb); entity.add(steerable); engine.addEntity(entity); return entity; }
/** Removes a steering behavior from the list. * @param behavior the steering behavior to remove */ public void remove (SteeringBehavior<T> behavior) { for (int i = 0; i < list.size; i++) { if(list.get(i).behavior == behavior) { list.removeIndex(i); return; } } }
public void setBehavior(SteeringBehavior<Vector2> behavior) { this.behavior = behavior; }
public SteeringBehavior<Vector2> getBehavior() { return behavior; }
public <T extends SteeringBehavior> SteeringBehavior getBehavior(Class<T> behaviorClass){ return cachedBehaviors.get(behaviorClass); }
public GameEntity cacheBehavior(SteeringBehavior behavior){ if(behavior == null) return this; this.cachedBehaviors.put(behavior.getClass(), behavior); return this; }
public SteeringBehavior<Vector2> getSteeringBehavior() { return steeringBehavior; }
public void setSteeringBehavior(SteeringBehavior<Vector2> steeringBehavior) { this.steeringBehavior = steeringBehavior; }
public BehaviorAndWeight (SteeringBehavior<T> behavior, float weight) { this.behavior = behavior; this.weight = weight; }
public SteeringBehavior<T> getBehavior () { return behavior; }
public void setBehavior (SteeringBehavior<T> behavior) { this.behavior = behavior; }
/** Adds the specified behavior to the priority list. * @param behavior the behavior to add * @return this behavior for chaining. */ public PrioritySteering<T> add (SteeringBehavior<T> behavior) { behaviors.add(behavior); return this; }
@Override public SteeringBehavior<Vector3> getSteeringBehavior() { return prioritySteering; }
@Override protected void processEntity(Entity entity, float deltaTime) { SteeringBehavior<Vector2> behavior = Components.STEERING_BEHAVIOR.get(entity).getBehavior(); SteerableComponent steerable = Components.STEERABLE.get(entity); if(behavior == null) return; if(steerable.getBody() == null) return; //We shouldn't need this behavior.calculateSteering(steeringOutput); boolean anyAccelerations = false; Body body = steerable.getBody(); if (!steeringOutput.linear.isZero()) { Vector2 force = steeringOutput.linear.scl(deltaTime); body.applyForceToCenter(force, true); anyAccelerations = true; } // Update orientation and angular velocity if (steerable.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 = body.getLinearVelocity(); if (!linVel.isZero(steerable.getZeroLinearSpeedThreshold())) { float newOrientation = steerable.vectorToAngle(linVel); body.setAngularVelocity((newOrientation - steerable.getAngularVelocity()) * deltaTime); // this is superfluous if independentFacing is always true body.setTransform(body.getPosition(), newOrientation); } } if (anyAccelerations) { // Cap the linear speed Vector2 velocity = body.getLinearVelocity(); float currentSpeedSquare = velocity.len2(); float maxLinearSpeed = steerable.getMaxLinearSpeed(); if (currentSpeedSquare > maxLinearSpeed * maxLinearSpeed) { body.setLinearVelocity(velocity.scl(maxLinearSpeed / (float)Math.sqrt(currentSpeedSquare))); } // Cap the angular speed float maxAngVelocity = steerable.getMaxAngularSpeed(); if (body.getAngularVelocity() > maxAngVelocity) { body.setAngularVelocity(maxAngVelocity); } } }
public SteeringBehaviorComponent init (SteeringBehavior<Vector2> behavior) { setBehavior(behavior); return this; }
public void setBehavior (SteeringBehavior<Vector2> behavior) { this.behavior = behavior; }
public SteeringBehavior<Vector2> getBehavior () { return behavior; }
public void setSteeringBehavior(SteeringBehavior<Vector2> behavior) { steeringBehavior = behavior; }
public SteeringBehavior<Vector2> getSteeringBehavior () { return steeringBehavior; }
public void setSteeringBehavior (SteeringBehavior<Vector2> steeringBehavior) { this.steeringBehavior = steeringBehavior; }
public SteeringBehavior<Vector3> getSteeringBehavior () { return steeringBehavior; }
public void setSteeringBehavior (SteeringBehavior<Vector3> steeringBehavior) { this.steeringBehavior = steeringBehavior; }
/** Adds a steering behavior and its weight to the list. * @param behavior the steering behavior to add * @param weight the weight of the behavior * @return this behavior for chaining. */ public BlendedSteering<T> add (SteeringBehavior<T> behavior, float weight) { return add(new BehaviorAndWeight<T>(behavior, weight)); }
/** * Returns the steering behavior of this steerer, usually a priority or a blended steering grouping other steering behaviors. */ public abstract SteeringBehavior<Vector3> getSteeringBehavior();