OpenGL Rotation Question
Hi, I''ve been learning OpenGL from the Red Book. I''ve been making simple 3-d worlds, however I can''t work out how to turn left or right. The only way I came change direction is by rotating the entire world around the y-axis. However when I do this, the entire world rotates around (0,0,0) and hence when the player is far away from the entire of the map, the ground moves from under the player''s feet because the player is just rotating around (0,0,0) at a constant radius.
What''s the easiest method of rotating the view from any particular position on the map??
Thanks,
Pointed (aka Evan Clark)
Rotate around the y-axis in the opposite direction then translate in the opposite direction. Here''s how I implemented movement in one of my programs:
// camera definitions
float xrot = 0.0f;
float yrot = -135.0f;
float zrot = 0.0f;
float xpos = 1.5f;
float ypos = 1.0f;
float zpos = 6.0f;
const float DEGTORAD = 0.0174532925199432957692369076848f;
if(keyboard_state[DIK_RIGHT])
yrot -= 0.5f;
if(keyboard_state[DIK_LEFT])
yrot += 0.5f;
if(keyboard_state[DIK_Z])
xrot -= 0.5f;
if(keyboard_state[DIK_A])
xrot += 0.5f;
if(keyboard_state[DIK_UP])
{
xpos -= sinf(yrot*DEGTORAD) * 0.05f;
zpos -= cosf(yrot*DEGTORAD) * 0.05f;
}
if(keyboard_state[DIK_DOWN])
{
xpos += sinf(yrot*DEGTORAD) * 0.05f;
zpos += cosf(yrot*DEGTORAD) * 0.05f;
}
glRotatef(-xrot, 1.0f, 0.0f, 0.0f);
glRotatef(-yrot, 0.0f, 1.0f, 0.0f);
glTranslatef(-xpos, -ypos, -zpos);
// camera definitions
float xrot = 0.0f;
float yrot = -135.0f;
float zrot = 0.0f;
float xpos = 1.5f;
float ypos = 1.0f;
float zpos = 6.0f;
const float DEGTORAD = 0.0174532925199432957692369076848f;
if(keyboard_state[DIK_RIGHT])
yrot -= 0.5f;
if(keyboard_state[DIK_LEFT])
yrot += 0.5f;
if(keyboard_state[DIK_Z])
xrot -= 0.5f;
if(keyboard_state[DIK_A])
xrot += 0.5f;
if(keyboard_state[DIK_UP])
{
xpos -= sinf(yrot*DEGTORAD) * 0.05f;
zpos -= cosf(yrot*DEGTORAD) * 0.05f;
}
if(keyboard_state[DIK_DOWN])
{
xpos += sinf(yrot*DEGTORAD) * 0.05f;
zpos += cosf(yrot*DEGTORAD) * 0.05f;
}
glRotatef(-xrot, 1.0f, 0.0f, 0.0f);
glRotatef(-yrot, 0.0f, 1.0f, 0.0f);
glTranslatef(-xpos, -ypos, -zpos);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement