Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Quaternions are a bit difficult, aren't they...

AJ DaSilva
woz ere
Join date: 15 Jun 2005
Posts: 1,993
08-25-2005 17:15
I've got no desire to learn complex numbers today; I'm just curious as to whether it's feasible (and, indeed, faster) to work directly with them rather than converting to Euler and back?

The implimentations I'm interested in would be setting random rotations within a set cone and finding a position from angle and rotation.
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
08-25-2005 18:12
Use Euler; save yourself a few headaches.

The efficiency probably wouldn't be noticeable.
AJ DaSilva
woz ere
Join date: 15 Jun 2005
Posts: 1,993
08-25-2005 18:14
I fully intend to use Euler, I expect I'd have heaps of trouble debugging otherwise. Like I said, I'm just curious about it.
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
08-25-2005 18:46
Well, personally, I don't quite understand how quaternions work. Or perhaps a better thing to say is that I don't understand WHY they work.
AJ DaSilva
woz ere
Join date: 15 Jun 2005
Posts: 1,993
08-25-2005 18:52
Heh, from what I've read it's something akin to transcending dimensions and becoming a being of pure energy. :D
Online Doesburg
absurd hero
Join date: 6 Jul 2005
Posts: 53
08-25-2005 20:08
The only thing I know regarding quaternions is that they are independent from the order, in which rotations are performed, while Euler rotations are supposedly not, and that that's the main reason they are used in computer graphics to specify rotations.

I think the best explanation of them I've heard is: it's an axis in 3d space (as a normalized vector) and a rotation about this axis, but I have no idea if that's a good or even correct explanation.

I picked up a Math encyclopedia in bookstore recently and looked it up, ... and put it down quickly. Maybe one day I'll read more about them.
Nyoko Salome
kittytailmeowmeow
Join date: 18 Jul 2005
Posts: 1,378
don't know if this'll help...
08-25-2005 21:45
it's been awhile since i've played with them in realbasic... i imagine that the same would apply here, but it could take some experimentation. i wanted to keep eulers for prefs storage; if i read you right, this was what you wanted to do it too...

but once you input them into a quat, you have to jump hoops to get eulers back out... and once a quat is set, it seems that, for controlling purposes, it wants almost like vectors for new angles, instead of the full eulers again: .5r,-.1p,-.2y, then your vector of distance since last frame.

so if you keep plugging in, say, 25,30,45 - your 'absolutes', you'll be spinning your object like no tomorrow. what i did was use my euler prefs as my base, then created/reset a quat to 0,0,0,0, and set the object to that to reset its orientation, then reinput my eulers.

that may not be the most efficient way there; perhaps compare this frame's eulers to the last frame, and just input the difference. again, this was all with realbasic, but i think quats shouldn't change their behavior between languages... hope that helps!! :)
_____________________

Nyoko's Bodyoils @ Nyoko's Wears
http://slurl.com/secondlife/Centaur/126/251/734/
http://home.comcast.net/~nyoko.salome2/nyokosWears/index.html

"i don't spend nearly enough time on the holodeck. i should go there more often and relax." - deanna troi
Seifert Surface
Mathematician
Join date: 14 Jun 2005
Posts: 912
08-26-2005 00:37
From: Online Doesburg
The only thing I know regarding quaternions is that they are independent from the order, in which rotations are performed, while Euler rotations are supposedly not, and that that's the main reason they are used in computer graphics to specify rotations.


The order of operations matters with quaternions as well, because it matters with real rotations too. Try this:

Take a book and hold it with the front cover facing you, right way up. We're going to do two rotations in a row, then do them the other way round, and we should see that you get different results when you do them in the two different orders.

The two rotations are going to be:

1. rotate anti-clockwise by 90 degrees along an axis along your line of sight.
2. rotate away from you by 90 degrees along an axis parallel to your chest. If you're riding a bike, your hands are gripping the axis I'm thinking of.

Ok, so lets do 1 first then 2. We start with the front cover facing you, right way up. After 1 the front is still facing us but the spine is now pointing down rather than to the left. After 2 you should now be looking at the spine of the book.

Now reset back to the book with cover facing you, right way up. When you do 2, you should now be looking at the edges of the bottoms of the pages. The spine is still pointing to the left. Now do 1, and you're still looking at the edges of the bottoms of the pages, but now the spine is pointing up.

So, you get a different thing. Rotations themselves care about what order you do them in, so any way for a computer to represent them must also.

As for understanding quaternions as used in SL, looking up quaternions in a book probably isn't your best bet. Quaternions are like complex numbers but more, erm, complex... and what you really want to know to be able to use them in SL is not what they are so much as how you can use them. To that end, knowing a bit about symmetries and how they form a group would be far more useful. The things you actually want to do with quaternions in SL are generally just "rotate this vector by this rotation" or "combine this rotation with that rotation", and for those all you need to understand is how rotations form a group.
_____________________
-Seifert Surface
2G!tGLf 2nLt9cG
Apotheus Silverman
I write code.
Join date: 17 Nov 2003
Posts: 416
08-26-2005 10:04
Quaternions that represent rotation (they can represent other things) really aren't that odd if you can find a decent visual illustration of how they work. In short, it's a modified <x,y,z> axis vector followed by an angle of rotation around that axis.

http://en.wikipedia.org/wiki/Quaternion#Rotation_group
Here is a very good explanation if you can grasp the extensive amount of jargon on that page.

http://secondlife.com/badgeo/wakka.php?wakka=quaternions
This page also explains how they are relavent in SL and some tricks to make them easier to work with.

Edit: Once I learned how they work and did some experimentation, I have personally found it easier to work directly with quaternions than converting back and forth between Euler angles. Note there will also be some precision loss (drift) in the floating point operations when converting back and forth a lot.
_____________________
Apotheus Silverman
Shop SL on the web - SLExchange.com

Visit Abbotts Aerodrome for gobs of flying fun.
Online Doesburg
absurd hero
Join date: 6 Jul 2005
Posts: 53
08-26-2005 11:33
http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

This page seems nice too, and explains some of the uses (and reasons for the use of) quarternions in computer graphics. Still pretty tough stuff ;) Fascinating, though!
AJ DaSilva
woz ere
Join date: 15 Jun 2005
Posts: 1,993
08-27-2005 08:44
Sounds like it might be something worth looking into when I've got some free time. Don't know if I'll ever understand all the math behind it (the stuff on those Wikipedia pages went right over my head) but from what Apotheus and Online have said I reckon I should be able to figure out how to use them in practical situations. Once I've had a chance to play with them a bit I'll see if I can't post some kinda diagram or something for peeps like me.

Nyoko, I'm affraid you got the wrong end of the stick. What I was aiming for was an idea of whether I could get rid of Eulers from my script all together. At the moment I generate a random rotation in Eulers then convert it to quaternions, if I could generate it straight off as quaternions I'd feel somehow happier about it. I doubt it'd be noticeably quicker but it'd make my code a little easier on the eye at least. :)

From: Online Doesburg
The only thing I know regarding quaternions is that they are independent from the order, in which rotations are performed, while Euler rotations are supposedly not, and that that's the main reason they are used in computer graphics to specify rotations.

Seifert, if I understand properly, I think what Online was refering to here is how a Euler rotation is actually 3 rotations one after another whereas a quaternion is only one rotation. Or something like that anyway...