News for hackers. Stuff that matters.
Re:How Quaternions encode rotations: derivation and sample code by Sharlin on Wednesday September 07, 2016 @06:49AM (#12442203)
I haven't thought about it before but once you start thinking of complex numbers as "numbers that want to turn" (every unit complex number is isomorphic to a rotation on R^2), the concept of quaternions analoguously representing rotations in R^3 becomes pretty intuitive even if you don't quite grok the details.
Re:How Quaternions encode rotations: derivation and sample code by ThrustVectoring on Wednesday September 07, 2016 @10:36AM (#12443728)
Similarly, Euler's Identity (e^i*pi = -1) becomes a lot more intuitive. The exponential function is continuous (edit: proportional) growth, i is turning sideways, sideways continuous growth traces out a circle, and pi is halfway around a circle.
Re:How Quaternions encode rotations: derivation and sample code by jordigh on Wednesday September 07, 2016 @10:53AM (#12443881)
... what?

I guess if it makes sense to you, ok, but "continuous growth in a circle" to explain the complex exponential sounds like cloud-gazing to me: the human mind making up patterns where there are none.

The only way it makes sense to me is by looking at the power series and seeing how the exponential power series is almost the same as the sine and cosine power series (alternatively and equivalently: all of the exponential, hyperbolic, and circular functions satisfy almost the same differential equation).

Re:How Quaternions encode rotations: derivation and sample code by ThrustVectoring on Wednesday September 07, 2016 @12:30PM (#12444705)
I think I was a bit loose in the wording in order to try to get the phrasing as compact as I could. The exponential function is continuous proportional growth - the limit of adding a part of a vector to itself proportional to the current vector and the "step size" as you take the step size to zero. (Real numbers are just the one-dimensional special case of vectors here).

Anyway, my point is that Euler's identity generalizes to any number that encapsulates "sideways" in Euclidean geometry. Any quaternion "x" that is a unit of rotation will satisfy "e^pi*x = -1". You can't get that easily from the power series understanding.

Re:How Quaternions encode rotations: derivation and sample code by abecedarius on Wednesday September 07, 2016 @06:18PM (#12448397)
No, it makes sense. exp(i epsilon) = 1 + i epsilon, to first order, i.e. when epsilon is near 0. You can think of 1 + i epsilon as a tiny rotation, when you multiply by it. exp(i t) = the product of a lot of tiny rotations, just as in real numbers exp(x) = the product of a lot of tiny scalings. The product of rotations equals one rotation by the sum of their (individually tiny) angles. As t increases continuously, exp(i t) rotates around from 1 continuously. The unchanging speed of rotation corresponds to how in e.g. bacterial growth, exp(t), the population gets some constant factor bigger in every equal interval.

(I first got this picture from the Feynman lecture on algebra, http://www.feynmanlectures.caltech.edu/I_22.html, which doesn't seem to ever explain it that way explicitly. It's funny how memory works.)

Re:How Quaternions encode rotations: derivation and sample code by jordigh on Wednesday September 07, 2016 @06:32PM (#12448519)
Okay, this makes a little more sense to me, since it's just the power series again. But I don't think that calling a rotation a growth makes sense. Also, to me, the approximation e^(i epsilon) ~ 1 + i epsilon kind of begs the question.
Re:How Quaternions encode rotations: derivation and sample code by ThrustVectoring on Wednesday September 07, 2016 @07:09PM (#12448805)
e^(epsilon) ~ 1 + epsilon in general, because of the definition of the derivative and the fact that d/dx (e^x) = e^x
Re:How Quaternions encode rotations: derivation and sample code by jordigh on Wednesday September 07, 2016 @09:54PM (#12449631)
Right, the latter fact is equivalent to considering exp the solution of a differential equation, which is a much bigger fact. Treating the approximation as an innocent brute fact is what I considered begging the question, because why should this differential equation be valid for complex values?
Re:How Quaternions encode rotations: derivation and sample code by abecedarius on Wednesday September 07, 2016 @10:21PM (#12449766)
I was trying to make a geometric intuition accessible to high schoolers who haven't mastered calculus -- though I haven't tried it on an actual teenager.

Of course there's also value in looking deeper into the foundations.

Re:How Quaternions encode rotations: derivation and sample code by jerf on Wednesday September 07, 2016 @11:06AM (#12443992)
"Continuous growth in a circle" may indeed be a personal visualization, but "complex numbers are deeply connected to rotation" is a mainstream point of view in mathematics.
Re:How Quaternions encode rotations: derivation and sample code by jordigh on Wednesday September 07, 2016 @11:22AM (#12444126)
Sure, complex multiplication is rotation, no problem with that part. To multiply two complex numbers you add their angles and multiply their lengths.
Re:How Quaternions encode rotations: derivation and sample code by ittekimasu on Wednesday September 07, 2016 @12:09PM (#12444542)
Parent is referring to the ODE \dot{z} = i z.
Re:How Quaternions encode rotations: derivation and sample code by Bootvis on Wednesday September 07, 2016 @06:51AM (#12442213)
Hmm, but I would think that a quaternion represents a rotation in R^4 (for which I lack intuition).
Re:How Quaternions encode rotations: derivation and sample code by Sharlin on Wednesday September 07, 2016 @08:19AM (#12442702)
If you only want to represent a rotation there's some extra information in a quaternion, just like there is in a complex number - an arbitrary number in C actually represents rotation-and-scale where the magnitude of the number is the scale factor. If you stick to numbers with unit magnitude, given one part you can calculate the other, up to reflection. Similarly, with a unit quaternion you can calculate any one component given the other three, again up to reflection.

In general the number of real numbers needed to describe a rotation is the number of rotational degrees of freedom. This number is n(n - 1) / 2 in n dimensions, or C(n, 2), basically the number of orthogonal (2-)planes of rotation in R^n.

[0] https://en.wikipedia.org/wiki/Degrees_of_freedom_(mechanics)

Re:How Quaternions encode rotations: derivation and sample code by UlyssesSKrunk on Wednesday September 07, 2016 @02:48PM (#12445968)
So in 3d that becomes 3(2)/2 = 3. Quaternions have 4 parts. That's what has always made it hard for me to understand.
Re:How Quaternions encode rotations: derivation and sample code by Sharlin on Wednesday September 07, 2016 @11:19PM (#12450048)
And in 2D it is 1 but complex numbers have two parts. As I tried to explain, there's simply some redundancy in the representations because it's just unit complex numbers/quaternions that we're interested in. We accept that because it makes the math prettier. Heck, matrices, the de facto standard for representing rotations, have much more redundancy - four and nine elements in 2D and 3D respectively.
Re:How Quaternions encode rotations: derivation and sample code by logfromblammo on Wednesday September 07, 2016 @11:38AM (#12444271)
The constraints between the numbers, when the quaternion is defined as a rotation, end up having three degrees of freedom, spread over four numbers. You can't change any one of them without also changing another or violating the constraints of the rotation definition.

Similarly, when a quaternion is defined as representing a translation, that is likewise 3 degrees of freedom in 4 numbers. The four numbers would be similar to a real cartesian translation with dX, dY, dZ, and a fourth number = sqrt( dX^2 + dY^2 + dZ^2 ).

It's like the 4th number is a checksum for the other 3. You are adding a fourth number to use a more elegant mathematical calculation, which automatically keeps the dependent data relationships intact, like magic.

Re:How Quaternions encode rotations: derivation and sample code by chombier on Wednesday September 07, 2016 @12:40PM (#12444813)
> Similarly, when a quaternion is defined as representing a translation

How do you encode translations with quaternions? Don't you need dual quaternions for that?

Re:How Quaternions encode rotations: derivation and sample code by logfromblammo on Wednesday September 07, 2016 @12:45PM (#12444887)
No. You need dual quaternions to encode rotation and translation at the same time.
Re:How Quaternions encode rotations: derivation and sample code by chombier on Wednesday September 07, 2016 @01:26PM (#12445296)
Ok this I know, but how to do translations only? (apart from the obvious vector space structure in R4)
Re:How Quaternions encode rotations: derivation and sample code by logfromblammo on Wednesday September 07, 2016 @02:41PM (#12445903)
There are multiple ways that will work. The simplest I can think of is to fix the real part at zero and use i, j, and k to represent deltas along the x, y, and z axes. Then point + translation = point.

There may also be a representation that also encodes distance from the origin, but I am probably confusing this in my memory with something else.

Re:How Quaternions encode rotations: derivation and sample code by uryga on Wednesday September 07, 2016 @07:00AM (#12442246)
===== EDIT: this is mostly wrong - see jblow's reply. Sorry for misleading you into thinking I know what I'm talking about :D =====

I think of it like this: a complex number represents a rotation in a plane. You need two "coordinates" to do that. 2d has one plane, but 3d has two planes - think about how an anti-air cannon rotates left-right and up-down. So, if we've got two rotations to represent, you must need two complex numbers - that's four "coordinates", or one quaternion.

Re:How Quaternions encode rotations: derivation and sample code by lloeki on Wednesday September 07, 2016 @10:54AM (#12443886)
Unfortunately you got it wrong, and your AAA example shows it very well.

Just as in 2d with a complex, in 3d you can encode rotations with a 3d vector. The problem is that to reach any point from any other arbitrary point you can't use a single rotation, but a combination of two, sometimes three. Think about how your AAA gun operates or FPS viewpoint controls. Thus you start to use rotations along the three axes of a base which gives you a typical rotation matrix.

As soon as you start to compose rotations along axes you introduce a terrible phenomenon known as gimbal locking, where discontinuities appear especially near north and south poles.

And so, just as a 2D rotation in a plane can be represented by a vector orthogonal to that plane (i.e in a space outside the plane), the gimbal lock and all artifacts that are cumbersome when constraining ourselves in 3D can be dealt with by going up a dimension... Hence quaternions which encode+ a rotation by putting an arbitrary but well chosen axis and an amplitude in a space grown othrogonally to our 3D space.

+ Or isomorohically so

Re:How Quaternions encode rotations: derivation and sample code by blt on Wednesday September 07, 2016 @12:58PM (#12445022)
Your analogy is incomplete because you forget rotating the cannon about its barrel.
Re:How Quaternions encode rotations: derivation and sample code by chombier on Wednesday September 07, 2016 @07:05AM (#12442266)
> but 3d has two planes

Why not 3 planes?

Re:How Quaternions encode rotations: derivation and sample code by uryga on Wednesday September 07, 2016 @07:41AM (#12442463)
Okay, my wording wasn't the best here. 3d doesn't "have" 2 planes. But, for some reason, you can look in any direction in 3d by turning in two planes. Test it yourself: turning your head left-right is rotation in one plane, turning it up-down is rotation in the other plane. here's my shot at illustrating this: https://goo.gl/photos/Cxt7KPbos5o2QnkD9

to be completely honest, I'm not sure if this intuition is correct. But it seems to make sense

Re:How Quaternions encode rotations: derivation and sample code by petters on Wednesday September 07, 2016 @08:26AM (#12442759)
I don't think that's correct. You can also rotate around the direction you are looking.

Rotations in 2D are one-dimensional. Complex numbers are two-dimensional but we fix one degree of freedom by fixing the length.

Rotations in 3D are there-dimensional. Quaternions are four-dimensional but we fix the length again.

Re:How Quaternions encode rotations: derivation and sample code by jblow on Wednesday September 07, 2016 @08:00AM (#12442569)
It's correct that you can look in any direction with only two planes, but that's not enough; you need to be able to control your orientation around that final axis. You need 3 planes for that (3 "rotational degrees of freedom"). The idea that a quaternion is somehow two complex numbers is wrong.
Re:How Quaternions encode rotations: derivation and sample code by uryga on Wednesday September 07, 2016 @08:32AM (#12442794)
Thanks. I did some research and found out that, indeed, my vague understanding of quaternions was wrong. I remember skimming the Wikipedia article and seeing this: https://en.m.wikipedia.org/wiki/Quaternion#Quaternions_as_pa...

I then assumed that my idea with two rotations is how quaternions work. Here's to actually reading about a concept before I start explaining it next time...

Re:How Quaternions encode rotations: derivation and sample code by chombier on Wednesday September 07, 2016 @08:36AM (#12442818)
I think you can do that using two rotation axis only (cf. proper Euler angles).

And quaternions are made of two complex numbers.

edit: typo.

Re:How Quaternions encode rotations: derivation and sample code by Kristine1975 on Wednesday September 07, 2016 @09:32AM (#12443204)
>And quaternions are made of two complex numbers.

I don't pretend to grok quaternions (I only use them), but I'm fairly sure a quaternion consists of three imaginary numbers plus a real number.

Re:How Quaternions encode rotations: derivation and sample code by chombier on Wednesday September 07, 2016 @10:16AM (#12443543)
Sure, but the way they play together can also be expressed by the means of two complex numbers (which makes 4 real numbers in total).
Re:How Quaternions encode rotations: derivation and sample code by jblow on Saturday September 10, 2016 @02:28PM (#12470342)
This is not true. You can't factor a quaternion into two complex numbers. Please don't spread misinformation.
Re:How Quaternions encode rotations: derivation and sample code by Kenji on Wednesday September 07, 2016 @08:22AM (#12442727)
We use quaternions in R^3 to avoid gimbal lock.