News for hackers. Stuff that matters.
Re:Sin and Cos: The Programmer's Pals by 60654 on Tuesday March 06, 2018 @11:26AM (#16529821)
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-...

Re:Sin and Cos: The Programmer's Pals by vvanders on Tuesday March 06, 2018 @12:18PM (#16530226)
Quaternions 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.

Re:Sin and Cos: The Programmer's Pals by Const-me on Tuesday March 06, 2018 @04:50PM (#16532401)
> 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...

Re:Sin and Cos: The Programmer's Pals by vvanders on Tuesday March 06, 2018 @05:17PM (#16532595)
You're right, I was mixing that up with the initial conversion from axis+angle to quaternion.
Re:Sin and Cos: The Programmer's Pals by 0xfaded on Tuesday March 06, 2018 @07:10PM (#16533311)
Until 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.
Re:Sin and Cos: The Programmer's Pals by blt on Tuesday March 06, 2018 @08:19PM (#16533628)
What 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-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.

Re:Sin and Cos: The Programmer's Pals by dahart on Tuesday March 06, 2018 @04:12PM (#16532142)
> 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.

Re:Sin and Cos: The Programmer's Pals by paulddraper on Tuesday March 06, 2018 @01:25PM (#16530794)
In other words...there is no atan3().
Re:Sin and Cos: The Programmer's Pals by jjaredsimpson on Tuesday March 06, 2018 @01:54PM (#16531030)
All 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.

Re:Sin and Cos: The Programmer's Pals by paulddraper on Wednesday March 07, 2018 @06:50PM (#16541069)
Just as atan2() returns 1 angle (XY), atan3() would return 2 angles (XY, XZ).

I agree dot product is often better, for other reasons.

Re:Sin and Cos: The Programmer's Pals by jjaredsimpson on Wednesday March 07, 2018 @07:59PM (#16541383)
I don't follow what this hypothetical atan3 implements?