Hitting the target

April 22, 2009

Aiming and shooting was one thing, but checking if the target was actually hit added a new set of problems.  What ever method I decided on it had to be quick.  There’s a lot of information on the web about collision detection using ray plane intersection, but the maths is quite throughput intensive especially if there are a lot of surfaces to check against.

The first method, and still used in some places, was to generate a bounding box around the target and then to see if the shot was within the box.  Remembering that the XYZ of an object is no longer the same as the XYZ of the drawing system.

Having generated the Up, Left and Forward vectors, or Normals, of our object I can now calculate the bounding box.


To determine if the shot is within the box is a simple dot product of the shot position against the Normals of the each box face.  If the result is positive it is Outside the face, if it is negative then it is Inside the face.  If all faces report Inside then I have hit the target.

After developing all the code to do this I then realised there was an optimisation that further reduced the calculations.



The Normals for the box faces were already calculated, these are the Up, Left and Forward vectors used to generate the bounding box.  This was then further simplified by only needing to calculate and check the dot products against two points of the bounding box on opposite sides.  Now one set of dot products has to report Inside the faces and the other set Outside, this saves flipping the Normals.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: