Download (direct link):
Suppose that instead of a hunting game, you’re now writing a game that involves the firing of a cannon from, say, a battleship onto a target some distance away, for example, another ship or an inland target such as a building. To add complexity to this activity for your user, you'll want to give him control of several factors that affect the shell’s trajectory, namely, the firing angle of the cannon, both horizontal and vertical angles, and the muzzle velocity of the shell, which is controlled by the amoun t of powder packed behind the shell when it is loaded into the cannon. The situation is set up in Figure 2-3.
I’ll show you how ro set up the kinematic equations for this problem by treating each vector component separately at first and then combining these components.
The x-components here are similar to that shown in the rifle example of the previous section in that there is no drag force acting on the shell; thus, the x-component of acceleration is zero, which means chat the x-component of velocity is constant and equal to the x-component of the muzzle velocity as the shell leaves the cannon. Note that since the cannon barrel may not be horizontal, you’ll have to compute the x-component of the muzzle velocity, which is a function of the direction in which the cannon is aimed.
34 | Chapter 2: Kinematics
The muzzle velocity vector is
Vm ~ Vn^i + Vmyj + Vmzk
and you are given only the direction of vm as determined by the direction in which the user points the cannon and its magnitude as determined by the amount of powder the user chooses to pack into the cannon. To calculate the components of the muzzle velocity, you need to develop some equations for these components in terms of the direction angles of the cannon and the magnitude of the muzzle velocity.
You can use the direction cosines of a vector to determine the velocity components as follows:
' COS0X = t'mx/t'm
COS By = Vmy/vm
COS @z =Vmz/vm
Refer to Appendix A for a description and illustration of vector direction cosines.
Since the initial muzzle velocity vector direction is the same as the direction in which the cannon is aimed, you can treat the cannon as a vector with a magnitude of L, its length, and pointing in a direction defined by the angles given in this problem. Using the cannon length, L, and its components instead of muzzle velocity in the equations for direction cosines gives
COS dx = Ljc/jL
cos??y = hy/L cos#! = LifL
In this example you are given the angles a and y (see Figure 2-4) that define the cannon orientation.
3D Particle Kinematics 35
Using these angles, it follows that the projection, b, of the cannon length, L, onto the xz-plane is
b = L cos(90° — a) '
and the components of the cannon length, L, on each coordinate axis are =
Lx = b cos y Ly — L cos a Lz = b sin y
Now that you have the information required to compute direction cosines, you can write equations for the initial muzzle velocity components as follows:
Vmx = Vm COS ex Vmy — Vjx, COS &y Vmz VmCOS$?
Finally, you can write the x-components of displacement, velocity, and acceleration as follows:
ax = 0
Vx — Vmx = Vra COS ex X = Vxr = (vm COS dx) t
The y-components are just like the previous rifle example, again with the exception here of the initial velocity in the y-direction:
Vmy “* Vm COS 6y
fly = -g
Vy = vmy -hat = (vm cos0y) - gt
Before writing the equation for the y-component of displacement, you need to consider the elevation of the base of the cannon, plus the height of the end of the cannon barrel to calculate the initiai y-component of displacement when the shell leaves the cannon. Let y b be the elevation of the base of the cannon, and let L be the length of che cannon barrel; then the initial y-component of displacement, yQ is
y0 - yb + Lcosa
Now you can write the equation for y as
y = y0 + vmy? + (l/2)fl?2 ' y — (yb + L cos a) + (vmcos0y)? - (l/2)g?2
36 | Chapter 2: Kinematics
The z-components are largely analogous Co the x-components and can be written as follows:
az = 0 vz - vmz = cos 6Z Z = vz t = (vm cos f)z)t
With the components all worked out, you can now combine them to form the vector for each kinematic property. Doing so for this example gives the displacement, velocity and acceleration vectors shown here:
s = [(vmcos?x)f]i + [(vb + L cos a) 4- (vm cos 6y)t - (l/2)gi2]j 4- [(vm co5 0z)t]k v = [vm cos 0x]i + [(vm cos 0y) -gt]j 4- [vm cos 0z]k a = ~g}
Observe here that the displacement vector essentially gives the position of the center of mass of the shell at any given instant in time; thus, you can use this vector to plot the trajectory of the shell from the cannon to the target.
Hitting the Target
Now that you have the equations fully describing the shell’s trajectory, you need to consider the location of the target to determine when a direct hit occurs. To show how this is done, I’ve prepared a sample program that implements these kinematic equations along with a simple bounding box collision detection method for checking whether or .not the shell has struck the target. Basically at each time step at which I calculate the position of the shell after it has left the cannon, I check to see whether this position falls within the bounding dimensions of the target object represented by a cube.