Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package AITest; import com.badlogic.gdx.ai.steer.Steerable; import com.badlogic.gdx.ai.steer.SteeringAcceleration; import com.badlogic.gdx.ai.steer.SteeringBehavior; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Body; /** * * @author Dalliny */ public class Box2DSteeringEntity implements Steerable<Vector2> { Body body; boolean tagged; float boundingRadius; float maxLinearSpeed; float maxLinearAcceleration; float maxAngularSpeed; float maxAngularAcceleration; SteeringBehavior<Vector2> behavior; SteeringAcceleration<Vector2> steeringOutput; public void update(float delta) { if (behavior != null) { behavior.calculateSteering(steeringOutput); applySteering(delta); } else System.out.println("no behavior set!"); } private void applySteering(float delta) { boolean anyAccelerations = false; if (!steeringOutput.isZero()) { Vector2 force = steeringOutput.linear.scl(delta); body.applyForceToCenter(force, true); anyAccelerations = true; } if (steeringOutput.angular != 0) { body.applyTorque(steeringOutput.angular * delta, true); anyAccelerations = true; } // else { // Vector2 linVel = getLinearVelocity(); // if(!linVel.isZero()) // { // float newOrientation = vectorToAngle(linVel); // body.setAngularVelocity(newOrientation - getAngularVelocity() * delta); // body.setTransform(body.getPosition(), newOrientation); // }} if (anyAccelerations) { Vector2 velocity = body.getLinearVelocity(); float currentSpeedSquare = velocity.len2(); if (currentSpeedSquare > maxLinearSpeed * maxLinearSpeed) { body.setLinearVelocity(velocity.scl(maxLinearSpeed / (float) Math.sqrt(currentSpeedSquare))); } if (body.getAngularVelocity() > maxAngularSpeed) { body.setAngularVelocity(maxAngularSpeed); } } } public Box2DSteeringEntity(Body body, float boundingRadius) { this.body = body; this.boundingRadius = boundingRadius; this.maxAngularAcceleration = 5; this.maxAngularSpeed = 5; this.maxLinearSpeed = 5; this.maxLinearAcceleration = 500; this.tagged = false; this.steeringOutput = new SteeringAcceleration<Vector2>(new Vector2()); this.body.setUserData(this); } public Body getBody() { return body; } public void setBehavior(SteeringBehavior<Vector2> behavior) { this.behavior = behavior; } public SteeringBehavior<Vector2> getBehavior() { return behavior; } @Override public Vector2 getPosition() { return body.getPosition(); } @Override public float getOrientation() { return body.getAngle(); } @Override public Vector2 getLinearVelocity() { return body.getLinearVelocity(); } @Override public float getAngularVelocity() { return body.getAngularVelocity(); } @Override public float getBoundingRadius() { return boundingRadius; } @Override public boolean isTagged() { return tagged; } @Override public void setTagged(boolean tagged) { this.tagged = tagged; } @Override public Vector2 newVector() { return new Vector2(); } @Override public float vectorToAngle(Vector2 vector) { return VectorUtils.VectorToAngle(vector); } @Override public Vector2 angleToVector(Vector2 outVector, float angle) { outVector = VectorUtils.AngleToVector(angle); return outVector.cpy(); } @Override public float getMaxLinearSpeed() { return maxLinearSpeed; } @Override public void setMaxLinearSpeed(float maxLinearSpeed) { this.maxLinearSpeed = maxLinearSpeed; } @Override public float getMaxLinearAcceleration() { return maxLinearAcceleration; } @Override public void setMaxLinearAcceleration(float maxLinearAcceleration) { this.maxLinearAcceleration = maxLinearAcceleration; } @Override public float getMaxAngularSpeed() { return maxAngularSpeed; } @Override public void setMaxAngularSpeed(float maxAngularSpeed) { this.maxAngularSpeed = maxAngularSpeed; } @Override public float getMaxAngularAcceleration() { return maxAngularAcceleration; } @Override public void setMaxAngularAcceleration(float maxAngularAcceleration) { this.maxAngularAcceleration = maxAngularAcceleration; } }