Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package uk.co.samatkins.racing;
- import uk.co.samatkins.Entity;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
- import com.badlogic.gdx.math.MathUtils;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.physics.box2d.Body;
- import com.badlogic.gdx.physics.box2d.BodyDef;
- import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
- import com.badlogic.gdx.physics.box2d.FixtureDef;
- import com.badlogic.gdx.physics.box2d.PolygonShape;
- import com.badlogic.gdx.physics.box2d.World;
- import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef;
- public class Car extends Entity {
- private Body chassis;
- public float acceleration = 1000;
- public float braking = 1;
- public float turning = 50;
- private Body[] wheels = new Body[2];
- private Vector2 wheelSize = new Vector2(4, 8);
- private int checkpointId; // The furthest checkpoint reached
- private ShapeRenderer shapeRenderer;
- public Car(float x, float y, float angle) {
- this.setPosition(x, y);
- this.setRotation(angle);
- this.setSize(10.0f, 20.0f);
- this.shapeRenderer = new ShapeRenderer();
- this.checkpointId = 0;
- }
- public void assignPhysicsBody(World world) {
- //// BUILD CHASSIS
- // Define body
- BodyDef bodyDef = new BodyDef();
- bodyDef.type = BodyType.DynamicBody;
- bodyDef.angularDamping = 1.0f;
- bodyDef.linearDamping = 0f;
- // Create body
- this.chassis = world.createBody(bodyDef);
- this.chassis.setUserData(this);
- this.chassis.setActive(false);
- // Define shapes
- PolygonShape box = new PolygonShape();
- box.setAsBox(getWidth() / 2, getHeight() / 2);
- // Define fixture
- FixtureDef fixtureDef = new FixtureDef();
- fixtureDef.shape = box;
- fixtureDef.density = 0.01f;
- fixtureDef.friction = 0.1f;
- // Create fixture
- this.chassis.createFixture(fixtureDef);
- //// WHEELS
- BodyDef wheelBodyDef = new BodyDef();
- wheelBodyDef.angularDamping = 0;
- wheelBodyDef.linearDamping = 0;
- PolygonShape wheelShape = new PolygonShape();
- wheelShape.setAsBox(wheelSize.x, wheelSize.y);
- FixtureDef wheelDef = new FixtureDef();
- wheelDef.shape = wheelShape;
- wheelDef.isSensor = true;
- wheelDef.density = 0.01f;
- RevoluteJointDef wheelJointDef = new RevoluteJointDef();
- wheelJointDef.collideConnected = false;
- wheelJointDef.bodyA = this.chassis;
- Vector2[] wheelPositions = new Vector2[] {
- new Vector2(5, 10),
- new Vector2(-5, 10)/*,
- {2, 1},
- {2, 2}*/
- };
- // Create the wheels
- for (int i=0; i<this.wheels.length; i++) {
- this.wheels[i] = world.createBody(wheelBodyDef);
- this.wheels[i].setActive(false);
- this.wheels[i].createFixture(wheelDef);
- this.wheels[i].setTransform(wheelPositions[i], 0);
- wheelJointDef.bodyB = this.wheels[i];
- wheelJointDef.localAnchorA.set(wheelPositions[i].x, wheelPositions[i].y);
- wheelJointDef.localAnchorB.set(0,0);
- world.createJoint(wheelJointDef);
- }
- // Move everything
- this.chassis.setTransform(this.getX(), this.getY(), this.getRotation() * MathUtils.degRad);
- //this.wheels[0].setTransform(this.getX()+5, this.getY()-10, this.getRotation() * MathUtils.degRad);
- //this.wheels[1].setTransform(this.getX()-5, this.getY()-10, this.getRotation() * MathUtils.degRad);
- // Activate bodies
- this.chassis.setActive(true);
- for (Body wheel: this.wheels) {
- wheel.setActive(true);
- }
- // Clean-up
- box.dispose();
- wheelShape.dispose();
- }
- @Override
- public void act(float delta) {
- super.act(delta);
- this.setPosition(this.chassis.getPosition());
- this.setRotation(this.chassis.getAngle() * MathUtils.radiansToDegrees);
- Gdx.app.debug("CAR", "Speed: " + this.getSpeed()
- + " Angle: " + this.getRotation());
- // Gdx.app.debug("CAR", "Position: " + this.getX() + ", " + this.getY() + " Angle: " + this.getRotation());
- // Gdx.app.debug("CAR", "Velocity: " + this.body.getLinearVelocity());
- }
- /**
- * Accelerate or decelerate. Currently braking and going backwards are the same thing.
- * @param amount - How much to accelerate/decelerate, from -1 for full reverse, to 1 for full forward.
- */
- public void accelerate(float amount) {
- this.chassis.applyForceToCenter(this.chassis.getWorldVector(new Vector2(0, this.acceleration * amount)));
- // this.wheels[0].applyForceToCenter(this.wheels[0].getWorldVector(new Vector2(0, this.acceleration * amount)));
- }
- public void brake(float amount) {
- this.chassis.setLinearDamping(amount * braking);
- }
- /**
- * Steer the car
- * @param rotation - How far the steering wheel is turned left or right. -1 = full left, 1 full right
- * @param delta - delta time
- */
- public void steer(float rotation, float delta) {
- // this.chassis.setTransform( this.chassis.getPosition(),
- // this.chassis.getAngle() - (rotation * delta * this.getSpeed() * this.turning * MathUtils.degRad));
- this.wheels[0].setTransform(this.wheels[0].getPosition(), rotation * this.turning * MathUtils.degRad);
- }
- public float getSpeed() {
- float speed = this.chassis.getLinearVelocity().len() * 0.01f;
- // if (this.body.getLinearVelocity().angle() == this.body.getAngle() * MathUtils.radDeg) {
- //
- // }
- // DANGER!!!! I think the angle will change depending on the track...
- return speed * this.getMovementDirection();
- }
- /**
- *
- * @return 1 = forwards, -1 = backwards
- */
- public int getMovementDirection() {
- float velDeg = this.chassis.getLinearVelocity().angle();
- float facingDeg = ((this.chassis.getAngle() * MathUtils.radDeg) + 90) % 360;
- if (facingDeg - 90 < velDeg && velDeg < facingDeg + 90) {
- return 1;
- } else {
- return -1;
- }
- }
- @Override
- public void draw(SpriteBatch batch, float parentAlpha) {
- batch.end();
- this.shapeRenderer.setProjectionMatrix(world.getCamera().combined);
- this.shapeRenderer.identity();
- this.shapeRenderer.begin(ShapeType.FilledRectangle);
- this.shapeRenderer.translate(getX(), getY(), 0);
- this.shapeRenderer.rotate(0, 0, 1, this.getRotation());
- // Chassis
- this.shapeRenderer.setColor(1, 0, 0, 1);
- this.shapeRenderer.filledRect(-getWidth()/2, -getHeight()/2, getWidth(), getHeight());
- // Wheels
- this.shapeRenderer.setColor(1, 1, 1, 1);
- Vector2 pos;
- for (Body wheel : this.wheels) {
- pos = wheel.getPosition();
- this.shapeRenderer.translate(pos.x, pos.y, 0);
- this.shapeRenderer.rotate(0, 0, 1, wheel.getAngle() * MathUtils.radDeg);
- this.shapeRenderer.filledRect(-wheelSize.x/2, -wheelSize.y/2, wheelSize.x, wheelSize.y);
- // Revert the wheel transforms
- this.shapeRenderer.rotate(0, 0, 1, - ( wheel.getAngle() * MathUtils.radDeg ));
- this.shapeRenderer.translate(-pos.x, -pos.y, 0);
- }
- this.shapeRenderer.end();
- batch.begin();
- }
- public void reachedCheckpoint(int id) {
- if (this.checkpointId == id-1) {
- Gdx.app.debug("Checkpoint", "Reached checkpoint #" + id);
- this.checkpointId = id;
- } else if (id == 0) {
- // See if we just completed a lap
- int checkpointCount = ((PlayScene)this.getStage()).getTrack().getNumberOfCheckpoints();
- if (this.checkpointId == checkpointCount-1) {
- ((PlayScene)this.world).lapComplete();
- this.checkpointId = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment