SHARE
TWEET
Untitled
a guest
Nov 8th, 2016
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- private void applySteering (float delta) {
- float newOrientation = 0;
- boolean anyAccelerations = false;
- // Update position and linear velocity. Velocity is trimmed to maximum speed
- if(!steeringOutput.linear.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()){
- newOrientation = (float) (vectorToAngle(linVel) + (Math.PI / 2));
- 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);
- }
- }
- if(!isPathfinding()){ //to prevent pushing ai into walls
- if(steeringOutput.linear.x == 0 && steeringOutput.linear.y == 0 && getTarget() == null)
- {
- if(Collision.collidesLeft(positionMap.get(entity), collisionMap.get(entity), velocityMap.get(entity),entity))
- {
- bodyMap.get(entity).body.setType(BodyDef.BodyType.KinematicBody);
- bodyMap.get(entity).body.setLinearVelocity(1, 0);
- }
- else if(Collision.collidesRight(positionMap.get(entity), collisionMap.get(entity), velocityMap.get(entity),entity))
- {
- bodyMap.get(entity).body.setType(BodyDef.BodyType.KinematicBody);
- bodyMap.get(entity).body.setLinearVelocity(-1, 0);
- }
- else if(Collision.collidesBottom(positionMap.get(entity), collisionMap.get(entity), velocityMap.get(entity),entity))
- {
- bodyMap.get(entity).body.setType(BodyDef.BodyType.KinematicBody);
- bodyMap.get(entity).body.setLinearVelocity(0, 1);
- }
- else if(Collision.collidesTop(positionMap.get(entity), collisionMap.get(entity), velocityMap.get(entity),entity))
- {
- bodyMap.get(entity).body.setType(BodyDef.BodyType.KinematicBody);
- bodyMap.get(entity).body.setLinearVelocity(0, -1);
- }
- else
- {
- bodyMap.get(entity).body.setType(BodyDef.BodyType.DynamicBody);
- bodyMap.get(entity).body.setLinearVelocity(0, 0);
- }
- }
- else
- {
- if(steeringOutput.linear.x < 0)
- {
- if(Collision.collidesLeft(positionMap.get(entity), collisionMap.get(entity), velocityMap.get(entity),entity))
- {
- bodyMap.get(entity).body.setLinearVelocity(0, 0);
- bodyMap.get(entity).body.applyForceToCenter(-steeringOutput.linear.x, 0, true);
- }
- }
- else if(steeringOutput.linear.x > 0)
- {
- if(Collision.collidesRight(positionMap.get(entity), collisionMap.get(entity), velocityMap.get(entity),entity))
- {
- bodyMap.get(entity).body.setLinearVelocity(0, 0);
- bodyMap.get(entity).body.applyForceToCenter(-steeringOutput.linear.x, 0, true);
- }
- }
- if(steeringOutput.linear.y < 0)
- {
- if(Collision.collidesBottom(positionMap.get(entity), collisionMap.get(entity), velocityMap.get(entity),entity))
- {
- bodyMap.get(entity).body.setLinearVelocity(0, 0);
- bodyMap.get(entity).body.applyForceToCenter(0, -steeringOutput.linear.y, true);
- }
- }
- else if(steeringOutput.linear.y > 0)
- {
- if(Collision.collidesTop(positionMap.get(entity), collisionMap.get(entity), velocityMap.get(entity),entity))
- {
- bodyMap.get(entity).body.setLinearVelocity(0, 0);
- bodyMap.get(entity).body.applyForceToCenter(0, -steeringOutput.linear.y, true);
- }
- }
- }
- }
- }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

