Thursday, 29 March 2007

Cars

During this workshop you will play with the mechanics of cars. You will also learn how to implement simple physics-based engine.

Prerequisites

First, please download the following image of a car. Click with a right button of your mouse and choose Save Picture as...
Please save the image where you have all the java games images (depending on the version, it may be the main Games folder or Games/images subfolder).

Below is the list of the source files that you need. You can write them yourself, create them working along with the Tutor, or simply download them. In all cases please use the Create New Package button in your eclipse to create a new package named Cars and ensure all the source files are created/stored within this package. The package home folder on the disk is in your eclipse Workplace, Games/kingston/games/cars.

This is the list of source files:
  • Cars.java - start-up class. Once created, you probably will have no need to modify it.
  • CarsGame.java - the main game class.
  • Car.java - the Car Sprite class.

CarsGame Class

This is the main class of this game. If you downloaded its file, you probably won't need any corrections to be done, as the version here available is enough functional to go through this exercise. Still you may want to know what this class does.

Summary of variables:

  • imageCar - contains a bitmap with the image of the car
  • car - reference to the Car object

Summary of functions:

  • onStart: loads the image of a car and create a Car class object - the car Sprite.
  • onUpdate: updates the state of the Car object - delegates this task to Car.onUpdate
  • onDraw: draws the playfield, namely the Car object on top of a large white rectangle.
  • onTerminate: does nothing.
  • keyPressed: this function is called whenever the user presses any key. It checks ESC and ^C keys to exit the game; F2 to start the game anew; RIGHT/LEFT arrows to control turning; UP/DOWN arrows for forward/reverse gears; SPACE for brakes.
  • keyReleased: this function is called whenever the user releases a key and switches off the key-related functions.
  • newGame: this function is called whenever the game is started anew after pressing F2. The function simply destroys the old car object and creates a new one.

Car Class

This class encapsulates entire functionality of the car and extends the Sprite class. It controls the your car look and behaviour.

Summary of variables:

  • turn: see turn function below.
  • accel: see accelerate function below.
  • brakes: see brake function below.

Summary of functions:

  • turn: call turn(-1) to turn left, turn(1) to turn right or turn(0) to drive straight on.
  • accelerate: call accelerate(1) for forward gear, accelerate(-1) for reverse gear, or accelerate(0) for neutral.
  • brake: call brake(true) to switch the breaks on and brake(false) to switch them off.
  • onUpdate: see the next section below...

Your task to do

Depending on:

  • the gear (forward, reverse or neutral),
  • the turning (left, right or straight on) and
  • the brakes (on or off)

please implement the physically realistic behaviour of a car. Your implementation should be placed within the Car.onUpdate function.

Hints:

  1. Depending on the gear, determine the acceleration. Reverse acceleration should be around 3-5 times lower than the forward one.
  2. Calculate air resistance as a multiple of the square of your velocity.
  3. Use fixed value for the rolling resistance.
  4. Brakes are another form of resistance. Apply only when brakes are on!
  5. Modify your velocity. In general, velocity = (previous) velocity + acceleration * time elapsed. Be careful about the resistances: they always should decrease the absolute value of the velocity.
  6. Finally use the turn value. You can turn left or right adding or subtracting very small values from the direction and rotation of your car.

Index of useful classes available within your Sprite/Car object:

  • getMyTime - time elapsed since previous update.
  • getVelocity, setVelocity - numerical value of velocity.
  • getDirection, setDirection - direction of the motion (vector or velocity)
  • getRotation, setRotation - rotation of the sprite.

No comments: