Tuesday, March 22, 2011

Solution not found :(

Hi guys.
I've follow your suggestion about use quaternions to rotate my camera into the space and the upside/down problem that i had using the lookAt method of the Camera class, seems to be fixed
(here the previous video to show you)



Before i've used the spherical coordinates.. when you move the mouse in x direction i change the yaw angle, and when you move in y direction i change the pitch angle.
After modified the angles, i compute the unitary vector that represent the orientation of the camera:

   direction.x=sin(yaw)*sin(pitch);
direction.y=cos(pitch);
direction.z=sin(pitch)*cos(yaw);

Finally i use the "lookAt" method to orientate the camera along the "direction" Vector3.
But the side effect is that, when pitch angle go outside the interval (0,PI), the camera flips upside down.

So, i've changed the approach.. no more lookAt but quaternions.
void MyCamera::lookAt(Vector3* dir) {

//no more used
//mCamera->lookAt(dir->x,dir->y,dir->z);

Vector3 src = mCamera->getDerivedOrientation() * Vector3::NEGATIVE_UNIT_Z; // Orientation from initial direction. Camera points to (0,0,-1)
Quaternion quat = src.getRotationTo(*dir);
mCamera->rotate(quat);
}



And this is the result:



i've changed the starship mesh with a more clear xyz axis mesh: x is in red, y in green and z in blue.
First i try to do a yaw rotation, around the Y axis... it works fine.
Then pitch rotation and.. well! it works! no more upside-down.
But.. when i combine both and give freedom to camera move something strange happens: moving it cause a side effect: some roll rotations starts to appears and the more i rotate clockwise my mouse, the more the roll appears... and rotating the mouse in other direction changes the roll direction too..

Why??

And how can i remove it?

2 comments:

Yombo said...

That is because you don't give complete information about the rotation of the ship. With only one direction vector, you leave freedom to roll. You must store the quaternion as the rotation of the ship (not only the dir vector), and don't use any angles. Just the quat. Then every rotation of the ship you must do in form of a quaternion. Say your pilot is rotating the ship by angX in x axis and angZ in z axis (these are small angles in LOCAL ship coord. system). Then you construct two quats, one with angX and one with angY, and then multiply the state quaternion of the ship by these two quats, one by one, and you obtain the state quaternion of the next frame.

PdG said...

Thank Yombo.
I'd like to speak with you.. May I write you an email? I'm not so pratical about twitter and i think that 160 chars are too few to discuss about 3d graphics :)
My email is pdg.pdg@gmail.com