Archived

This topic is now archived and is closed to further replies.

Last Attacker

Quaternion Camera

Recommended Posts

Hey all! I''ve tried to code my own Quaternion Camera but to no success. I have downloaded a few sample codes and tutorials on the subject but allmost none of them can perform the rotation that can prevent the "Gimbal-lock". I''ve been working on this for days now but no success. Please I NEED HELP! To move the camera is no problem but to rotate it correctly on the X, Y and Z axis, I really need help. How can I also perform a 3rd person view with Quaternions also in all 3 rotations. Thanks! Any help is appreciated. // Last Attacker \\---=( LA )=---// LA Extreme \\

ICQ Number : 120585863

E-mail: laextr@icqmail.com

Share this post


Link to post
Share on other sites
Hello there: this is the code I use to rotate for my Quaternion Camera. It is done in JOGL (Java).


public void rotate(double speed, Vector3 vAxis) {
Quaternion qRot = new Quaternion();
Quaternion qNewDir = new Quaternion(vView.sub(vPosition));
Quaternion qNewRot = new Quaternion();
qRot = qRot.buildFromAxisAngle(speed, vAxis);
qNewRot = qRot.mult(qNewDir).mult(qRot.conjugate());
vView = vPosition.add(qNewRot.v);
}

public void orbit(Vector3 vCenter, double speed, Vector3 vAxis) {
Quaternion qRot = new Quaternion();
Quaternion qNewPos = new Quaternion();
Vector3 vTmp = vPosition.sub(vCenter);
Quaternion qPos = new Quaternion(vTmp);
qRot = qRot.buildFromAxisAngle(speed, vAxis);
qNewPos = qRot.mult(qPos).mult(qRot.inverse());
Vector3 v = qNewPos.v;
vPosition = vView.add(v);
}


It works fairly well, except in orbiting around a point on the X Axis. I still haven''t been able to find a solution.
The problem is that if I''m looking North (for example), it rotates OK making a sort of circular path around the object (passing above and then beneath). If I move myself to the right, and try to orbit on the X-Axys, it still makes a circular path.. The problem is that these circular path are parallel.

Let me explain better: if I''m looking at an object from south to north it rotates in a circular path around the object. If I position myself on the right side so that I''m looking at the object from the east to the west.. it makes a circular path parallel to the first. In theory from east->west it should make a circular path that intersects the previous one, right? The viewing directiong remains centered anyway (so that when I''m orbiting it still look at the same point).

I''m sure it''s an easy thing to fix.. But I really don''t know.. any help would be really appreciated.

Share this post


Link to post
Share on other sites
hehe, one day i''ll count the operations needed for a quat and a matrix camera, just to see if there is any reason for one at all. i wouldnt be surprised if all this back and forth and using glulookat etc. is just more work and less intuitive.

question is: do you want to rotate around _global_ or _local_ x,y,z axes? also, be careful to make sure those tutorials arent some of those "lets use quaternions and still use euler angles and wonder why it still cant work, because flawed math is flawed math, no matter what kind of space youre doing it in". dressing up euler angles in quaterions instead of matrices to apply rotations will simply not make any difference.

btw. i just learned to appreciate the ability to overload implicit typecasting. passing a vector class to opengl functions like it was a float* (ie. glVertex3fv(V)) instead of passing x,y,z seperatly or using ugly ways (ie glVertexfv(V.x) or glVertexfv((float*)&V)) and not to mention the even uglier c++ typecasts *fg*) can make code so nice and clean. i just wish it had options to add new operators (never find a good for cross product and using & can be confusing for someone to read *fg*). but: better start using a lot of brackets to make sure it wont screw up the order (compiler cant know if your own operator x is binding stronger or weaker than operator y)

Share this post


Link to post
Share on other sites
If you can show me how to perform all those above mentioned rotations (around the camera''s axis and/or orbitally) without Quaternions, then I''ll drop that and use something simpler and faster.

I can easily code a camera thats like a first person shooter cam where you can look up/down , etc. But I want a camera that can do almost anything I want to.

So until then I''m using Quaternions.


// Last Attacker \\---=( LA )=---// LA Extreme \\

ICQ Number : 120585863

E-mail: laextr@icqmail.com

Share this post


Link to post
Share on other sites
well, you want to rotate either around your objects right/up/front (which is exactly what glrotate does, the only "trick" is to store this matrix and only update it with the new rotations each frames) or around the global x,y,z axes, which means either letting the matrices switch place (easy) or rotating around the axis you get by inverting the matrix (extra work).

for opengl to rotate you just do this (do the matrix math yourself if you do it a lot each frame as reading back the matrix from the card might be a little slow if you do it hundreds of times):
void CameraGL::rotate(float deg, float x, float y, float z, bool global) {
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadMatrixf(Transform);

if (global) glRotatef(deg,
x*Transform[0] + y*Transform[1] + z*Transform[2],
x*Transform[4] + y*Transform[5] + z*Transform[6],
x*Transform[8] + y*Transform[9] + z*Transform[10]);
else glRotatef(deg, x,y,z);
glGetFloatv(GL_MODELVIEW_MATRIX, Transform);
glPopMatrix();
}

for your quat camera you probably store view/up and position vectors, which is just what you find in the matrix (plus a "right" vector that can be handy to strafe). if you dont like storing 16 floats you can get away with 9 (view,up,position) but have to get the right vector via cross product and add w components, which is btw. just what glulookat does.

Share this post


Link to post
Share on other sites
Thanks Trienco!

The only thing I don''t like about gluLookAt is that you cannot rotate above or below the 90'' up or down angles without invertically jumping to another direction.

I guess you have to rotate the Up Vector but I don''t know how or if it will help.

Thanks

// Last Attacker \\---=( LA )=---// LA Extreme \\

ICQ Number : 120585863

E-mail: laextr@icqmail.com

Share this post


Link to post
Share on other sites
hm.. what i dont like about glulookat is that with a matrix camera you would basically first convert your perfect vectors into the form glulookat is expecting, just so it can turn them back into what you already had (with two completely unnecessary cross products etc.). and without glulookat i even removed the last glu function.

but one way or another, you need at least TWO vectors and trying to get away with just the view vector will bite you sooner or later ,-)

Share this post


Link to post
Share on other sites