AtkinsSJ

Untitled

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