3 ms·
Yeah, there are multiple reasons. Perf is important: inverse trig functions are generally much slower both on the CPU and on the GPU [1] But also - most of the
by 60654 9y ago
Yeah, there are multiple reasons. Perf is important: inverse trig functions are generally much slower both on the CPU and on the GPU [1]
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-instruction-cost-(performance)/msg84618/#msg84618 http://www.fractalforums.com/programming/shader-function-or-...
- dahart 9y ago> 1/x, sin(x), cos(x), log2(x), exp2(x), 1/sqrt(x) - 0 or close to 0, as long as they are limited to 1/9 of all total ops (can go up to 1/5 for Maxwell). This is what surprised me to learn recently, that some transcendentals are free, as long as you don't use too many of them.
- vvanders 9y agoQuaternions have a bunch of properties that make them awesome: * 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.
- Const-me 9y ago> 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/DirectXMathMatrix.inl#L1654-L1736 https://github.com/Microsoft/DirectXMath/blob/master/Inc/Dir...
- vvanders 9y agoYou're right, I was mixing that up with the initial conversion from axis+angle to quaternion.
- 0xfaded 9y agoUntil you need to differentiate a quaternion ... 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.
- blt 9y agoWhat is the problem with differentiating a quaternion? E.g. the time derivative of the quaternion w.r.t. a fixed angular velocity is given here: https://fgiesen.wordpress.com/2012/08/24/quaternion-differentiation/ https://fgiesen.wordpress.com/2012/08/24/quaternion-differen... . 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.
- paulddraper 9y agoIn other words...there is no atan3().
- jjaredsimpson 9y agoAll triangles are 2d. Using atan2 to find the angle between 3d vectors would require projection onto an orthonormal basis. 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.
- paulddraper 9y agoJust as atan2() returns 1 angle (XY), atan3() would return 2 angles (XY, XZ). I agree dot product is often better, for other reasons.
- jjaredsimpson 9y agoI don't follow what this hypothetical atan3 implements?