Math in computer games
[30 Jun 2007]
Gamesheep.com has an excellent tutorial which explains how to get realistic effects in Flash-based games. And of course, you need some mathematics to achieve that.
The tutorial on developing a car racing game in Flash involves the following mathematics (the links go to explanations of the math):
- Acceleration (and deceleration, which they call “speedDecay”) and velocity
- Trigonometry (including sine and cosine of angles)
- Conversion of degrees to radians (Flash uses an odd mix of degrees and radians): angle_radians = angle_degrees * (PI/180)
- Cartesian coordinate system
- Collision detection (and behaviour after a collision) involves detection of the car’s position, angle and speed
- The floor and rounding functions:
seconds = Math.floor(milliseconds/1000); [this means round the number down to the next lowest whole number], and
tensTXT = Math.round((milliseconds-seconds*1000)/10); [this is normal rounding]
- And of course, they are using addition, subtraction, multiplication and division – which is really all we are doing for most of the time in mathematics
The explanation for realistic steering gives an insight into how they use math to get the desired effect:
We could simply add or subtract a fixed angle (in degrees) to/from the car’s rotation, but that’s not good enough. In order to simulate a natural movement, steering must depend on speed, otherwise you will be able to rotate your car even if it’s almost stopped and it will look like a propeller
One day I hope to get time to write some tutorials on the maths behind the Flash games and activities on Interactive Mathematics that I wrote.