Urban Software - UrbanCTF, UrbanDM, Earn A Weapon, CTG...
Guided Rockets and Client Prediction


Date : 01/18/99
Author(s) :
Skill(s) :
Source Project(s) :
Revision :


With this tutorial, we'll learn how to handle the client prediction in Q3:A, with the Guided Rockets (tm). Basically, client prediction is used to avoid too much net traffic. In Quake2, the server had to send to all the clients packets holding the origins of the rocket, so the client's renderer (quake2.exe) could display it using those coords. Okey, that was fun, but now there's something better ! We're gonna tells Q3 from where our rocket is fired, and what direction our rocket is taking, and we'll let the Q3 renderer calculate where the rocket will explode !

In q_shared.h, you'll find the following structures :


The trajectory_t structure will be used when we'll have to give a movement to an entity. The main fields will be trDelta, the old Q2's ent->velocity (the velocity vector), and trBase, the origin of the movement/trajectory.

Now, let's write our guided rocket code. Open up g_missile.c, and write this just above the fire_rocket() function :

Then move to the fire_rocket(...) function just under our new one, and modify it so it looks like this :

So what we're doing here is replacing the regular rocket think function (goes straight ahead and explode once it touchs something) by our own think function, Guided_Missile_Think. So when the player will shoot a rocket, he'll be able to aim his rocket wherever he wants with his mouse. However, we're testing if "self" is really a client, just in case... Bots will also be able to use guided rockets as they use the same firing function.

Compile your code, and run the game. Take the rocket launcher, and fire... Add some bots and kick their ass around the map... Who said "soooo easy !" ? ;)

Okey now stop playing... Let's study our code. In our think function, we've played with client prediction. Everytime we change the direction of the rocket, we tells the engine that we're also changing the rocket's behaviour, so the client's renderer knows he has to change the rocket's movement. We're using the current rocket's origin to setup the origin of our new direction change, and then we also change the velocity and angle of the rocket so it's going to the direction where we're aiming at. Now, on the client side, the renderer (well, the cgame part) will calculate where the rocket should go, thanks to our trBase and trDelta informations. Just try removing those datas from our new code, and you'll see your rockets going in VERY strange directions ;)