The Technical Details


This really boring page is only here because someone asked for it as it were. The game makes use of some basic physics to calculate in real time the paths of the projectiles. I have tried to keep this as maths free as possible, so as to give a general overview of the techniques that have gone in to the game.

Intial game setup

Initially the positions of all the bodies are generated randomly. This is done by giving an random value to the x and y position of the planets and then the stations and then checking that they don't overlap. This whole thing is repeated until the program is happy with the arrangement. It is very crude, but seems to work most of the time! The parameters are changed for the different scenarios, eg, the star scenario doesn't allow planet 1's position to vary much, therefor recreating a star-system like map. Easy really. I have varied the density of the planets and stars in the different scenarios in order to result in a playable, but different environment for each scenario. There is no point in having even slightly realistic values for mass, radius or distance, or the chance of hitting anyone would be quite literaly astronomical!

Firing the bullets

With the map set up, the game starts. Initial angles and power are read and stored from the sliders, easy enough in java! From the initial angle and power and using a little trigonometry the initial velocity (as x and y components) and x and y position of the bullet can be calculated taking in to account the size of the station so the bullet starts outside the station and is aiming away from it.

Main simulation

When the calculation starts properly each iteration of the main loop corresponds to a time step, delta t. The overall force and hence acceleration (by F=ma) is only a function of position and therefor can be calculated by summing the forces in the x and y direction separately for all of the gravitational bodies. The individual forces are (thanks to Newtons law of gravitation) proportional the inverse of the square of the distance. I put the gravitational constant to unity for simplicity. So having calculated the force and therefor the acceleration on each bullet, the velocities are updated for that time step and then the positions are changed for that time step by the velocities. Having changed the positions, the bullets can be checked for various collisions.

Numerical problems

The calculation suffers from numerical errors if the time step is too large, so the calculation uses quite a small time step. However the position of the bullet is only output to the screen every so many timesteps so the game runs smoothly. There are many other more complicated methods for calculating this, however they are only really needed for astronomical simulation of great accuracy. The path taken by the bullet is surprisingly accurate and only shows signs of going wrong for the unusual scenairios of wormholes and black holes. If you want to test it, select the black hole scenairio and put the number of planets to 1. Fire a shot at right angles to the black hole at low power. The shot should either hit the black hole or orbit it and hit you. Any deviation from returning to the exact position fired from is due to numerical errors. The display is flicker free and smooth, by not double but triple buffering allowing for a background layer that records the trails of the bullets without them being overwritten by explosions etc. The starfield, planets and trail are on the back buffer, which is copied to the front buffer on to which the bullets, stations and explosions are placed which is then copied to the screen buffer.

Computer player AI

The artificial intellegence (AI) of the computer players is very crude. They take pseudo-random simulated shots with less time steps and record the shot that gets closest. They then try and improve on that shot by slightly adjusting the angle and power. The best shot from the previous turn is used next turn making them more accurate each go. The challenge is not making them accurate, but making them aim better and better each time like a human. The higher level bots also select a target as a team, not all firing at the same target and tend to target the closest enemy first or the biggest threat. The better bots use more accurate simulations when firing and simulate longer shots. Hyperspacing is done randomly (I will change this at some point) for the better bots the chance of hyperspacing is effected by conditions.

Related Links

click here to return to DEATH STAR BATTLES