PDA

View Full Version : 3D Math Problem


Eriond
2006.07.23, 09:41 PM
Okay... I'm trying to write a Space Sim in C++ and OpenGL, and I've hit a snag.

I've got three axes upon which I can turn my camera, x, y and z, or, respectively, pitch, yaw and roll.

Now, I've adjusted for roll (z), so that when you press the arrow keys, you turn left, right, up and down, regardless if it actually is absolute up, left, right or down. However, when I pitch (x) my camera up and change my yaw, the camera moves as if it's mapped to a sphere, moving along a parallel of yaw (x) degrees.

Now, I rotate my camera in such a way that when I press the left/right arrow keys on a pitched camera, it doesn't move along a parallel, but moves in a large cricle, equivalent to the equator of this 'sphere' that the camera seems to be mapped to, moving down, then when it's at 180 degees around the y axis, moving up again.

Sorry if this is a little complicated, but it's hard to describe exactly what I need.

So, basically I need to know what to do with the values of yaw and pitch whenever I press the left/right arrow keys. I just can't seem to wrap my head around it.

Thanks in advance :)
Eriond

ThemsAllTook
2006.07.23, 10:22 PM
The problem is a classic. One of the common solutions is to represent rotations using quaternions.

See my tutorial here: http://www.sacredsoftware.net/tutorials/Quaternions/Quaternions.xhtml

ia3n_g
2006.07.23, 11:05 PM
The OpenGL book I am reading recommends storing your frames of reference as three vectors: location, direction, and an up vector. Then you just rotate them by matrix multiplication.

OneSadCookie
2006.07.23, 11:22 PM
matrices have a habit of getting messy. I'd definitely go with quaternions.

Eriond
2006.07.23, 11:56 PM
Ah! But rotation itself is not a problem... I think... uh... OpenGL handles all the calculations for me. The problem is finding out how much rotation I need whenevr I press the arrow key... or am I just confusing myself :blink: I've looked into Quaternions before, along with Matrices... but I didn't think it'd help, because with matrices, I stll need to know WHAT the new axes I'm making are, becuse i'm not jsut increasing each by a degrees, I'm increasing them in proportion to SOMETHING.

I dunno.. are you sure Quaternions are what I need?

unknown
2006.07.23, 11:57 PM
you are confused, learn about what causes gimbal lock.

Eriond
2006.07.24, 12:12 AM
Ook. Will do. Thanks guys :) I owe you one :)

Eriond
2006.07.26, 11:49 PM
Well, it appears I have a problem. I read up on quaternions (thoroughly), and sort of understand them. I implemented what was shown in the excellent tutorial given by ThemsTookAll (thanks, btw :P) and I'm having somewhat of a similar problem. I can't really grasp the underlying fundamentals of quaternions, so I'll have to describe what happened graphically, as best I can.

Basically, I've taken three vectors, [1,0,0] [0,1,0] [0,0,1], and then taken the rotation (in radians) that I want to preform around them.

I convert these axes + rotations to quaternions (using the formulas in the tutorial), multiply them all together, convert it to a matrix, then load it to OpenGL.

Now, I'm essentially back to where I was earlier. Well, almost. I took out the corrections I had implemented for the camera, and now Quaternions seem to do that instinctively, but the problem still remains. When I roatate along the y axis, I move along the parallel of an upright sphere instead of the equator of a rotated one.

So... gimbal lock is NOT the problem in this case.... anyone else have an idea?

But you've been great so far guys! I appreciate the quaternion help :)

unknown
2006.07.27, 12:02 AM
multiply them all together, convert it to a matrix, then load it to OpenGL.
WOAH woah wait what?
you dont do that.. you want to apply rotations to vectors and keep the axis seperate!

to apply a rotation as described by a quaternion (q) to a vector (v) you do this

make a quaternion from v like this:
w = v.x*i + v.y*j + v.z*k
create a new quaternion by conjugating w by q,
w' = q*w*q'
then w' will be of the form
w = a*i + b*j + c*k
and you can just create a vector (a, b, c) from that.

