Archived

This topic is now archived and is closed to further replies.

xg0blin

Cylindrical Coordinate System Camera

Recommended Posts

I am attempting to make a camera that rotates using cylindrical coordinates. Here is the code:
        
    void CylinderCam::RotateView(GLdouble r, GLdouble Theta, GLdouble z)
{
	// Temp variables to hold our temporary Positions ( we need to

	// pretend that we are at 0, 0, 0);

	Robert_Vector::Vector<GLdouble> Cylinderical, TempView, TempPosition;

	// change the degrees to radians

	Theta = (Theta * 3.14159)/180;
	
	// set position to 0, 0, 0

	TempPosition.SetVector(0.0f, 0.0f, 0.0);
	// set view equal to the length of the vector from the origin

	TempView = View - Position;
	
	// convert to cylinderical

	Cylinderical.x = sqrt(TempView.x * TempView.x + TempView.y * TempView.y);
	
	// We can avoid a divide by zero here. If x is equal to 0 and y > 0, then

	// theta will be pi/2 (think of the unit circle). If x is equal to 0 and

	// y < 0, then theta will be -pi/2 (also unit circle). Also, we have the

	// case where x < 0, and y = 0, where atan() will give 0, but it should be

	// pi. We handle those three cases first, and then we do the atan case

	if(TempView.x == 0 && TempView.y > 0)
		Cylinderical.y = 3.14159/2;
	else if(TempView.x == 0 && TempView.y < 0)
		Cylinderical.y = -3.14159/2;
	else if(TempView.x < 0 && TempView.y == 0)
		Cylinderical.y = 3.14159;
	else if(TempView.x == 0 && TempView.y == 0)
		Cylinderical.y = 0;
	else
		Cylinderical.y = atan(TempView.y/TempView.x);
	
	Cylinderical.z = TempView.z; 

	// add the values to change by

	Cylinderical.x += r;
	Cylinderical.y += Theta;
	Cylinderical.z += z;

	// convert back to rectangular coordinates

	Cylinderical.x = Cylinderical.x * cos(Cylinderical.y);
	Cylinderical.y = Cylinderical.x * sin(Cylinderical.y);
	Cylinderical.z = Cylinderical.z;
	
	// add the new coordinates to the length of the original vector

	View = Position - Cylinderical;
}
  
I am calling it like this:
  
void CylinderCam::CheckForMovement(GLdouble Speed)
{
	if(GetKeyState('W') & 0x80 || GetKeyState('w') & 0x80)
		MoveCamera(speed);
	if(GetKeyState('S') & 0x80 || GetKeyState('s') & 0x80)
		MoveCamera(-speed);
	if(GetKeyState('D') & 0x80 || GetKeyState('d') & 0x80)
		StrafeCamera(speed);
	if(GetKeyState('A') & 0x80 || GetKeyState('a') & 0x80)
		StrafeCamera(-speed);
	if(GetKeyState(VK_LEFT) & 0x80)
		RotateView(1, 0, 0);
	if(GetKeyState(VK_RIGHT) & 0x80)
		RotateView(-1, 0, 0);
}
    
It's all messed up, nothing works right. Anyone have a clue? [edited by - xg0blin on November 9, 2002 3:24:43 AM] [edited by - xg0blin on November 9, 2002 11:25:16 PM]

Share this post


Link to post
Share on other sites