Skip to main content
GameDev.net gamedev.net
🔒 Locked

Camera problem

Started by programBoy Oct 28, 2004 at 11:56 AM 6 replies 1.7k views
Original Post
programBoy
programBoy
I know there are lots of threads about this subjects, but the all didn't help. The camera should move with this code:


void Bcamera::MoveLeft(Bfloat angle) {
	rotation.angle -= angle;
	if (rotation.angle < 0) rotation.angle += 360.0f;
}

void Bcamera::MoveRight(Bfloat angle) {
	rotation.angle += angle;
	if (rotation.angle > 359.0f) rotation.angle -= 360.0f;
}
void Bcamera::MoveForward(Bfloat speed) {
	position.x -= (float) sin(rotation.angle * 0.017453292519943295769236907684886f) * speed;
	position.z -= (float) cos(rotation.angle * 0.017453292519943295769236907684886f) * speed;
// that long number (0.01743 etc.) is PI / 180
}

void Bcamera::MoveBackward(Bfloat speed) {
	position.x += (float) sin(rotation.angle * 0.017453292519943295769236907684886f) * speed;
	position.z += (float) cos(rotation.angle * 0.017453292519943295769236907684886f) * speed;
}




And this part not to be forgotten:

// current matrix mode is 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);




What is wrong with this code..? The camera moves, but only in the direction i don't want :)
Ever noticed how fast Windows runs? Neither did I.
MARS_999
MARS_999
get rid of that huge number 7 places should be plenty enough. If you code works like you say and you are moving left instead of right or vice versa change your +/- on those movements to the opposite...
programBoy
programBoy
Quote:
Original post by MARS_999
If you code works like you say and you are moving left instead of right or vice versa change your +/- on those movements to the opposite...


I'm not stupid :P I mean not when it rotates, but when I go forward. The rotation stays the same, but I go in another direction.
Ever noticed how fast Windows runs? Neither did I.
Ranger_One
Ranger_One
I got the same impression as MARS_999...

so if it's not flipped, then how is it moving 'wrong'?

Zakwayda
Zakwayda
Hi,

I have a couple of thoughts.

First of all, I'd rename MoveLeft() and Right() to RotateLeft() and Right(). It's a little confusing as is (or could be, especially where you call them).

Also, as others have said, get that constant out of there! :-) The way it's usually done is to have some constants declared in a math header somewhere. It only happens once, and it's out of the way, so you can declare pi to as many millions of places as you want. Then, declare your other pi-related constants - piovertwo, twopi, degtorad, radtodeg - in terms of pi. Then your code just becomes (rotation.angle * DEG_TO_RAD), and is much easier to read and maintain.

Finally, I assume current_cam->rotation is your up vector, 0, 1, 0 in this case. But more importantly, I'm not sure but shouldn't you be glRotating by -current+cam->rotation.angle? That might be your problem.
squicklid
squicklid
Just a tip on style.. don't use magic numbers. they are confusing and ugly. Instead, use a const.
MARS_999
MARS_999
Quote:
Original post by squicklid
Just a tip on style.. don't use magic numbers. they are confusing and ugly. Instead, use a const.


:D Couldn't have said it better myself. Waste of time looking for that magic number later on. Easier to use a const and change once.
Also that HUGE number defeats the purpose of being precise due to floats only hold 7 digits of precision. I see you wanted it to be a float with the suffix. Unless you are doing some very very small objects you don't need that precise of a number.

Also I never said the original poster was stupid... Just pointing out what I thought was wrong from the posts implied problem.
programBoy
programBoy
I've made a small demo of my camera problems, so that you can see how my camera moves :D:

http://members.lycos.nl/gideon715/map_test.html
(click there on the link, with lycos you can't post the link to the file in one time)

[Edited by - programBoy on October 30, 2004 9:13:54 AM]
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.