I''ve been doing a camera class to create motion for my characters through keyboard/mouse. It''s with OpenGL/Glut.
I get a wierd but in which when I move down on axis, the camera moves back on the forward arrow, and forward on the back arrow. I tought I just had a sign mixed up in my trig math, but the wierdest part is that when you turn 180 degrees on this abberation, you would expect to get the same error mirrored, but you don''t. It works fine were it logically shouldn''t. Here''s the code.
#ifndef Camera_h
#define Camera_h
#include "Vector.cpp"
class CCamera {
public:
CVector m_fVelocity;
CVector m_fPosition;
CVector m_fRotation;
public:
void OrientGl();
void Drift(float);
void Strafe(float);
};
#endif // Camera_h //
#ifndef Camera_cpp
#define Camera_cpp
#include <gl/gl.h>
#include <gl/glu.h>
#include "Camera.h"
#include "Trig.h"
void CCamera::OrientGl() {
glRotatef( -m_fRotation.x(), 1.0f, 0.0f, 0.0f );
glRotatef( -m_fRotation.y(), 0.0f, 1.0f, 0.0f );
glRotatef( -m_fRotation.z(), 0.0f, 0.0f, 1.0f );
glTranslatef( -m_fPosition.x(),-m_fPosition.y(),-m_fPosition.z() );
}
void CCamera::Drift(float speed) {
int deg = int(m_fRotation.y());
if(deg<0) {
deg = -deg;
}
deg %= 360;
m_fPosition.x( m_fPosition.x() - (g_fvSin[deg] * speed) );
m_fPosition.z( m_fPosition.z() - (g_fvCos[deg] * speed) );
}
void CCamera::Strafe(float speed) {
int deg = int(m_fRotation.y() + 90.0f);
if(deg<0) {
deg = -deg;
}
deg %= 360;
m_fPosition.x( m_fPosition.x() - (g_fvSin[deg] * speed) );
m_fPosition.z( m_fPosition.z() - (g_fvCos[deg] * speed) );
}
#endif // Camera_cpp //
#ifndef Trig_h
#define Trig_h
float g_fvSin[360];
float g_fvCos[360];
void BuildTrigTables();
#endif // Trig_h //
#ifndef Trig_cpp
#define Trig_cpp
#include <math.h>
#include "Trig.h"
void BuildTrigTables() {
for( int deg = 0; deg < 360; deg++ ) {
float rad = deg * 3.1415 / 180;
g_fvSin[deg] = float(sin(rad));
g_fvCos[deg] = float(cos(rad));
}
}
#endif // Trig_cpp //
#ifndef Main_h
#define Main_h
#include <stdlib.h>
#include "Trig.cpp"
#include "Camera.cpp"
#include "Bsp.cpp"
// Game defs //
const float g_cfWalkSpeed = 0.3f;
const float g_cfRunSpeed = 0.9f;
const float g_cfMousePitch = 0.06f;
const float g_cfMouseYaw = -0.06f;
// Globals //
CCamera g_Camera;
float g_fWidth;
float g_fHeight;
// Glut callbacks //
GLvoid Display(GLvoid);
GLvoid Reshape(GLint,GLint);
GLvoid Keyboard(GLubyte,GLint,GLint);
GLvoid PassiveMotion(GLint,GLint);
GLvoid Special(GLint,GLint,GLint);
GLvoid Idle(GLvoid);
// Extra functions //
void ExitProc();
#endif // Main_h //
#include <windows.h>
// OpenGL headers //
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
// My headers //
#include "Main.h"
int main( int argc, char **argv ) {
atexit(ExitProc);
BuildTrigTables();
g_Camera.m_fVelocity = CVector( 0.0f, 0.0f, 0.0f );
g_Camera.m_fPosition = CVector( 0.0f, 1.5f, 0.0f );
g_Camera.m_fRotation = CVector( 0.0f, 0.0f, 0.0f );
glutInit(&argc,argv);
glutInitWindowPosition(50,50);
glutInitWindowSize(512,380);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Navigator");
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Keyboard);
glutPassiveMotionFunc(PassiveMotion);
glutIdleFunc(Idle);
glutMainLoop();
return 0;
}
// Glut callbacks //
GLvoid Display(GLvoid) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
g_Camera.OrientGl();
glBegin(GL_LINES);
for(float i=-9.0f; i<10.0f; i+=1.0f) {
glVertex3f(i,0,-9.0f);
glVertex3f(i,0,+9.0f);
glVertex3f(-9.0f,0,i);
glVertex3f(+9.0f,0,i);
}
glEnd();
glutSwapBuffers();
}
GLvoid Reshape(GLint w,GLint h) {
if(!h)h++;
float ratio = 1.0f*w/h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,w,h);
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
g_Camera.OrientGl();
g_fWidth = glutGet(GLUT_WINDOW_WIDTH);
g_fHeight = glutGet(GLUT_WINDOW_HEIGHT);
}
GLvoid Keyboard(GLubyte key,GLint x,GLint y) {
switch(key) {
case 27: // Esc key
exit(0);
break;
case ''w'':
g_Camera.Drift(+g_cfWalkSpeed);
break;
case ''W'':
g_Camera.Drift(+g_cfRunSpeed);
break;
case ''s'':
g_Camera.Drift(-g_cfWalkSpeed);
break;
case ''S'':
g_Camera.Drift(-g_cfRunSpeed);
break;
case ''a'':
g_Camera.Strafe(+g_cfWalkSpeed);
break;
case ''A'':
g_Camera.Strafe(+g_cfRunSpeed);
break;
case ''d'':
g_Camera.Strafe(-g_cfWalkSpeed);
break;
case ''D'':
g_Camera.Strafe(-g_cfRunSpeed);
break;
};
}
GLvoid PassiveMotion(GLint x,GLint y) {
bool warp = false;
int cx = int(g_fWidth/2);
int cy = int(g_fHeight/2);
float dx = x - cx;
float dy = y - cy;
if(x!=cx) {
warp = true;
g_Camera.m_fRotation.y( g_Camera.m_fRotation.y() - dx * g_cfMousePitch );
}
if(y!=cy) {
warp = true;
g_Camera.m_fRotation.x( g_Camera.m_fRotation.x() - dy * g_cfMouseYaw );
}
if(warp) {
glutSetCursor(GLUT_CURSOR_NONE);
glutWarpPointer(cx,cy);
}
}
GLvoid Idle(GLvoid) {
glutPostRedisplay();
}
// Extra functions //
void ExitProc() {
// empty so far
}
Enough code I hope. Probably more than some want to read. Please don''t knock my use of .h/.cpp''s. I know it''s dumb to include .cpp files but It''s quicker to change stuff while I''m so early in the debug stage.
Thank you in advance to anyone who responds.