Sign in to follow this  
jimnovy

Quaternion Problems

Recommended Posts

jimnovy    122
Hi there, I'm attempting to use quaternions for the rotation of ships in a 3d Descent-like game. Here's the issue I'm having: I rotate/multiply/whatever the quaternion, turn it into a matrix, apply it as the viewpoint, and then translate the viewpoint back back with no problems, but when I then apply the matrix a second time to align the player's ship with the camera, it adds to the rotation instead of negating it. I've tried rotating a second quaternion in the opposite direction and applying that, but that doesnt seem to help much except while rotating about a vertical axis. As soon as a roll, or try to pitch up it screws up. I don't really understand the math too well, and can't seem to figure much out from the gamedev articles, so if you could either point out a flaw in my thinking or point me to an example/tutorial of some sort I would greatly appreciate it. Thanks, Jim

Share this post


Link to post
Share on other sites
Palidine    1315
post this code:
Quote:

apply it as the viewpoint, and then translate the viewpoint back back with no problems, but when I then apply the matrix a second time to align the player's ship with the camera, it adds to the rotation instead of negating it


-me

Share this post


Link to post
Share on other sites
jimnovy    122
void CShip::positioncamera()
{
GLfloat rotMatrix[ 16 ];
qcamorientation.getMatrix( rotMatrix );
glMultMatrixf( rotMatrix );


CVector heading = qcamorientation.getDirectionVector();

glTranslatef( -position.x-3*heading.x, -position.y-3*heading.y, -position.z-3*heading.z );
}

void CShip::draw()
{
glPushMatrix();
glEnable(GL_LIGHTING);
glTranslatef(position.x,position.y,position.z);
GLfloat rotMatrix[ 16 ];
qcamorientation.getMatrix( rotMatrix );
glMultMatrixf( rotMatrix );
glScalef(.005,.005,.005);
model.Draw();
glDisable(GL_LIGHTING);
glPopMatrix();

}

Share this post


Link to post
Share on other sites
Palidine    1315
oh rite. if you've already positioned the camera and you know the ship is going to be in the same place don't do any extra rotation on the ship. you've already moved the world so that the camera is in the right place. drawing the ship is easy, you just need to draw it without any transformations and it should show up in the right place. drawing everything else is, IMHO, confusing the way you've done it. you need to transform everything else into camera local space and draw it there, rather than drawing it where it's supposed to be.

the, again IMHO, easier way to do things is to use gluLookAt to position the camera. this will transform the appropriate matrices so the camera appears where you want it to be. then you can just draw everything else without any additional transformations.

-me

Share this post


Link to post
Share on other sites
jimnovy    122
The second way sounds a whole lot easier. =P I've got a function that can extract a heading vector from the quaternion, so I'd guess I add that to the position to get the view vector, but how do I get an up vector for the gluLookAt funct?

Share this post


Link to post
Share on other sites
Palidine    1315
if you can get the view out of the quaternion you can get the up out of it as well. if you can't find happy docs for specific quaternion examples, convert the quat to a matrix and extract the view & up from there. you should be able to find plenty of info on how to extract orientation vectors from a quaternion.

actually just take whatever your default forward vector is and postmultiply it by the quaternion, it'll give you the forward.

then do the same with whatever is your default up vector (typically 0,0,1) though in openGL it may be (0,1,0) i forget.

-me

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I don't mean to be mean, but that's precicely your problem. If you simply copy/paste code here and there, you'll never be able to achieve as much as someone who actually understand what's going on. Do you even need quaternions in the first place? There's other ways to rotate besides quaternions! I would strongly suggest that you get a book which can help you undertsand what's hapenning. You can easilly do a search on amazon for something like "mathematics game" and you'll get a list of books available on the subject. Since I've lookde into it myself not so long ago, you can also check overstock.com It has a book which is unbelieveably cheap and recent "Mathematics for Game Developers".

Share this post


Link to post
Share on other sites
jimnovy    122
After playing around with it for a little while I got it to work with the gluLookAt() function, but it demonstrates the same behavior it did before. =( Here's what I've got:

void CShip::positioncamera()
{
CQuaternion qup = qcamorientation;
qup.postMult( CQuaternion( 0, 1, 0 ) );
CVector heading = qcamorientation.getDirectionVector().normalize();
CVector up = qup.getDirectionVector();
CVector camposition = CVector(position.x-3*heading.x, position.y-3*heading.y, position.z-3*heading.z);
CVector camview = position;
gluLookAt(camposition.x,camposition.y,camposition.z,camview.x,camview.y,camview.z,up.x,up.y,up.z);
}

void CShip::draw()
{
glPushMatrix();
glEnable(GL_LIGHTING);
glTranslatef(position.x,position.y,position.z);
GLfloat rotMatrix[ 16 ];
qcamorientation.getMatrix( rotMatrix );
glMultMatrixf( rotMatrix );
glScalef(.005,.005,.005);
model.Draw();
glDisable(GL_LIGHTING);
glPopMatrix();
}

Share this post


Link to post
Share on other sites
jimnovy    122
Quote:
Original post by Anonymous Poster
I don't mean to be mean, but that's precicely your problem. If you simply copy/paste code here and there, you'll never be able to achieve as much as someone who actually understand what's going on. Do you even need quaternions in the first place? There's other ways to rotate besides quaternions! I would strongly suggest that you get a book which can help you undertsand what's hapenning. You can easilly do a search on amazon for something like "mathematics game" and you'll get a list of books available on the subject. Since I've lookde into it myself not so long ago, you can also check overstock.com It has a book which is unbelieveably cheap and recent "Mathematics for Game Developers".


I've tried using eular angles, which seemed impossibly difficult when rotating around arbitrary axises, and, and polar coors, which dont have roll information. I don't copy/paste code for everything, but I'll admit that when I come across something I don't understand and can't figure out after researching and coding for a couple hours I generally try to make example code work. I'm definately still beginning and am overwhelmed by the vast amount of stuff you need to know to make something kinda-sorta work right. As far as the book thing goes, thats why I was asking not only for specific help but for tutorials. I've found a lot of basic OpenGL/Direct3D tutorials, and a lot of more advanced stuff, but nothing that lies where I'm at. I'm kinda broke and can't afford to spend $30-$40 every time I've encounter a problem. That's why I'm asking for suggestions

Share this post


Link to post
Share on other sites
Dmytry    1151
I wrote small program that among other things does quaternion rotation, and there was a thread about it...
(and also,with sources)
edit:if you will use it,post yout FPS to thread :) it's not too old so it's not a necro.

Share this post


Link to post
Share on other sites
__Daedalus__    480
I am not an expert on this but are you using a left handed system? I think quaternions are right handed so you may need to take the inverse of it to make it left handed (if that's applicable).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this