Advertisement
dermetfan

Car.java

Aug 21st, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. package net.dermetfan.someLibgdxTests.entities;
  2.  
  3. import com.badlogic.gdx.Input.Keys;
  4. import com.badlogic.gdx.InputAdapter;
  5. import com.badlogic.gdx.math.Vector2;
  6. import com.badlogic.gdx.physics.box2d.Body;
  7. import com.badlogic.gdx.physics.box2d.BodyDef;
  8. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  9. import com.badlogic.gdx.physics.box2d.CircleShape;
  10. import com.badlogic.gdx.physics.box2d.FixtureDef;
  11. import com.badlogic.gdx.physics.box2d.PolygonShape;
  12. import com.badlogic.gdx.physics.box2d.World;
  13. import com.badlogic.gdx.physics.box2d.joints.WheelJoint;
  14. import com.badlogic.gdx.physics.box2d.joints.WheelJointDef;
  15.  
  16. public class Car extends InputAdapter {
  17.  
  18.     private Body chassis, leftWheel, rightWheel;
  19.     private WheelJoint leftAxis, rightAxis;
  20.     private float motorSpeed = 75;
  21.  
  22.     public Car(World world, FixtureDef chassisFixtureDef, FixtureDef wheelFixtureDef, float x, float y, float width, float height) {
  23.         BodyDef bodyDef = new BodyDef();
  24.         bodyDef.type = BodyType.DynamicBody;
  25.         bodyDef.position.set(x, y);
  26.  
  27.         // chassis
  28.         PolygonShape chassisShape = new PolygonShape();
  29.         chassisShape.set(new float[] {-width / 2, -height / 2, width / 2, -height / 2, width / 2 * .4f, height / 2, -width / 2 * .8f, height / 2 * .8f}); // counterclockwise order
  30.  
  31.         chassisFixtureDef.shape = chassisShape;
  32.  
  33.         chassis = world.createBody(bodyDef);
  34.         chassis.createFixture(chassisFixtureDef);
  35.  
  36.         // left wheel
  37.         CircleShape wheelShape = new CircleShape();
  38.         wheelShape.setRadius(height / 3.5f);
  39.  
  40.         wheelFixtureDef.shape = wheelShape;
  41.  
  42.         leftWheel = world.createBody(bodyDef);
  43.         leftWheel.createFixture(wheelFixtureDef);
  44.  
  45.         // right wheel
  46.         rightWheel = world.createBody(bodyDef);
  47.         rightWheel.createFixture(wheelFixtureDef);
  48.  
  49.         // left axis
  50.         WheelJointDef axisDef = new WheelJointDef();
  51.         axisDef.bodyA = chassis;
  52.         axisDef.bodyB = leftWheel;
  53.         axisDef.localAnchorA.set(-width / 2 * .75f + wheelShape.getRadius(), -height / 2 * 1.25f);
  54.         axisDef.frequencyHz = chassisFixtureDef.density;
  55.         axisDef.localAxisA.set(Vector2.Y);
  56.         axisDef.maxMotorTorque = chassisFixtureDef.density * 10;
  57.         leftAxis = (WheelJoint) world.createJoint(axisDef);
  58.  
  59.         // right axis
  60.         axisDef.bodyB = rightWheel;
  61.         axisDef.localAnchorA.x *= -1;
  62.  
  63.         rightAxis = (WheelJoint) world.createJoint(axisDef);
  64.     }
  65.  
  66.     @Override
  67.     public boolean keyDown(int keycode) {
  68.         switch(keycode) {
  69.         case Keys.W:
  70.             leftAxis.enableMotor(true);
  71.             leftAxis.setMotorSpeed(-motorSpeed);
  72.             break;
  73.         case Keys.S:
  74.             leftAxis.enableMotor(true);
  75.             leftAxis.setMotorSpeed(motorSpeed);
  76.         }
  77.         return true;
  78.     }
  79.  
  80.     @Override
  81.     public boolean keyUp(int keycode) {
  82.         switch(keycode) {
  83.         case Keys.W:
  84.         case Keys.S:
  85.             leftAxis.enableMotor(false);
  86.         }
  87.         return true;
  88.     }
  89.  
  90.     public Body getChassis() {
  91.         return chassis;
  92.     }
  93.  
  94.     public Body getLeftWheel() {
  95.         return leftWheel;
  96.     }
  97.  
  98.     public Body getRightWheel() {
  99.         return rightWheel;
  100.     }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement