Advertisement

mouselook / heading problem?

Started by November 17, 2004 04:22 PM
1 comment, last by cozzie 20 years ago
Hi all. Some of you may have seen my earlier posts about my camera heading in combination with strafe problem. The problem is that when I rotate my camera around the X_axis (when the heading changes), strafing doesn't work anymore. When I initalised my scene, strafing works fine, till I rotate the camera using free mouselook. I've drawn all physics on paper to solve the problem, but I just can't find it. Here's the code that I use, maybe someone can help me: Move the camera forward:

scene->cam->pos.x -= (float)sin(scene->cam->heading*piover180) * scene->cam->speed;
scene->cam->pos.z -= (float)cos(scene->cam->heading*piover180) * scene->cam->speed;

Strafe the camera left:

scene->cam->pos.x -= (float)sin(scene->cam->heading+(1*90)*piover180) * scene->cam->speed;
scene->cam->pos.z -= (float)cos(scene->cam->heading+(1*90)*piover180) * scene->cam->speed;

Mouselook:

GetCursorPos(&mpos);	

*_camheading += ((float)((screenwidth/2 - mpos.x)) * _speed);
*_camyrot = *_camheading;
*_camxrot -= (float)(mpos.y - screenheight/2) * _speed;

SetCursorPos(screenwidth/2, screenheight/2);

Part of renderscene function:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);

for(c=0;c<nr_lights;c++) glLightfv(lights[c].id, GL_POSITION, lights[c].pos);
	
glLoadIdentity();						

xtrans = -cam->pos.x;
ytrans = -cam->pos.y;
ztrans = -cam->pos.z;
sceneroty = 360 - cam->rot.y;
scenerotx = 360 - cam->rot.x;

glRotatef(scenerotx, 1, 0, 0);
glRotatef(sceneroty, 0, 1, 0);
glTranslatef(xtrans, ytrans, ztrans);

// draw all objects with glPushMatrix and glPopMatrix



Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

cozzie,

consider keeping track of your camera's position, view, and up vectors. Then, before you draw anything, you collect input information. Generally I do mouse look first. The mouse look handles all of the rotations, so the position doesnt change, rather the view vector is rotated. Once I've done the mouse look stuff then I can calculate a new strafe vector. It will equal Normalize( CrossProduct( view - position, upvector));

Now that the view and strafe vectors are rotated I collect keyboard input. If the forward or backward keys are pressed then I modify my position vector like this:

position += (Normalize(view - position) * (speed * elapsed time));

if the left or right keys are pressed then I modify the postion vector like this:

position += (Normalize(strafe) * (speed * elapsed));

Now that the position vectors are taken care of I then when it comes time to draw instead of doing raw translations and rotations I use:

gluLookAt(pos.x,pos.y,pos.z,view.x,view.y,view.z,up.x,up.y,up.z);

Anyway, hope this helps...
Hard work USUALLY pays off in the future, but laziness ALWAYS pays off right now.
Advertisement
Hi,
Thanks for the reaction, I've solved the problem.
You wouldn't believe what the problem was.

I forgot ( )'s around the formula where I'm using the heading + 90 degrees * piover180.

I had the piover180(deg2rad), inside the sinus function.

Thanks for the reaction again.
greets from holland

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement