Skip to main content
gamedev.net
🔒 Locked

(still) a camera problem

Started by programBoy 9 replies 900+ views
Original Post
programBoy
programBoy
I've posted earlier a thread about the same thing, but it still doesn't work.. The camera moves wrong, and I use this code to move forward/backward:
[source lang="cpp]
void Bcamera::RotLeft(Bfloat angle) {
	rotation.angle -= angle;
	if (rotation.angle < 0) rotation.angle += 360.0f;
}

void Bcamera::RotRight(Bfloat angle) {
	rotation.angle += angle;
	if (rotation.angle > 359.0f) rotation.angle -= 360.0f;
}

void Bcamera::MoveForward(Bfloat distance) {
	position.x -= (float) sin(rotation.angle * PI / 180) * distance;
	position.z -= (float) cos(rotation.angle * PI / 180) * distance;
}

void Bcamera::MoveBackward(Bfloat distance) {
	position.x += (float) sin(rotation.angle * PI / 180) * distance;
	position.z += (float) cos(rotation.angle * PI / 180) * distance;
}

/*-----------------------------------------------------------*/

// (current matrixmode: GL_MODELVIEW)
glRotatef(current_cam->rotation.angle, current_cam->rotation.x, current_cam->rotation.y, current_cam->rotation.z);	
glTranslatef(-current_cam->position.x, -current_cam->position.y, -current_cam->position.z);

and here is demo of how it moves wrong: http://members.lycos.nl/gideon715/map_test.html (click on the link on that page, with lycos you can't post the link to the file in one time :S)
Ever noticed how fast Windows runs? Neither did I.
Enigma
Enigma
Without looking into it too hard I would suggest changing the order of your glRotate and glTranslate calls. It looks like your calculating the position of the camera in world coordinates, but because of the rotate call you're actually moving in camera coordinates. This might not be the problem, but it's the first thing that comes to mind.

Enigma
programBoy
programBoy
If I first translate the axis would be at (0, 0, 0), I read in the redbook.
Ever noticed how fast Windows runs? Neither did I.
Enigma
Enigma
I'm afraid your statement doesn't make much sense. What axis?. If you load the indentity matrix and then translate your axes are (1, 0, 0), (0, 1, 0) & (0, 0, 1). Where abouts in the red book did you read this?

Enigma
ProPuke
ProPuke
When you apply a transformation like glRotate or glTranslate it is multiplied by the current matrix. So if you were to translate first & then rotate the result, you would infact not be rotating about the origin (0,0,0). You would be rotating the already done translation. It's kinda like it's in reverse - so instead of imagining the actions being carried out in the order you specify like a little LOGO turtle, you're actually specifying the actions in reverse overlap order (backwards)
I believe this is what programBoy meant
____ _ ___ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
programBoy
programBoy
Chapter 3, page 112, fourth edition =P
Ever noticed how fast Windows runs? Neither did I.
Enigma
Enigma
I'll look it up when I get home.

Enigma
Enigma
Enigma
I've got the third edition and there's nothing relevant on page 112 or the surrounding pages. You'll have to quote it for me. Regardless, I maintain that changing the order of the operations is possibly the solution. Now that I have time to explain:

Currently you calculate the translation based on the rotation. So if the camera is pointing at a 45º angle and you move forward 1 unit you calculate the the movemebt as -0.707 in x and -0.707 in y. This is in world coordinates since if you were operating in camera coordinates moving forward would only ever affect y. When you come to render you rotate into camera coordinates with your glRotate call and then translate based on the world coordinates you calculated. So your actual movement would be at 90º. 45º from the rotation into camera coordinates and another 45º from the translation calculated in world coordinates.

Enigma
programBoy
programBoy
I'd tried it already, but it still doesn't work.. :(

And oh yeah, I did mean this image (looked it up online): http://fly.cc.fer.hr/~unreal/theredbook/figures/fig3-4.gif
Ever noticed how fast Windows runs? Neither did I.
Enigma
Enigma
That image is just the standard translate-then-rotate vs rotate-then-translate diagram. If your translation is in world coordinates then you want translate-then-rotate. If it's in camera coordinates then you want rotate-then-translate.

Now that I've had a chance to download and run your executable it looks like your rotations are simply reversed. Try swapping the + and - signs on position.z -= and position.z += in MoveForward and MoveBackward.

Enigma
programBoy
programBoy
Thanks, it works now!! :)
Ever noticed how fast Windows runs? Neither did I.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.