Advertisement

Camera strafe and mouselook not compatible

Started by November 01, 2004 03:50 PM
0 comments, last by bu11frog 20 years, 1 month ago
Hi All, Since today I've added a strafing to my camera in my OpenGL engine, when I use free mouselook and don't strafe, there's no problem, the other way around also. But when I look with the mouselook and after that try to strafe, the strafing doesn't work anymore. Here's code: Strafe:

if(input->buffer[DIK_A] || input->buffer[DIK_LEFT] & 0x80)
{
	scene->cam->xpos -= (float)sin(scene->cam->heading+(1*90)*piover180) * scene->cam->speed;
	scene->cam->zpos -= (float)cos(scene->cam->heading+(1*90)*piover180) * scene->cam->speed;
}
if(input->buffer[DIK_D] || input->buffer[DIK_RIGHT] & 0x80)
{
	scene->cam->xpos -= (float)sin(scene->cam->heading+(-1*90)*piover180) * scene->cam->speed;
	scene->cam->zpos -= (float)cos(scene->cam->heading+(-1*90)*piover180) * scene->cam->speed;
}


And here's the free mouselook code:

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

If someone has some good advice, please tell me. Thank you in advance.

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

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

Cozzie,

rather than keeping straight up rotation angles alone, consider keeping these vectors:

Position (world coords of the camera),
Direction (direction on the X,Z plane ie no elevation),
Up (elevation),
Strafe (cross product of Direction and Up)

if you modify these vectors based on movement and mouse look then you shouldnt have a problem. Ie, Use MouseLook to calculate new Direction and Up vectors. Then do the cross product to get the strafe vector. Position is only modified by adding the (magnitude of the move * the Direction vector) for forward and back movement, then magnitude of the move * the Strafe vector) for the left and right movement. As a note, it is important that each of these vectors (except for the position) be a unit vector...ie the magnitude of the vector = 1.

Hope this helps.
Hard work USUALLY pays off in the future, but laziness ALWAYS pays off right now.

This topic is closed to new replies.

Advertisement