Skip to main content
gamedev.net
🔒 Locked

Camera Rotation

Started by AstAnDK Apr 26, 2006 at 8:37 AM 3 replies 1k views
Original Post
AstAnDK
AstAnDK
Hi Somehow I don't get the camera rotation working correctly :( Normally this should be done in 5min but somehow I don't get it right this time.
		__forceinline void addCameraRotationX(type::real rotx)
		{
			type::real dy= _position.r[Y] - _target.r[Y];
			type::real dz= _position.r[Z] - _target.r[Z];

			type::real len= sqrt(dy*dy + dz*dz);
			type::real angle= atan(dy / dz) + rotx;

			_position.r[Y]= _target.r[Y] + len * sin(angle);
			_position.r[Z]= _target.r[Z] + len * cos(angle);

			_view_matrix_version++;
		}

		__forceinline void addCameraRotationY(type::real roty)
		{
			type::real dx= _position.r[X] - _target.r[X];
			type::real dz= _position.r[Z] - _target.r[Z];

			type::real len= sqrt(dx*dx + dz*dz);
			type::real angle= atan(dz / dx) + roty;

			_position.r[X]= _target.r[X] + len * cos(angle);
			_position.r[Z]= _target.r[Z] + len * sin(angle);

			_view_matrix_version++;
		}
I'm sure my mistake should be quite obvious but I just can't see it. Please help. cheers Daniel
JohnBolton
JohnBolton
When you rotate the camera, shouldn't you be changing the target's position instead of the camera's position?
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
AstAnDK
AstAnDK
Depends on what you want to do. I want to rotate the camera around the target.
If you want to rotate the camera on its current position, then of course, you have to move the target.
JohnBolton
JohnBolton
You didn't say what the problem is, but I'm going to guess that you should be using atan2() instead of atan(). The range of atan is only -π/2 to +π/2 and you want -π to +π. You also avoid divide-by-zero that way.

BTW, the word for what you are trying to do is "revolve", not "rotate". But that is beside the point.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
AstAnDK
AstAnDK
I was looking for an atan2 function but it was not listed. Now I looked again and VS found it. Thanks to your explanation, I also see where my mistake was. The problem was that it only rotated half of the way, or revolving as this is the correct word ;)

thx for the help

cheers
Daniel

Topic Locked

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

Sign in to reply to this topic.