Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


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

  • You cannot reply to this topic
1 reply to this topic

#1 Dbof   Members   -  Reputation: 105

Like
0Likes
Like

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?


Thanks in advance!



Sponsor:

#2 Dbof   Members   -  Reputation: 105

Like
0Likes
Like

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.



PARTNERS