Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Maths problem

Started by drb2k2 3 replies 600+ views
Original Post
drb2k2
drb2k2
Hi all, I am trying to get a camera to rotate around an object. I have used the base camera class from UltimateGamesProgramming.com It works okay in a limited context, but if the camera moves to the right(while still looking at one object) the up direction of (0,1,0) is no longer valid. Instead of the camera going over the object, it goes sideways and skewy. The code I have is to try and get the up key to rotate over the object no matter what the orientation is. When the key is press I do this to get the 'up' vector.

						dir = Microsoft.DirectX.Vector3.Cross(new Microsoft.DirectX.Vector3(0,1,0),this.m_VecCamPos);
					RotateCamAroundPoint(1f,dir)
and the actual rotation method RotateCamAroundPoint is

private void RotateCamAroundPoint(float Angle, Microsoft.DirectX.Vector3 Direction)
		{
			Microsoft.DirectX.Quaternion qRotation = new Microsoft.DirectX.Quaternion(0,0,0,0);
			qRotation.RotateAxis(Direction,Microsoft.DirectX.Direct3D.Geometry.DegreeToRadian(Angle));
			


			Microsoft.DirectX.Quaternion qView = new Microsoft.DirectX.Quaternion();
			qView.X = this.m_VecCamPos.X - this.m_VecCamLook.X;
			qView.Y = this.m_VecCamPos.Y - this.m_VecCamLook.Y;
			qView.Z = this.m_VecCamPos.Z - this.m_VecCamLook.Z;
			qView.W = 0;


			

			Microsoft.DirectX.Quaternion qNewView = new Microsoft.DirectX.Quaternion();
			Microsoft.DirectX.Quaternion qTmp = new Microsoft.DirectX.Quaternion();

			qTmp = Microsoft.DirectX.Quaternion.Multiply(qRotation, qView);
			qNewView = Microsoft.DirectX.Quaternion.Multiply(qTmp,Microsoft.DirectX.Quaternion.Conjugate(qRotation));
		

			Microsoft.DirectX.Vector3 tmp = this.m_VecCamPos;

	
			this.m_VecCamPos.X = this.m_VecCamLook.X + qNewView.X;
			this.m_VecCamPos.Y = this.m_VecCamLook.Y + qNewView.Y;
			this.m_VecCamPos.Z = this.m_VecCamLook.Z + qNewView.Z;

	}
This works well until the camera is parallel to (0,1,0) I just can't work out what I need to do. Ideally I would like the dir vector to represent a vector that is perpendicular to the looking vector. That way the angle between the two will always be 90 degrees and the camera will never become parallel. phew, I hope i've explained it ok. Any pointers would be much appreciated. Cheers DRB2k2
drb2k2
drb2k2
just to make it a bit clearer,
I would like to know how to derive a vector given one vector and the angle of 90 degrees.
Note that this is quite difficult since the camera could be in any orientation, i.e. it is free to move around the object in any yaw pitch roll combination.
xsirxx
xsirxx
I have a real hard time understanding here. Is your problem gimbal lock? meaning when you adjust yaw pitch and roll it works in most cases except 90 degrees? And you want to be able to get around this by not allowing that certain angle?

Otherwise, Just set a target and a source and let directX handle the rest...
--X
drb2k2
drb2k2
thanks for the link, but that solution is too simple for me.
Lets imagine that we have an object at 0,0,0 and the camera at 0,0,7, where the Z axis runs into the screen.
I want the camera to over the top of the object and back around again, so that it is in effect orbiting it. Here we would just continually add 1 to the Y coordinate and a smaller amount to the Z coordinate until the camera is directly above the object.

Now imagine that the camera is off at an angle, e.g. -4,0,7. In this case we would need to add a small amount to X,Y and Z in order to get the rotation around the object.

I get the vector that the camera is facing by doing cameraLook - cameraPosition,
now I need a vector that is at a right angle to calculate the cross product which results in the up direction. Once I have this I can just add it to the current camera position, and it should rotate nicely around the object.

Cheers
DRB2k2

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.