Vector3.Dot Method (Vector3, Vector3, Single)
Namespace: Microsoft.Xna.Framework
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)
public static void Dot ( ref Vector3 vector1, ref Vector3 vector2, out float result )
Parameters
- vector1
- Source vector.
- vector2
- Source vector.
- result
- [OutAttribute] The dot product of the two vectors.
The dot product is also known as the inner product.
For any two vectors, the dot product is defined as:
Vector3.Dot( vector1, vector2 ) == (vector1.X * vector2.X) + (vector1.Y * vector2.Y) + (vector1.Z * vector2.Z)
The result of this calculation, plus or minus some margin to account for floating point error, is equal to:
Length( vector1 ) * Length( vector2 ) * System.Math.Cos( theta )
Here, theta is the angle between the two vectors.
If vector1 and vector2 are unit vectors, the length of each vector will be equal to 1. So, when vector1 and vector2 are unit vectors, the dot product is simply equal to the cosine of the angle between the two vectors. The following calculation:
Vector3.Dot( vector1.Normalize(), vector2.Normalize() )
Is equivalent to:
System.Math.Cos( theta )
Therefore, if vector1 and vector2 are unit vectors (i.e., we've called Normalize on each vector to make it a unit vector), without knowing the value of theta or using a potentially processor-intensive trigonometric function, the dot product can tell us the following things:
-
If
Vector3.Dot( vector1.Normalize(), vector2.Normalize() ) > 0, the angle between the two vectors is less than 90 degrees. -
If
Vector3.Dot( vector1.Normalize(), vector2.Normalize() ) < 0, the angle between the two vectors is more than 90 degrees. -
If
Vector3.Dot( vector1.Normalize(), vector2.Normalize() ) == 0, the angle between the two vectors is 90 degrees; that is, the vectors are orthogonal. -
If
Vector3.Dot( vector1.Normalize(), vector2.Normalize() ) == 1, the angle between the two vectors is 0 degrees; that is, the vectors point in the same direction and are parallel. -
If
Vector3.Dot( vector1.Normalize(), vector2.Normalize() ) == -1, the angle between the two vectors is 180 degrees; that is, the vectors point in opposite directions and are parallel.
Caution |
|---|
| Because of floating point error, two orthogonal vectors may not return a dot product that is exactly zero. It might be zero plus some amount of floating point error. In your code, you will want to determine what amount of error is acceptable in your calculation, and take that into account when you do your comparisons. |
Caution