Jump to content
  • Advertisement
Sign in to follow this  
EricmBrown

Quaternion Camera Problem

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to use a quaternion camera for my game engine. the camera works perfectly while the position is at 0,0,0. but whenever i move the position the rotation of the camera goes wacky. i havent been able to place my finger on the cause, here is the code - any help is very welcome :-).
void update()
{
        //horizontal camera movement

	if(diMgr.getCurMouseX() == 0)
		xDir = 0;

	else if(diMgr.getCurMouseX() > 1)
		xDir = -.1;

	else if(diMgr.getCurMouseX() < 1)
		xDir = .1;

        //vertical camera movement
	if(diMgr.getCurMouseY() == 0)	
		yDir = 0;

	else if(diMgr.getCurMouseY() > 1)
		yDir = -.05;

	else if(diMgr.getCurMouseY() <1)
		yDir = .05;


        //dont want to look up or down more than 1 radian!
	rotationAboutX += yDir; //how much is camera rotated up/down?
	if(rotationAboutX > 1) //if it rotates up too much, cap it
	{
		rotationAboutX = 1;
		yDir = 0;
	}
	if(rotationAboutX < -1) //same if it rotates down too far
	{
		rotationAboutX = -1;
		yDir = 0;
	}

        //now find the horizontal axis to rotate around the x-axis
        vector3 temp, axis;
	temp.x = view.x - position.x;
	temp.y = view.y - position.y;
	temp.z = view.z - position.z;
	axis = crossProduct(temp, up);
	axis = normalize(axis);

	rotateCamera(axis,yDir);		//rotate vertical camera
	rotateCamera(vector3(0,1,0),xDir);	//rotate horizontal

	if(GetAsyncKeyState(VK_UP) <0)	
	{
		position.z += view.z; //move in the direction we are facing
		position.x += view.x;
	}
	if(GetAsyncKeyState(VK_DOWN) <0)
	{
		position.z += -view.z; //move away from the direction we face
		position.x += -view.x;
	}

	//set camera location
	gluLookAt(position.x,position.y,position.z,(position.x + view.x),
                 (position.y + view.y),(position.z + view.z),up.x,up.y,up.z);
}//end update()

void rotateCamera(vector3 vec, double angle)
{
	quaternion temp, quat_view, result;

	//set the rotation quaternion to get the final coordinates of the view
	temp.x = vec.x * sin(angle/2);
	temp.y = vec.y * sin(angle/2);
	temp.z = vec.z * sin(angle/2);
	temp.w = cos(angle/2);


	//set view quaternion equal to view vector
	quat_view.x = view.x;
	quat_view.y = view.y;
	quat_view.z = view.z;
	quat_view.w = 0;

	//rotate the quaternion by equation finalQuat = rotQuat * viewQuat * 
        //conjugate(rotQuat)

	result = mult(mult(temp, quat_view), conjugate(temp));

	//set the view vector equal to the final, rotated quaternion coordinates
	view.x = result.x;
	view.y = result.y;
	view.z = result.z;
}
I used the tutorial in the math and physics section of gamedev articles to make the camera. and like i said - it works perfectly while at camera position (0,0,0). but it gives me all sorts of wacky controls once i move my camera position. please help :-\

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You are using the view vector inconsistently. The code that calculates the rotations to perform is treating the view vector as if it was the point to look at; the code that set up the camera matrix and the rotation code is treating the view vector as a direction vector.

The easiest fix would be to change the code that is calculating the rotation axis. Keep in mind that the view vector is already a unit vector representing a direction.

Share this post


Link to post
Share on other sites
Took me about 5 minutes of studying and staring at your response to finally get it, but it hit me like a ton of bricks! thanks alot, i apreciate the help :-)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!