## Time Based Movement in D

We can use the Vector3 class to do time-based movement in 3D in much the same way as we do in two dimensions. As an example, let's use a little 3D vector math to calculate a target vector and work out the intermediate coordinates for a projectile weapon (see Figure 8-3).

### Figure 8-3. Calculating a target vector

Soldier Alpha has walked a few meters away from his original position in Figure 8-2, and is now standing at point (-6, 2, 0). The spy droid is still hovering at (7, 5, 10), monitoring Alpha's actions. Fortunately Alpha's acute hearing (or the player's speakers) pick up the faint whirring noise of its antigravity engine and he decides to take out the droid. To fire at the droid, Alpha needs to calculate a vector from his shoulder-mounted plasma rifle to the droid's location. Figure 8-3. Calculating a target vector

Alpha may be standing over point (-6, 2, 0), but his shoulder is 2 meters above the ground at point (-6, 2, 2), so this is the starting point for the vector calculation. The vector to his target is produced by subtracting the droid's position—point B at (7, 5, 10)—from the starting point A at (-6, 2, 2), giving us a target vector of (13, 3, 8). Normalizing this vector produces a heading vector that can be used in time-based movement. Listing 8-4 shows how to do these calculations in code.

Listing 8-4. Creating a Target Vector from gameobjects.vector3 import *

plasma_speed = 100. # meters per second

AB = Vector3.from_points(A, B) print "Vector to droid is", AB

distance_to_target = AB.get_magnitude()

print "Distance to droid is", distance_to_target, "meters"

Running Listing 8-4 produces this output:

Distance to droid is 15.5563491861 meters