• Advertisement
Sign in to follow this  

[SOLVED] Camera Rotation and lookAt

This topic is 1843 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'm trying to implement rotation functions for a camera. Here is some code:

void dbCamera::rotateAroundXYZ(dbVector3 position, float angle){	// from: http://www.gamedev.net/topic/183293-camera-rotating-a-vector-around-an-arbitrary-axis/	dbVector3 vNewView;	dbVector3 vView = camPos - camDirection;		float cosTheta = cos(angle);	float sinTheta = sin(angle);		// x position of the new rotated point	vNewView.x	= (cosTheta + (1 - cosTheta) * position.x * position.x) * vView.x;	vNewView.x += ((1 - cosTheta) * position.x * position.y - position.z * sinTheta) * position.y;	vNewView.x += ((1 - cosTheta) * position.x * position.z + position.y * sinTheta) * vView.z;	// y position of the new rotated point	vNewView.y  = ((1 - cosTheta) * position.x * position.y + position.z * sinTheta)	* vView.x;	vNewView.y += (cosTheta + (1 - cosTheta) * position.y * position.y)		* vView.y;	vNewView.y += ((1 - cosTheta) * position.y * position.z - position.x * sinTheta)	* vView.z;	// z position of the new rotated point	vNewView.z   = ((1 - cosTheta) * position.x * position.z - position.y * sinTheta)	* vView.x;	vNewView.z  += ((1 - cosTheta) * position.y * position.z + position.x * sinTheta)	* vView.y;	vNewView.z  += (cosTheta + (1 - cosTheta) * position.z * position.z)		* vView.z;	camPos = camDirection + vNewView;}

This seems to work for now. My problem now is the looAt function:

void dbCamera::lookAt(dbVector3 target){	// from: http://msdn.microsoft.com/en-us/library/windows/desktop/bb281710%28v=vs.85%29.aspx	camDirection = dbVector3Norm(target - camPos);	camRight = dbVector3Norm(dbVector3Cross(camUp, camDirection));	camUp = dbVector3Norm( dbVector3Cross(camDirection, camRight) );}

The function names should be self-explanatory. The bug acts when I try to rotate around an axis while looking at a point on the axis.
What I want to achieve is a camera which can rotate around an object and always looks at it (like a 3rd person camera).
What the camera does is...well, rotate around itself during the rotation.

http://youtu.be/CKud5uKotxE

Uploaded a short video of the rotation. The first rotation is on the same Y-position of the car. The second one the camera goes up and it all fucks up.

Any idea what may cause this problem?


Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement

OMG...figured out by myself. The formula I was using for the lookAt was wrong.

    void dbCamera::lookAt(dbVector3 target)
    {
    camUp = dbVector3(0.f, 1.f, 0.f);          // this is what was missing...
    
    // from: http://msdn.microsoft.com/en-us/library/windows/desktop/bb281710%28v=vs.85%29.aspx
    camDirection = dbVector3Norm(target - camPos);
    camRight = dbVector3Norm(dbVector3Cross(camUp, camDirection));
    camUp = dbVector3Norm( dbVector3Cross(camDirection, camRight) );
    }

I confused the camera up-vector with the world up-vector...

Share this post


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

  • Advertisement