## 3D Math Problem

Eriond
Unregistered

Post: #1
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.

Eriond
Moderator
Posts: 1,559
Joined: 2003.10
Post: #2
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/...ions.xhtml
Member
Posts: 49
Joined: 2006.07
Post: #3
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.
Luminary
Posts: 5,139
Joined: 2002.04
Post: #4
matrices have a habit of getting messy. I'd definitely go with quaternions.
Eriond
Unregistered

Post: #5
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 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?
Sage
Posts: 1,403
Joined: 2005.07
Post: #6
you are confused, learn about what causes gimbal lock.

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Eriond
Unregistered

Post: #7
Ook. Will do. Thanks guys I owe you one
Eriond
Unregistered

Post: #8
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 ) 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
Sage
Posts: 1,403
Joined: 2005.07
Post: #9
Quote: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 of q

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

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Eriond
Unregistered

Post: #10
unknown Wrote: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 of q

also see this document http://www.geometrictools.com//Documenta...rnions.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...
Quote: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?
Luminary
Posts: 5,139
Joined: 2002.04
Post: #11
I don't understand why unknown took such a violent objection to what you said :|
Moderator
Posts: 522
Joined: 2002.04
Post: #12
unknown Wrote: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
Moderator
Posts: 3,567
Joined: 2003.06
Post: #13
Eriond Wrote: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!
Sage
Posts: 1,403
Joined: 2005.07
Post: #14
Quote: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.

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

Quote: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.

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Luminary
Posts: 5,139
Joined: 2002.04
Post: #15
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"