# Effective methods to continuously update movement in a tower defense game?

We are four guys making a Tower Defense game as a project in first grade on a university. The game is going to be really simple!

• Three different towers
• Three different monsters
• One map (maybe add some more if we have time)

The game has to be object oriented.

The class structure is as following

• Game - Drawing of graphic, etc.
• Level - Every level is an object of this class. Each level has a finite number of wave-objects (in a list)
• Wave - Contains a list of monster-objects.
• Monster - This is a superclass. We make subclasses for the different type of monsters
• Tower - Superclass to the towers. There are subclasses for each type of tower.

We are thinking about how to solve the problem that many objects have to do stuff, at the same time, e.g. move one pixel in one direction.

What we came up with is the idea of implementing av class Timer, to control when objects do things. I am not sure this is the best way to do it. Could someone give some good ideas about how to solve the continious update case?

Game Coding Complete describes an Update loop well. It's simply a while loop running in your main class that does the following (more or less):

while( game.isRunning )
{
GetInput();
Update( dt );
Render();
}


You pass in a dt (delta time) depending on whether you want your game running at a fixed or variable frame rate. For a fixed frame rate, this isn't necessary.

Now to do what you want to do quite easily is use the notion of 2D vectors, where one vector represents the position of the monster (x,y) and a velocity vector that represents the direction the monster is travelling in (in units/sec).

So to create monsters with different speeds, all you need to do is change the velocity vector to whatever you want and let the monster.update() function handle it for you.

For example:

Monster monster = new Monster();
monster.position = new Vector2( 0, 0 );
monster.velocity = new Vector2( 1, 0 );   // Every frame I want the monster
// to move 1 unit to the right


And your update method would look like this (for a fixed frame rate of 60 frames per second):

monster.position += monster.velocity * ( 1.0f / 60.0f ); // The ( 1.0f / 60.0f ) would
// be dt if it was passed into the Update() method


This is known as the Euler update method and it's a very easy and straightforward method of implementing the equations of motion.

Hope this helps.

• +1 for the great explanation. I'd say it'd be worth to mention that "variable time" is not the only approach (although my personal favorite) approach to timing of update loops, variable time vs fixed time is often debated at places such as this and worth a search :) – Zaky German Apr 11 '11 at 14:19

Basically, I use an update loop and not an event based system.

To update monsters, you could iterate a list of monsters and update them in the update loop with, let´s say, monster.update()

If a monster has to walk it has to update its position. So, in the update method I calculate the new position on base of the elapsed time, which is stored in a seperate 'timer' class.

That should be all the 'walking' stuff ;)

• We have the method for monster movement. That is simple enough. The problem is how to get monsters to move, and towers to shoot, when they have different "speed"... – Øyvind Apr 10 '11 at 17:24
• @Øyvind: Just pass the elapsed time as parameter to the update method. Then a monster can calculate its new position by moving elapsedTime * movementPerSecond. Or you could have a cooldown on the shooting tower: if(cooldown > 0){ cooldown -= elapsedTime } else { cooldown = X, shoot(); } etc. – bummzack Apr 10 '11 at 19:14

I suppose it will fill it's purpose well for a uni project, but in a real game i would have a concrete monster class, and instead of subclassing for types, have the different types of monsters described in data files. Speed, health points, resistances to different types of damage and sprites can be perfectly described in files, it will make it easier for you to add new types of monsters to your game, and you won't have to compile each time you'd want to do so.

Anyway, about the movement. Give all your "movable" objects different a speed variable. Each frame in your game logic you loop through all those movable objects and do something like

object.x += Math.cos(object.angle) * object.speed * timeSinceLastIteration;
object.y += Math.sin(object.angle) * object.speed * timeSinceLastIteration;


This applies to monsters, missiles from towers, etc...