AtkinsSJ

Untitled

May 11th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.08 KB | None | 0 0
  1. package uk.co.samatkins.racing;
  2.  
  3. import uk.co.samatkins.Entity;
  4.  
  5. import com.badlogic.gdx.Gdx;
  6. import com.badlogic.gdx.graphics.Color;
  7. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  8. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  9. import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
  10. import com.badlogic.gdx.math.MathUtils;
  11. import com.badlogic.gdx.math.Vector2;
  12. import com.badlogic.gdx.math.Vector3;
  13. import com.badlogic.gdx.physics.box2d.Body;
  14. import com.badlogic.gdx.physics.box2d.BodyDef;
  15. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  16. import com.badlogic.gdx.physics.box2d.FixtureDef;
  17. import com.badlogic.gdx.physics.box2d.PolygonShape;
  18. import com.badlogic.gdx.physics.box2d.World;
  19. import com.badlogic.gdx.physics.box2d.joints.RevoluteJoint;
  20. import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef;
  21.  
  22. public class Car extends Entity {
  23.    
  24.     private Vector3 startPosition; // x and y = position, z = rotation
  25.    
  26.     private Body chassis;
  27.     public float acceleration = 800;
  28.     public float braking = 10;
  29.     public float turning = 20;
  30.     public float baseDampening = 10;
  31.    
  32.     private Body[] wheels = new Body[4];
  33.     private Vector2 wheelSize = new Vector2(0.4f, 0.8f);
  34.    
  35.     private int checkpointId; // The furthest checkpoint reached
  36.    
  37.     private ShapeRenderer shapeRenderer;
  38.     private Color chassisColor = Color.RED;
  39.     private Color wheelColor = Color.BLACK;
  40.    
  41.     public Car(float x, float y, float angle) {
  42.         this.setPosition(x, y);
  43.         this.setRotation(angle);
  44.         this.setSize(1.5f, 4);
  45.        
  46.         this.startPosition = new Vector3(x, y, angle);
  47.        
  48.         this.shapeRenderer = new ShapeRenderer();
  49.        
  50.         this.checkpointId = 0;
  51.     }
  52.    
  53.     public void setColor(Color chassisColor) {
  54.         this.chassisColor = chassisColor;
  55.     }
  56.    
  57.     public void restart() {
  58.         // Jump to start position
  59.         this.setPosition(this.startPosition.x, this.startPosition.y);
  60.         this.setRotation(this.startPosition.z);
  61.        
  62.         // Destroy physics bodies
  63.         World world = this.chassis.getWorld();
  64.         world.destroyBody(this.chassis);
  65.         for (Body wheel: this.wheels) {
  66.             world.destroyBody(wheel);
  67.         }
  68.        
  69.         // Create new physics bodies
  70.         this.assignPhysicsBody(world);
  71.     }
  72.  
  73.     public void assignPhysicsBody(World world) {
  74.        
  75.         //// BUILD CHASSIS
  76.        
  77.         // Define body
  78.         BodyDef bodyDef = new BodyDef();
  79.         bodyDef.type = BodyType.DynamicBody;
  80.         bodyDef.angularDamping = 1f;
  81.         bodyDef.linearDamping = 0;// 0.8f;
  82.        
  83.         // Create body
  84.         this.chassis = world.createBody(bodyDef);
  85.         this.chassis.setUserData(this);
  86.        
  87.         // Define shapes
  88.         PolygonShape box = new PolygonShape();
  89.         box.setAsBox(getWidth() / 2, getHeight() / 2);
  90.        
  91.         // Define fixture
  92.         FixtureDef fixtureDef = new FixtureDef();
  93.         fixtureDef.shape = box;
  94.         fixtureDef.density = 16f;
  95.         fixtureDef.friction = 0.1f;
  96.        
  97.         // Create fixture
  98.         this.chassis.createFixture(fixtureDef);
  99.        
  100.         //// WHEELS
  101.         BodyDef wheelBodyDef = new BodyDef();
  102.         wheelBodyDef.type = BodyType.DynamicBody;
  103.         wheelBodyDef.angularDamping = 10f;
  104.         wheelBodyDef.linearDamping = 10f;
  105.        
  106.         PolygonShape wheelShape = new PolygonShape();
  107.         wheelShape.setAsBox(wheelSize.x, wheelSize.y);
  108.        
  109.         FixtureDef wheelDef = new FixtureDef();
  110.         wheelDef.shape = wheelShape;
  111.         wheelDef.isSensor = true;
  112.         wheelDef.density = 1f;
  113.        
  114.         RevoluteJointDef wheelJointDef = new RevoluteJointDef();
  115.         wheelJointDef.collideConnected = false;
  116.         wheelJointDef.bodyA = this.chassis;
  117.        
  118.         Vector2[] wheelPositions = new Vector2[] {
  119.             new Vector2(0.6f, 1),
  120.             new Vector2(-0.6f, 1),
  121.             new Vector2(0.6f, -1),
  122.             new Vector2(-0.6f, -1)
  123.         };
  124.        
  125.         // Create the wheels
  126.         for (int i=0; i<this.wheels.length; i++) {
  127.             this.wheels[i] = world.createBody(wheelBodyDef);
  128.             this.wheels[i].createFixture(wheelDef);
  129.             this.wheels[i].setTransform(wheelPositions[i], 0);
  130.            
  131.             wheelJointDef.bodyB = this.wheels[i];
  132.             wheelJointDef.localAnchorA.set(wheelPositions[i].x, wheelPositions[i].y);
  133.             wheelJointDef.localAnchorB.set(0,0);
  134.             RevoluteJoint j = (RevoluteJoint) world.createJoint(wheelJointDef);
  135.             j.enableLimit(false);
  136.         }
  137.        
  138.         // Move everything
  139.         float angle = this.getRotation() * MathUtils.degRad;
  140.         Vector2 wheelPos;
  141.         this.chassis.setTransform(this.getX(), this.getY(), angle);
  142.         for (int i=0; i<this.wheels.length; i++) {
  143.             wheelPos = this.chassis.getWorldPoint(wheelPositions[i]);
  144.             this.wheels[i].setTransform(wheelPos.x, wheelPos.y, angle);
  145.         }
  146.        
  147.         // Clean-up
  148.         box.dispose();
  149.         wheelShape.dispose();
  150.     }
  151.    
  152.     @Override
  153.     public void act(float delta) {
  154.         super.act(delta);
  155.        
  156.         this.setPosition(this.chassis.getPosition());
  157.         this.setRotation(this.chassis.getAngle() * MathUtils.radiansToDegrees);
  158.        
  159.         this.wheels[2].setTransform(this.wheels[2].getPosition(), this.chassis.getAngle());
  160.         this.wheels[3].setTransform(this.wheels[3].getPosition(), this.chassis.getAngle());
  161.     }
  162.    
  163.     /**
  164.      * Accelerate or decelerate. Currently braking and going backwards are the same thing.
  165.      * @param amount - How much to accelerate/decelerate, from -1 for full reverse, to 1 for full forward.
  166.      */
  167.     public void accelerate(float amount) {
  168.         Vector2 force = this.wheels[0].getWorldVector(new Vector2(0, this.acceleration * amount));
  169.         this.wheels[0].applyForceToCenter(force);
  170.         this.wheels[1].applyForceToCenter(force);
  171.     }
  172.    
  173.     public void brake(float amount) {
  174.         for (Body wheel: this.wheels) {
  175.             wheel.setLinearDamping((amount * braking) + baseDampening);
  176.         }
  177.     }
  178.    
  179.     /**
  180.      * Steer the car
  181.      * @param rotation - How far the steering wheel is turned left or right. -1 = full left, 1 full right
  182.      */
  183.     public void steer(float rotation) {
  184.         float rot = this.chassis.getAngle() + (rotation * -1 * this.turning * MathUtils.degRad);
  185.         this.wheels[0].setTransform(this.wheels[0].getPosition(), rot);
  186.         this.wheels[1].setTransform(this.wheels[1].getPosition(), rot);
  187.     }
  188.    
  189.     public float getSpeed() {
  190.        
  191.         float speed = this.chassis.getLinearVelocity().len() * 0.1f;
  192.        
  193. //      if (this.body.getLinearVelocity().angle() == this.body.getAngle() * MathUtils.radDeg) {
  194. //         
  195. //      }
  196.        
  197.         // DANGER!!!! I think the angle will change depending on the track...
  198.        
  199.         return speed;// * this.getMovementDirection();
  200.     }
  201.    
  202.     /**
  203.      *
  204.      * @return 1 = forwards, -1 = backwards
  205.      */
  206.     public int getMovementDirection() {
  207.         float velDeg = this.chassis.getLinearVelocity().angle();
  208.         float facingDeg = ((this.chassis.getAngle() * MathUtils.radDeg) + 90) % 360;
  209.        
  210.         if (facingDeg - 90 < velDeg && velDeg < facingDeg + 90) {
  211.             return 1;
  212.         } else {
  213.             return -1;
  214.         }
  215.     }
  216.  
  217.     @Override
  218.     public void draw(SpriteBatch batch, float parentAlpha) {
  219.         batch.end();
  220.        
  221.         this.shapeRenderer.setProjectionMatrix(world.getCamera().combined);
  222.         this.shapeRenderer.begin(ShapeType.FilledRectangle);
  223.        
  224.         // Chassis
  225.         this.shapeRenderer.setColor(this.chassisColor);
  226.         this.shapeRenderer.identity();
  227.         this.shapeRenderer.translate(getX() * this.getScaleX(), getY() * this.getScaleY(), 0);
  228.         this.shapeRenderer.rotate(0, 0, 1, this.getRotation());
  229.         this.shapeRenderer.filledRect(-getWidth()/2 * this.getScaleX(), -getHeight()/2 * this.getScaleY(),
  230.                 getWidth() * this.getScaleX(), getHeight() * this.getScaleY());
  231.        
  232.         // Wheels
  233.         this.shapeRenderer.setColor(this.wheelColor);
  234.         Vector2 pos;
  235.         for (Body wheel : this.wheels) {
  236.             this.shapeRenderer.identity();
  237.             pos = wheel.getPosition();
  238.             this.shapeRenderer.translate(pos.x * this.getScaleX(), pos.y * this.getScaleY(), 0);
  239.             this.shapeRenderer.rotate(0, 0, 1, wheel.getAngle() * MathUtils.radDeg);
  240.             this.shapeRenderer.filledRect(-wheelSize.x/2 * this.getScaleX(), -wheelSize.y/2 * this.getScaleY(),
  241.                     wheelSize.x * this.getScaleX(), wheelSize.y * this.getScaleY());
  242.         }
  243.        
  244.         this.shapeRenderer.end();
  245.        
  246.         batch.begin();
  247.     }
  248.  
  249.     public void reachedCheckpoint(int id) {
  250.         if (this.checkpointId == id-1) {
  251.             Gdx.app.debug("Checkpoint", "Reached checkpoint #" + id);
  252.             this.checkpointId = id;
  253.         } else if (id == 0) {
  254.             // See if we just completed a lap
  255.             int checkpointCount = ((PlayScene)this.getStage()).getTrack().getNumberOfCheckpoints();
  256.             if (this.checkpointId == checkpointCount-1) {
  257.                 ((PlayScene)this.world).lapComplete();
  258.                 this.checkpointId = 0;
  259.             }
  260.         }
  261.        
  262.     }
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment