Saturday, March 19, 2011

I need a little help.

I'm just stucked inside a small but annoying issue and i'm tired and unable to find an elegant solution.
So i'll post here my problem to see if there's someone could help me..


I'm working on orientation of our spaceship.
The ship has two vector (Vector3): the position and the orientation. The first is the position of the ship in space, the second should be a unitary vector that represent the "lookAt" orientation (in local coordinates).
If you're sitted on the cockpit, this represent the direction you're looking to.

To implement this i've used the spherical coords, so, to obtain the vector:


orientation.x=sin(fi)*sin(theta);
orientation.y=cos(theta);
orientation.z=sin(theta)*cos(fi);



where fi is the yaw angle, and theta is the pitch angle.

It works fine for yaw, but.. for pitch, when the angles become less below zero or greather than PI, the screens flips! (look at 0:17)




I think that the issue should be that when theta is outside (0,PI), sin(theta) is <0, and both x and z become negative, and in some ways it invert the up-right vectors.
You can see it into the video: on the upper-left panel there's the ship position and ship orientation (you should use 720p resolution to see the numbers)
For now i've limited the rotation from 0 to PI but it's ugly.. i'd like to pitch and yaw without restricts, i'd like to make loops with my ship without this glitch.
Any idea?

Thank you.

4 comments:

Anonymous said...

Pernso che nel tuo caso questo possa essere utile:

http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

PdG said...

uhm.. mi sembra un argomento piuttosto complesso.
In che modo pensi possano aiutarmi?
A me in fondo basta una formula che mi dica i vettori look at-up-right a partire dai 2 angoli pitch e yaw. In realta' sono tre ma non vorrei complicare troppo la cosa.

Yombo said...

The first commenter is right. Use of two angles can be well suited for a simple aerial simulator, but in a space simulator is more suitable the use of quaternions.
Quaternions can be hard, but you only need 2 or 3 functions to convert to/from matrices.
If you want some help for coding you can send me a DM on twitter @yombo

PdG said...

Thank you!
Indeed i'm studying the quaternions and i've made some simulation. And yes, it seems that should resolve my problem. I was able to take a mesh and orient it along any direction, using the quaternions.
So i think that if i made the same thing with the camera, it should be ok.
Anyway if i'll have question, i'll write to you! ;)