LookAt() functionality for FPS (Euler Angle) Camera

Started by
14 comments, last by Wavesonics 14 years, 1 month ago
Quote:Original post by Wavesonics
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).
Your 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.
Advertisement
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!

#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;}
Aahhh that makes a whole lot of sense now, thanks guys.
==============================
A Developers Blog | Dark Rock Studios - My Site
Hhhhmmm... Ok, I tweaked a few things and it seems to work. But maybe not 100%, i'm not sure:
    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.
==============================
A Developers Blog | Dark Rock Studios - My Site
This:
float r = sqrt( v.x*v.x + v.y*v.y );
Should be:
float r = sqrt( v.x*v.x + v.z*v.z );
Fantastic! It works perfectly now, thank you all for your time :D
==============================
A Developers Blog | Dark Rock Studios - My Site

This topic is closed to new replies.

Advertisement