But also - most of the time gamedev happens in 3D space not in 2D space (even if it gets rendered down to 2D). So it's more natural to do a dot product of two 3d vectors, than try to mess around with trig functions that work on scalar angles. Same with rotations: it's easier and better to use quaternions in 3D space than try to rotate along each axis separately.
[1] GPU intrinsic estimated costs: http://www.fractalforums.com/programming/shader-function-or-...
* Compact storage compared to matrices
* No gimbal-lock
* Multiplication is cheap (mult + add, very similar to vector operations)
* Interpolation is cheap
You really only pay the sin/cos costs when translating out to a Mat3x3 at the end.
You don’t need sin/cos for that either.
Here’s a code that does that:
https://github.com/Microsoft/DirectXMath/blob/master/Inc/Dir...
Also, you don't pay the sin/cos costs when converting to a rotation matrix. For angle axis
R = I + (sin x) * S + (1 - cos(x)) * S^2
Where S is the 3x3 skew symmetric matrix.A unit quaternion representing a 3D rotation is sort of like angle axis
q = cos(x/2), sin(x/2) <axis>
But sin(x) = 2*cos(x/2)*sin(x/2)
(1-cos(x) = 2*sin(x/2)*sin(x/2)
Where you pay the cost is in the construction of the quaternion.I've more often encountered the need to integrate a quaternion, e.g. from inertial measurements or a rigid body physics model, but this is also a textbook lookup away.
This is what surprised me to learn recently, that some transcendentals are free, as long as you don't use too many of them.
atan2 requires this even in 2d, we just assume the natural basis and assume the other vector is x hat.
Dot product avoids all this in any dimension.
I agree dot product is often better, for other reasons.
It's because atan2, along with all the other inverse trigonometric functions, is incredibly slow. On a GPU, sin and cos may be a couple of cycles, but asin/acos/atan/atan2 may be upwards of 30 or 40.
Often times, angles are best avoided when possible for this reason. Normalized vectors end up faster and simpler in many cases.
Relevant anecdote: I recently wrote some code to compute normals in 2D and thought I would be clever by encoding the normal as an angle to save space. It turned out that the atan2 call at the end was so slow that it outweighed the cost of everything else I was doing several times over. Lesson learned…