Pr0gMa

11-02-2014, 02:56 AM

Hi there.

I'm completely new to the whole openGL thing, so please forgive the dumbness of my question; on top of that, I'm no English native speaker, so please pardon my writing skills too.

What I'm trying to do is implementing a sort of FPS camera you can move using the keyboard, and these are the lines of codes concerned I've written so far.

const double PI = 3.14159265;

const double RAD = PI / 180;

const double roaming_step = .2;

const double angle_step = 1.5;

double angleXZ = .0;

double angleYZ = .0;

const double look_at_offset = 9.0;

double x_pos = .0;

double y_pos = 3.0;

double z_pos = 9.0;

double x_look_at = .0;

double y_look_at = 3.0;

double z_look_at = z_pos - look_at_offset;

bool keyStates[256] = { false }; // used to keep track of pressed buttons

void keyboardOperations()

{

if(keyStates['a'])

{

x_pos -= roaming_step * cos(angleXZ * RAD);

z_pos -= roaming_step * sin(angleXZ * RAD);

x_look_at -= roaming_step * cos(angleXZ * RAD);

z_look_at -= roaming_step * sin(angleXZ * RAD);

}

if(keyStates['d'])

{

x_pos += roaming_step * cos(angleXZ * RAD);

z_pos += roaming_step * sin(angleXZ * RAD);

x_look_at += roaming_step * cos(angleXZ * RAD);

z_look_at += roaming_step * sin(angleXZ * RAD);

}

if(keyStates['w'])

{

x_pos += roaming_step * sin(angleXZ * RAD);

z_pos -= roaming_step * cos(angleXZ * RAD);

x_look_at += roaming_step * sin(angleXZ * RAD);

z_look_at -= roaming_step * cos(angleXZ * RAD);

}

if(keyStates['s'])

{

x_pos -= roaming_step * sin(angleXZ * RAD);

z_pos += roaming_step * cos(angleXZ * RAD);

x_look_at -= roaming_step * sin(angleXZ * RAD);

z_look_at += roaming_step * cos(angleXZ * RAD);

}

if(keyStates['o'])

{

angleXZ += angle_step;

if(angleXZ >= 360.0)

angleXZ -= 360.0;

x_look_at = x_pos + look_at_offset * sin(angleXZ * RAD);

z_look_at = z_pos - look_at_offset * cos(angleXZ * RAD);

}

if(keyStates['i'])

{

angleXZ -= angle_step;

if(angleXZ < .0)

angleXZ += 360.0;

x_look_at = x_pos + look_at_offset * sin(angleXZ * RAD);

z_look_at = z_pos - look_at_offset * cos(angleXZ * RAD);

}

}

void display(void)

{

keyboardOperations();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

gluLookAt(x_pos, y_pos, z_pos,

x_look_at, y_look_at, z_look_at,

.0, 1.0, .0);

// ...stuff...

glutSwapBuffers();

}

For some reason, rotating my camera around the X and Y axis leads to strange behaviors: everything seems to work fine at the beginning, but it starts to act oddly very soon. Instead, it works perfectly if I limit the rotation around only one of the two axis.

I think the problem is somehow related to 'z_look_at', but I have no idea how to solve it. Any suggestion?

Thank you for your attention.

I'm completely new to the whole openGL thing, so please forgive the dumbness of my question; on top of that, I'm no English native speaker, so please pardon my writing skills too.

What I'm trying to do is implementing a sort of FPS camera you can move using the keyboard, and these are the lines of codes concerned I've written so far.

const double PI = 3.14159265;

const double RAD = PI / 180;

const double roaming_step = .2;

const double angle_step = 1.5;

double angleXZ = .0;

double angleYZ = .0;

const double look_at_offset = 9.0;

double x_pos = .0;

double y_pos = 3.0;

double z_pos = 9.0;

double x_look_at = .0;

double y_look_at = 3.0;

double z_look_at = z_pos - look_at_offset;

bool keyStates[256] = { false }; // used to keep track of pressed buttons

void keyboardOperations()

{

if(keyStates['a'])

{

x_pos -= roaming_step * cos(angleXZ * RAD);

z_pos -= roaming_step * sin(angleXZ * RAD);

x_look_at -= roaming_step * cos(angleXZ * RAD);

z_look_at -= roaming_step * sin(angleXZ * RAD);

}

if(keyStates['d'])

{

x_pos += roaming_step * cos(angleXZ * RAD);

z_pos += roaming_step * sin(angleXZ * RAD);

x_look_at += roaming_step * cos(angleXZ * RAD);

z_look_at += roaming_step * sin(angleXZ * RAD);

}

if(keyStates['w'])

{

x_pos += roaming_step * sin(angleXZ * RAD);

z_pos -= roaming_step * cos(angleXZ * RAD);

x_look_at += roaming_step * sin(angleXZ * RAD);

z_look_at -= roaming_step * cos(angleXZ * RAD);

}

if(keyStates['s'])

{

x_pos -= roaming_step * sin(angleXZ * RAD);

z_pos += roaming_step * cos(angleXZ * RAD);

x_look_at -= roaming_step * sin(angleXZ * RAD);

z_look_at += roaming_step * cos(angleXZ * RAD);

}

if(keyStates['o'])

{

angleXZ += angle_step;

if(angleXZ >= 360.0)

angleXZ -= 360.0;

x_look_at = x_pos + look_at_offset * sin(angleXZ * RAD);

z_look_at = z_pos - look_at_offset * cos(angleXZ * RAD);

}

if(keyStates['i'])

{

angleXZ -= angle_step;

if(angleXZ < .0)

angleXZ += 360.0;

x_look_at = x_pos + look_at_offset * sin(angleXZ * RAD);

z_look_at = z_pos - look_at_offset * cos(angleXZ * RAD);

}

}

void display(void)

{

keyboardOperations();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

gluLookAt(x_pos, y_pos, z_pos,

x_look_at, y_look_at, z_look_at,

.0, 1.0, .0);

// ...stuff...

glutSwapBuffers();

}

For some reason, rotating my camera around the X and Y axis leads to strange behaviors: everything seems to work fine at the beginning, but it starts to act oddly very soon. Instead, it works perfectly if I limit the rotation around only one of the two axis.

I think the problem is somehow related to 'z_look_at', but I have no idea how to solve it. Any suggestion?

Thank you for your attention.