• Create Account

### #ActualDbof

Posted 03 January 2013 - 11:55 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?

### #1Dbof

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?