h.i.e.p. karma

If you use Firefox...

Firefox user? If not, get started! If so, then check this article out:

Ten Firefox Extensions You Can't Live Without
Permalink - 01/26/06 - 15:20:36 - Hiep - No comments

3D Math: Determining which side an object is on

With some sketches and thinking, I found out how to determine if something is to your left or your right, using 3D math and the vector dot product.
The key here is understanding how the dot product works, and how you can use 90 degrees to your advantage.

The dot product will tell you relatively quickly if something is more, less or equal to 90 degrees. How do we rearrange this around to determine what side something is on relative to us? Let's look:

These are the things we know:
- Our location (lets call that loc1)
- Our object of interest's location (loc2)
- Our facing [vector] (vec1)
- The vector pointing from us to our object of interest (loc1 to loc2, lets call that vec2)

Here's the trick:

Using our facing vector, we rotate that 90 degrees clockwise, so that it points exactly to our right. This is the key.
Now we have this vector, which we'll name vecRight.

(To get vecRight the easiest way to do it is generate a 90 deg Y rotation matrix, and multiply that matrix with our facing vector. That should rotate our facing vector vec1 90 degrees. This will rotate our vector nicely. Which radian value you use (neg or positive) depends on your coordinate handedness.)

All that's left to do is get the vector dot product of vecRight with vec2. If the result is > 0 (therefore acute angle), our object is to our right. If the result is < 0 (obtuse), our object is to the left.
Permalink - 06/25/04 - 15:05:41 - Hiep - No comments
Get Firefox!