Browse Source

Camera rotation working properly

Marko Pintera 13 years ago
parent
commit
c6eacbbee6
2 changed files with 14 additions and 18 deletions
  1. 10 18
      CamelotClient/CmDebugCamera.cpp
  2. 4 0
      CamelotClient/CmDebugCamera.h

+ 10 - 18
CamelotClient/CmDebugCamera.cpp

@@ -15,7 +15,8 @@ namespace CamelotEngine
 	const float DebugCamera::ROTATION_SPEED = 0.5f; // Degrees/pixel
 
 	DebugCamera::DebugCamera(GameObjectPtr parent)
-		:Component(parent), mGoingForward(false), mGoingBack(false), mGoingLeft(false), mGoingRight(false), mFastMove(false), mCameraRotating(false)
+		:Component(parent), mGoingForward(false), mGoingBack(false), mGoingLeft(false), mGoingRight(false), 
+		mFastMove(false), mCameraRotating(false), mPitch(0.0f), mYaw(0.0f)
 	{
 		mCamera = gameObject()->getComponent<Camera>();
 		mCamera->setNearClipDistance(5);
@@ -110,27 +111,18 @@ namespace CamelotEngine
 
 		if(mCameraRotating)
 		{
-			//float horizontalSpeed = event.relX / gTime().getFrameDelta();
+			mYaw += Degree(gInput().getHorizontalAxis() * ROTATION_SPEED);
+			mPitch += Degree(gInput().getVerticalAxis() * ROTATION_SPEED);
 
-			GO()->yaw(Degree(gInput().getHorizontalAxis() * ROTATION_SPEED));
-			GO()->pitch(Degree(gInput().getVerticalAxis() * ROTATION_SPEED));
+			Quaternion yRot;
+			yRot.FromAngleAxis(Radian(mYaw), Vector3::UP);
 
-			// Prevent roll due to inprecision
-			Vector3 validRight = GO()->getForward().crossProduct(Vector3::UP);
+			Quaternion xRot;
+			xRot.FromAngleAxis(Radian(mPitch), yRot.xAxis());
 
-			if(GO()->getForward().dotProduct(Vector3::UP) > 0.5f)
-			{
-				validRight = GO()->getForward().crossProduct(-Vector3::FORWARD);
-			}
+			Quaternion camRot = xRot * yRot;
 
-			Quaternion rightRot = GO()->getRight().getRotationTo(validRight);
-			//if(validRight.dotProduct(Vector3::RIGHT) < 0.0f)
-			//{
-			//	rightRot.w = -rightRot.w;
-			//}
-
-
-			GO()->setRotation(GO()->getRotation() * rightRot);
+			GO()->setRotation(camRot);
 		}
 	}
 }

+ 4 - 0
CamelotClient/CmDebugCamera.h

@@ -3,6 +3,7 @@
 #include "CmPrerequisites.h"
 #include "CmComponent.h"
 #include "CmInputHandler.h"
+#include "CmMath.h"
 
 namespace CamelotEngine
 {
@@ -18,6 +19,9 @@ namespace CamelotEngine
 		bool mFastMove;
 		bool mCameraRotating;
 
+		Degree mPitch;
+		Degree mYaw;
+
 		CameraPtr mCamera;
 
 		void keyDown(CamelotEngine::KeyCode keyCode);