Quote:Original post by WavesonicsYour Cartesian-to-spherical conversion considers +z to be up, so if +y is up in your simulation you'll need to adjust the code accordingly.
Ah no my Z axis is not up, it's standard OpenGL coordinate system so -Z should be forward if I'm not mistaken, and +y is up.
Also how I'm applying the rotation is by generating a quaternion from the Euler Angles, then extracting a matrix from that (Just for convince at the moment).
LookAt() functionality for FPS (Euler Angle) Camera
Cool, it looks like you are close to solving the problem; just out of curiosity I want to post this code, which is a version that uses a length computed with 3 components. This is very close to your initial guess, the differences are (1) in the vector subtraction, as noted by jyk; (2) in the fact that it uses asin in place of acos; (3) in the fact that considers the OpenGL reference frame, which is right handed with y-axis being up.
Bye!
Bye!
#include <stdio.h>#include <math.h>int main(int argc, char *argv[]){ float x1 = -1; float y1 = -1; float z1 = 3; float position_x = 1; float position_y = 1; float position_z = 1; float v_x = x1 - position_x; float v_y = y1 - position_y; float v_z = z1 - position_z; float length = sqrt(v_x * v_x + v_y * v_y + v_z * v_z); float pitch = asin(v_y / length); float yaw = atan2(v_z, v_x); printf("Pitch = %d\n", (int)(180 * (pitch / 3.1415))); printf("Yaw = %d\n", (int)(180 * (yaw / 3.1415))); return 0;}
Hhhhmmm... Ok, I tweaked a few things and it seems to work. But maybe not 100%, i'm not sure:
Notice I reverse the values in yaw's atan2(), and I inverted the y value being passed into pitch's atan2()
It's the best it's ever been, but when i'm far away or at extreme angles, it doesn't seem to center the point exactly in the center of the screen.... Hhhhmmm...
[edit]
One thought I had here was when adding translation to the camera, i subtract it from the position, so the "position" is already inverted for translation.
void lookAt( float x, float y, float z ) { VectorR3 v( x, y, z ); v -= m_position; float r = sqrt( v.x*v.x + v.y*v.y ); float yaw = atan2( v.x, v.z ); float pitch = atan2( v.y*-1.0f, r ); setRotation( yaw, pitch, 0.0f, true ); }
Notice I reverse the values in yaw's atan2(), and I inverted the y value being passed into pitch's atan2()
It's the best it's ever been, but when i'm far away or at extreme angles, it doesn't seem to center the point exactly in the center of the screen.... Hhhhmmm...
[edit]
One thought I had here was when adding translation to the camera, i subtract it from the position, so the "position" is already inverted for translation.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement