Game Development Reference
In-Depth Information
Taking Aim
When discussing aim, we'll primarily be talking about rifles or carbines here, as hand‐
guns are not usually used for long-distance shooting. Similarly, shotguns, given that
they fire many small projectiles, are generally not carefully aimed but instead pointed.
Both of these weapons are what are known as point-blank weapons . Rifles have a point-
blank range, which we will discuss, but for handguns and shotguns this really refers to
the fact that at the ranges where these weapons are effective, you can reasonably expect
to hit where you the point the gun. That is not to say that these weapons don't need to
be aimed to be effective, but in the fast-paced combat central to most first-person
shooters it would be tedious to have the player use the sights on a handgun to effectively
hit anything. Instead, most games use a “shoot from the hip” or free aim model, where
the gun is not even in line with the camera. The careful programmer might still check
to see if the target is within the effective range of the bullet before counting it as a hit
even when auto-aiming in this manner. The effective range is the distance the bullet can
travel before hitting the ground. Determining this is a straightforward application of
the equations in Chapter 2 and Chapter 6 .
To discuss firearms, we've adapted the code from the Cannon2 example in Chapter 6
to run in a Java program called Marksman. In our example, the player is looking through
a scope at a target of 1 meter by 1 meter. We've provided him an adjustable level of zoom
so that as the range increases, he can still see the target. The aiming point is shown as
an empty circle, and the bullet hole as a black filled circle. The method we've used to
determine where the user is aiming in the model world converts the pixel-based location
of the mouse to a coordinate in the model world. This distance and the range are then
used to find the angles required to aim the gun. The code to do this is shown next, where
alp and gmm are the angles of inclination and bearing. These are measured from hori‐
zontal and the line of view:
alp = 90-Math.toDegrees(Math.atan(((200-aimY)*(targetH/(drawH)))/range));
gmm = Math.toDegrees(Math.atan(((200-aimX)*(targetH/(drawH)))/range));
where targetH/drawH is just the ratio of the target height to the height of the target in
pixels on the screen. This allows the mouse coordinate given in pixels to be converted
to meters. The arctangent then converts the ratio of these distances to an angle for the
gun. The constant 200 refers to the pixel coordinate system, which is 200 pixels away
from the center of the target. If you were to adapt this for a full 3D rendering system,
you could remove a lot of these conversions we have to do.