View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

[SOLVED] Camera Rotation and lookAt

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

#1Dbof  Members

Posted 03 January 2013 - 08:49 AM

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.

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?

#2Dbof  Members

Posted 03 January 2013 - 11:54 AM

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...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.