q' is the conjugate (http://mathworld.wolfram.com/QuaternionConjugate.html) of q

also see this document http://www.geometrictools.com//Documentation/Quaternions.pdf for the only information about quaternions ive been able to understand yet.

Eriond
2006.07.27, 01:03 AM
WOAH woah wait what?
you dont do that.. you want to apply rotations to vectors and keep the axis seperate!

to apply a rotation as described by a quaternion (q) to a vector (v) you do this

make a quaternion from v like this:
w = v.x*i + v.y*j + v.z*k
create a new quaternion by conjugating w by q,
w' = q*w*q'
then w' will be of the form
w = a*i + b*j + c*k
and you can just create a vector (a, b, c) from that.

q' is the conjugate (http://mathworld.wolfram.com/QuaternionConjugate.html) of q

also see this document http://www.geometrictools.com//Documentation/Quaternions.pdf for the only information about quaternions ive been able to understand yet.

But I thought that when you multipled quaternions together, you melded all their transformations into one? Anyway, I'll check out the doc now... and I'll re-read that tutorial.

EDIT:

Yeah... You can multiply two quaternions together as shown in the following code. The effect of this is to concatenate the two rotations together into a single quaternion.. Isn't that what I'm trying to do?

OneSadCookie
2006.07.27, 01:17 AM
I don't understand why unknown took such a violent objection to what you said :|

aarku
2006.07.27, 01:24 AM
you are confused, learn about what causes gimbal lock.
You should politen up your posts. I'm sure you don't mean to be rude but it can easily come across that way in text only. The forum has a higher established level of formality than that of the IRC channel. In my opinion your posts are along the lines of IRC/IM formality. Nothing to cause alarm, just please keep it in mind, and keep on trucking.

-Jon

AnotherJake
2006.07.27, 03:18 AM
I can't really grasp the underlying fundamentals of quaternions...
Nobody can. All you can do is try to understand how to *use* them. It's tricky for the uninitiated. Sometimes those of us who `get it' (or even just barely grasp the fundamentals like me) can't understand why those who can't can't. That's just typical geek crap. Just keep researching. It's a good mind-twister, and well worth the effort. Quaternions are a jewel of a black magical box in the realm of 3D. It's good shit. You're in a small club. Enjoy the ride!

unknown
2006.07.27, 08:12 AM
when you multipled quaternions together, you melded all their transformations into one?

This is true, but I dont think its what you wanted to do, if you had quaternions q1, and q2 then you would do:

w = q2*q1*w*q1'*q2'
which happens to equal
w = q2*q1*w*q2'*q1'
which happens to equal
w = (q2*q1)*w*(q2*q1)'

which is shown in the document I posted a link to.

You should politen up your posts.
Yes I will, but what I said looks a lot less rude in context.

Nobody can
Sure you can, it just takes several days of searching to find enough information. The stuff on quaternions is very scares compared to that on Vectors and similar subjects, I think it has somthing to do with how abstract they are.

OneSadCookie
2006.07.27, 08:27 AM
personally, once I understood how the axis & angle are stored in the quaternion, they started making sense. Not perfect sense, and I won't claim to intuitively understand why their operations work exactly the way they do, but more sense than "just use the magic formulae" :)

ThemsAllTook
2006.07.27, 09:56 AM
Eriond: You've fallen into a common trap. What you're essentially doing is using quaternions in the same way as euler angles, which still gives you gimbal lock. To use quaternions effectively, you'll have to shift your way of thinking about rotations by a little bit...

What you'll probably want to do is represent all of your rotations as a single axis/angle pair. No matter how you go about it, if you're rotating around each of the three axes separately, you'll run into problems. A single axis/angle pair can describe any possible rotation; a sequence of three rotations around a fixed set of axes can't.

Maybe I'll add something about this to my tutorial.

