Show newer


The other important formulas are how to multiply quaternions, which like for matrices, corresponds to composing transforms (a * b = rotation by b then by a), and the formula for actually transforming a vector given its x/y/z, but I'll let you look those up on your own since they're a lot to put here XD'
(I might add them later though, for reference ^^' )




So here are the formulas for that!

For quaternion = (a,b,c,d)

a = cos(θ/2)
b = x * sin(θ/2)
c = y * sin(θ/2)
d = z * sin(θ/2)

Where x/y/z are the coordinates of the axis of rotation vector and θ is the amount/angle of rotation in radians :D



To store a 3D axis and an angle, that would take four numbers, right? Three for the axis vector and one for the degrees/radians :>

Quaternions are simply a way of storing those four numbers in a way that's very nice because in quaternion form, the expensive trig functions are precomputed/cached!

And all you need to do to rotate a vector by a quaternion is just multiplication and addition! :D

(A lot like a rotation matrix! But a quaternion only takes 4 numbers not 9!)



Now a bit of background: all rotations can be thought of as rotations around an axis and an amount/angle of rotation around it, right? :>

You can even take *as many rotations as you like* about *different* axes and combine them together into one probably-bizarre rotation about a single very particular axis and a single very particular angle!

That's :>


@dannyboy :D!

Well it's actually a little bit different than I said there; I corrected myself a few posts down ^^'

But you know how in a given coordinate system, a 3D vector is basically just 3 numbers?

Well in a coordinate system, a quaternion is basically a group of four numbers! Usually called a,b,c,d


@tpfto @freemo Oh that looks like that's it! That's awesome, thanks! :D

Someday I'll know what enough of the words mean to understand that page XD

@tpfto yesssss :>

(But btw see my other post for how I got it slightly wrong! The scalar part is actually the cosine of half the angle and the axis-of-rotation vector part is not unit length but sine of half the angle long! :3 )

@clarfonthey Ahhh I wondered if that was how it was used :D

Thanks for all this! :D

@freemo and yes definitely! there's no matrix multiply primitive in machine cores (at least not yet XD )

just things like FMA—floating-point-multiply-and-add, which is useful for matrix multiplication but can be used for other things :>
(I once made a parallel thing in C that used it and numerically computed ordinary differential equations without actual "matrices")

but it's very nice and convenient codewise to reuse mmul()/etc., esp. if there's no computational benefit from another formula :3

@freemo ohhhhhh I see! the normal way of using Euler angles involves an if/then branch?

well then yes that's not good for SIMD/etc. XD


since if there's a simpler formula to go straight from a quaternion and vector to the transformed vector than the number of operations involved in matrix multiplication, then even the GPU/parallelized code would benefit! :D

I mean you could just write it out, but if that's the same number of additions/multiplications as matrix multiplication would be, there's no point X3

@freemo also I've never used Euler angles in practice, so I didn't know gimbal lock was a thing until just now—I thought you meant a kind of concurrency locking thing XD'

@freemo ohhhhkay I think I get it :>

so you're saying if you start with axis-angle representation or euler angle representation and calculate a matrix for rotation, then parallelize the application of that matrix to all the points like normal, that's hardly more efficient than computing the matrix from a quaternion and applying the same matrix to all the points? that makes tons of sense yes :3

I wonder if there's a simpler way than computing a matrix from a quaternion though? :>

@freemo Ohhhhhh I see :D

You mean compared to running the trig function for every point!

Factorizing the rotation into values that can be kept the same for all points means the parallelized part need only apply a simple addition/multiplication formula for each point and the threads can share those factorized numbers across them :>

And if you store and pass around *just* those numbers across functions instead of the axis x/y/z and θ, you'd basically end up recreating quaternions! :D



1) I get it :D

2) You'll have to run that by me again. Surely parallelizing the three or so formulas of a single vector's rotation isn't really a thing that's fruitfully done so I must be missing something! (I'd figure one would parallelize the same rotation for many points on say a big polyhedron, regardless of whether quaternions or etc. are used for it. Like one CPU core would take a quarter of the vertices to be rotated and another core another fourth, etc.)

@freemo well they did seem obscure to me in that 2D complex numbers come from negative square roots and logarithms,

whereas quaternions just seemed arbitrary and "made up"—like that the multiplication table could have been different and would have been a different extension of complex numbers to 4D!

is that not true? is there something about it that makes it a unique extension of complex numbers in 4D? :>

@freemo that's still super useful :>

for me, largely for the eternal quest of avoiding actually computing trig functions in code! XD :D

but it's wrong to say that the quaternion coefficients match up bijectively, since the vector ones are indeed the axis of rotation, but are still dependent of θ, so you can't separate so neatly them!

the quaternion vector is the axis of rotation *scaled* by a function of θ, not as a unit vector! ;D

@freemo OH WAIT
Attention everyone who saw this post! This is totally wrong!! XD'''

Quaternions *are* strange formulas for transforming coordinates!!

And here they are!
It still annoys me that they don't appear *anywhere on the actual wikipedia page for quaternions*!! X'D

But on this page!

for quaternion = a + bi + cj + dk

a = cos(θ/2)
b = x sin(θ/2)
c = y sin(θ/2)
d = z sin(θ/2)

where x/y/z are the axis of rotation and θ is the amount in radians!

Show older

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!