PDA

View Full Version : MOvement wiht camera



02-04-2006, 12:40 PM
I have gotten a camera to wokr and I had
the function to move correctly, but it the camera was not staying on the floor:

/*Mouse View Events*/
void glCLeft() {m_angle -= 1.5f; }
void glCRight() {m_angle += 1.5f;}
void glCUp() {m_height += 1.5f;}
void glCDown() {m_height -= 1.5f;}


/*Keyboard movement events */
void glCMLeft() {camerax += 1.0f;}
void glCMRight(){camerax -= 1.0f;}
void glCMUp() {cameraz += 1.0f;}
void glCMDown() {cameraz -= 1.0f;} Whereas the Render:


void System::Render()
{

// clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// load the identity matrix (clear to default position and orientation)
glLoadIdentity();

glRotatef(m_height, 1.0f ,0.0f ,0);
glRotatef(m_angle, 0.0f ,1.0f ,0);
glTranslatef(camerax * SINE(m_angle),cameray, cameraz * COS(m_angle));


/*Draw floor*/
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, m_textureObjectOne);
glDrawFloor();
glPopMatrix();

} And COS and SINE are defined as:


#include <math.h>
#ifndef __GL_COMPONENT
#define __GL_COMPONENT

#define PI 3.14159
#define TWO_PI PI*2.0
#define HALF_PI PI/2.0
#define SINE(x) (float)sin((PI/180.0f) * x)
#define COS(x) (float)cos((PI/180.0f) * x)
When I preass 'A' or 'D' to strafe, it moves on its original X Y axis, not the one I am loking at.
Can I get help?

~Axesor

02-04-2006, 12:42 PM
Oh yeah, one more thing. This also happens when I want to 'move' not strafe, up. 'W' and 'S'

Purvis
02-05-2006, 04:47 PM
Because you are straffing in World Coordinates not in Camera Coordinates, so it doesn't matter where you are looking at, it will always move on the fixed X,Y and eventually Z axis (Albeit this might be what you want for Z).

To move on Camera Coordinates transform your displacement vector (say, -1,0,0 for left, 0,1,0 for front, or whatever convention you choose) by the inverse of the camera transformation matrix.

02-06-2006, 01:39 AM
...huh? Can I get an example of some sort?

02-06-2006, 02:08 AM
Fixed it 0_0
All I did was:

float camerarot = 360.0f - m_angle;
glRotatef(m_height,0,1,0);
glRotatef(camerarot,1,0,0);
glTranslatef(-camerax, cameray, -cameraz);

I donot know why that made it work though >_>
I think it is know going of the basis of 360 degrees, which alows it to move in a circle.
Thnaks for the help though! ^_^