unknown
2006.07.27, 10:19 AM
I just want to point out:
According to Euler's rotation theorem, any rotation may be described using three angles.

AnotherJake
2006.07.27, 11:28 AM
Sure you can, it just takes several days of searching to find enough information.
Mathematically sure. Conceptually no. What I was trying to communicate is that you can't actually understand them in the sense that they are four-dimensional. We live in a three-dimensional reality. Mentally it is easy to visualize what is happening with Euler angles. It is impossible with quats because of the extra dimension.

OneSadCookie
2006.07.27, 06:16 PM
A quaternion ultimately represents an axis and an angle, which is perfectly easily visualized in 3D...

kelvin
2006.07.27, 06:42 PM
Mathematically sure. Conceptually no. What I was trying to communicate is that you can't actually understand them in the sense that they are four-dimensional. We live in a three-dimensional reality. Mentally it is easy to visualize what is happening with Euler angles. It is impossible with quats because of the extra dimension.
It's easy to understand if you take it down one dimension...
Imagine a 2D plane with two equal length vectors A and B on it.
Now to rotate from vector A to vector B you can either sweep across the plane (ala yaw), or you can rotate out of the plane into the 3rd dimension.

Now imagine a half angle between vector A and B. Use this as the rotational axis. You rotate vector A around the half-angle axis into the third dimension, then continue rotating until you arrive at vector B back on the plane in 2D. If you abstract that up to 3D, then you have a quaternion. The quaternion rotates a 3D vector half way into 4D, then back down into your new desired 3D vector.

AnotherJake
2006.07.27, 06:51 PM
That's an interesting way to look at it kelvin! Abstracting 2D into 3D I can do. However, I still can't get my mind to abstract 3D into 4D. I just can't do it. Sorry.

A quaternion ultimately represents an axis and an angle, which is perfectly easily visualized in 3D...

Yeah, but it is an axis and an angle represented by x, y, z, w... Are you suggesting that you can clearly understand what each of those values of a quaternion mean at any given time? In 4D space?

[adding] I understand how quaternions work from the standpoint that adding the extra dimension allows us to solve 3D rotations without those axes implicitly constraining each other. I simply cannot visualize what is happening after I convert a 3D axis and angle into the fourth dimension. Once it gets converted back I'm happy though :)

OneSadCookie
2006.07.27, 07:00 PM
x, y, z are the axis (scaled by the sine of half the angle) and w is 1 (scaled by the cosine of half the angle). So no, just by looking at the values, you don't necessarily know immediately what they mean, but you're only a divide, an inverse sine and a multiply away from something that's easily intelligible :)

AnotherJake
2006.07.27, 07:13 PM
That's what I'm saying. The human mind just wasn't designed to navigate 3D space in terms of four dimensions. But I see your angle (pun intended). It really is just an axis and an angle when it comes down to it. It's not like converting it to a quaternion turned it into pixie dust or something.

kelvin
2006.07.27, 07:27 PM
That's what I'm saying. The human mind just wasn't designed to navigate 3D space in terms of four dimensions. But I see your angle (pun intended). It really is just an axis and an angle when it comes down to it. It's not like converting it to a quaternion turned it into pixie dust or something.
I think the easiest way to go from 2D/3D to 3D/4D is to think of 3 axis aligned 2D planes. Kinda like a box with the top and 2 sides missing.

If you're familiar with dot products, then you know how to project your 3D vectors (A and B) onto each of those planes. Now, from here you can imagine each of those projections as analogous to my previous 2D->3D example. Each projected rotation will pop off of that respective plane into an orthogonal 3D space. So, if you have planes XY, XZ, and YZ. you can think of their normal direction (Z, Y, and X respectively) as part of the component W dimension. I don't have the math off hand but I'm pretty sure you can mangle those values together into a W component for the rotation into 4D.

AnotherJake
2006.07.27, 07:41 PM
Wow! I think I actually get that. :blink:

Now somebody needs to make a visualizer...