From ab1fa41c240b6e8626ffa4bff1ac3479eda593fb Mon Sep 17 00:00:00 2001 From: gugus Date: Wed, 23 Feb 2011 12:28:46 +0100 Subject: [PATCH] fix camera bug --- ogre/mouselook.cpp | 58 +++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/ogre/mouselook.cpp b/ogre/mouselook.cpp index 84e6e0397..c74f5365c 100644 --- a/ogre/mouselook.cpp +++ b/ogre/mouselook.cpp @@ -2,6 +2,7 @@ #include #include +#include using namespace OIS; using namespace Ogre; @@ -9,29 +10,48 @@ using namespace OEngine::Render; void MouseLookEvent::event(Type type, int index, const void *p) { - if(type != EV_MouseMove || camera == NULL) return; + if(type != EV_MouseMove || camera == NULL) return; - MouseEvent *arg = (MouseEvent*)(p); + MouseEvent *arg = (MouseEvent*)(p); - float x = arg->state.X.rel * sensX; - float y = arg->state.Y.rel * sensY; + float x = arg->state.X.rel * sensX; + float y = arg->state.Y.rel * sensY; - camera->yaw(Degree(-x)); + camera->getParentSceneNode()->getParentSceneNode()->yaw(Degree(-x)); + camera->getParentSceneNode()->pitch(Degree(-y)); + if(flipProt) + { + // The camera before pitching + /*Quaternion nopitch = camera->getParentSceneNode()->getOrientation(); - if(flipProt) - { - // The camera before pitching - Quaternion nopitch = camera->getOrientation(); + camera->getParentSceneNode()->pitch(Degree(-y)); - camera->pitch(Degree(-y)); + // Apply some failsafe measures against the camera flipping + // upside down. Is the camera close to pointing straight up or + // down? + if(Ogre::Vector3(camera->getParentSceneNode()->getOrientation()*Ogre::Vector3::UNIT_Y)[1] <= 0.1) + // If so, undo the last pitch + camera->getParentSceneNode()->setOrientation(nopitch);*/ + //camera->getU - // Apply some failsafe measures against the camera flipping - // upside down. Is the camera close to pointing straight up or - // down? - if(camera->getUp()[1] <= 0.1) - // If so, undo the last pitch - camera->setOrientation(nopitch); - } - else - camera->pitch(Degree(-y)); + // Angle of rotation around the X-axis. + float pitchAngle = (2 * Ogre::Degree(Ogre::Math::ACos(camera->getParentSceneNode()->getOrientation().w)).valueDegrees()); + + // Just to determine the sign of the angle we pick up above, the + // value itself does not interest us. + float pitchAngleSign = camera->getParentSceneNode()->getOrientation().x; + + // Limit the pitch between -90 degress and +90 degrees, Quake3-style. + if (pitchAngle > 90.0f) + { + if (pitchAngleSign > 0) + // Set orientation to 90 degrees on X-axis. + camera->getParentSceneNode()->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f), + Ogre::Math::Sqrt(0.5f), 0, 0)); + else if (pitchAngleSign < 0) + // Sets orientation to -90 degrees on X-axis. + camera->getParentSceneNode()->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f), + -Ogre::Math::Sqrt(0.5f), 0, 0)); + } + } }