Index: code/branches/turretFS14/data/levels/templates/spaceshipTurretTest.oxt
===================================================================
--- code/branches/turretFS14/data/levels/templates/spaceshipTurretTest.oxt	(revision 10008)
+++ code/branches/turretFS14/data/levels/templates/spaceshipTurretTest.oxt	(revision 10018)
@@ -39,5 +39,5 @@
   >
     <attached>
-        <Model position="0,0,0" pitch="0" roll="0" mesh="turretHead.mesh" scale3D="10,10,10"/>
+        <Model position="0,0,0" pitch="90" roll="0" mesh="turretHead.mesh" scale3D="10,10,10"/>
     </attached>
     <collisionShapes>
Index: code/branches/turretFS14/data/levels/turretTest.oxw
===================================================================
--- code/branches/turretFS14/data/levels/turretTest.oxw	(revision 10008)
+++ code/branches/turretFS14/data/levels/turretTest.oxw	(revision 10018)
@@ -42,5 +42,5 @@
         <Model position="0,0,0" mesh="turretSocketRight.mesh" scale3D="10,10,10" />
 
-        <Turret position="0,10,0" pitch="0" yaw="90" roll="0">
+        <Turret position="0,10,0" pitch="-90" yaw="0" roll="0" maxPitch=30 maxYaw=30>
             <templates>
               <Template link=spaceshipturrettest />
Index: code/branches/turretFS14/src/modules/objects/Turret.cc
===================================================================
--- code/branches/turretFS14/src/modules/objects/Turret.cc	(revision 10008)
+++ code/branches/turretFS14/src/modules/objects/Turret.cc	(revision 10018)
@@ -45,6 +45,8 @@
     {
         RegisterObject(Turret);
-        this->controller_ = new WaypointPatrolController(this->getContext());
-        gotOrient_ = false;
+        this->startOrientInv_ = Quaternion::IDENTITY;
+        this->maxPitch_ = 0;
+        this->maxYaw_ = 0;
+        this->gotOrient_ = false;
     }
 
@@ -60,88 +62,109 @@
     void Turret::rotatePitch(const Vector2& value)
     {
-        Radian currentPitch = this->getOrientation().getPitch();
-        Radian startPitch = startOrient_.getPitch();
-        Radian limit = Radian((Degree)45);
-        Radian upperBoundary = startPitch+limit;
-        if(upperBoundary > Radian(Degree(360)))
+        if (this->maxPitch_ == 0)
         {
-            upperBoundary -= Radian(Degree(360));
+            return;
         }
-        Radian lowerBoundary = startPitch-limit;
-        if(lowerBoundary < Radian(Degree(0)))
+        if (this->maxPitch_ >= 180) //no need to check, if the limit too big
         {
-            lowerBoundary += Radian(Degree(360));
+           SpaceShip::rotatePitch(value);
+           return;
         }
-        //orxout() << "Pitch:\t" << currentPitch << "\t" << startPitch << endl;
-        
-        if(currentPitch > upperBoundary && value.x > 0 ||
-           currentPitch < lowerBoundary && value.x < 0)
+
+        Quaternion drot = startOrientInv_ * this->getOrientation();
+
+        Ogre::Real val = boundBetween(drot.getPitch(false).valueDegrees(), -180, 180);
+        Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180);
+        Ogre::Real lowerBound = offset - this->maxPitch_;
+        Ogre::Real upperBound = offset + this->maxPitch_;
+        if (lowerBound < -180) //Avoid wrapping around of the boundaries
         {
-            //return;
+            lowerBound += this->maxPitch_;
+            upperBound += this->maxPitch_;
+            val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here
         }
-        
-        SpaceShip::rotatePitch(value);
+        else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side)
+        {
+            lowerBound -= this->maxPitch_;
+            upperBound -= this->maxPitch_;
+            val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here
+        }
+        if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0)) 
+        {
+            SpaceShip::rotatePitch(value);
+        }
+        return;
     }
 
     void Turret::rotateYaw(const Vector2& value)
     {
+        if (this->maxPitch_ == 0)
+        {
+            return;
+        }
+        if (this->maxPitch_ >= 180) //no need to check, if the limit too big
+        {
+           SpaceShip::rotateYaw(value);
+           return;
+        }
 
-        Radian currentYaw = this->getOrientation().getYaw();
-        Radian startYaw = startOrient_.getYaw();
-        Radian limit = Radian(Degree(45));
-        Radian upperBoundary = startYaw + limit;
-        Radian lowerBoundary = startYaw - limit;
-        /*if(upperBoundary >= Radian(Degree(180)));
+        Quaternion drot = startOrientInv_ * this->getOrientation();
+
+        Ogre::Real val = boundBetween(drot.getYaw(false).valueDegrees(), -180, 180);
+        Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180);
+        Ogre::Real lowerBound = offset - this->maxPitch_;
+        Ogre::Real upperBound = offset + this->maxPitch_;
+        if (lowerBound < -180) //Avoid wrapping around of the boundaries
         {
-            upperBoundary -= Radian(Degree(180));
-            lowerBoundary -= Radian(Degree(180));
-            currentYaw -= Radian(Degree(180));
+            lowerBound += this->maxPitch_;
+            upperBound += this->maxPitch_;
+            val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here
         }
-        if(lowerBoundary <= Radian(Degree(-180)))
+        else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side)
         {
-            lowerBoundary += Radian(Degree(180));
-            upperBoundary += Radian(Degree(180));
-            currentYaw += Radian(Degree(180));
-        }*/
-        //orxout() << "Yaw:\t" << (Degree)currentYaw << "\t" << (Degree)upperBoundary << "\t" << (Degree)lowerBoundary << endl;
-        //if((currentYaw > upperBoundary && value.x > 0) ||
-        //   (currentYaw < lowerBoundary && value.x < 0))
-        if((currentYaw < Radian(1) && value.x < 0) || (currentYaw > Radian(3) && value.x>0))
+            lowerBound -= this->maxPitch_;
+            upperBound -= this->maxPitch_;
+            val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here
+        }
+        if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0)) 
         {
-            //return;
+            SpaceShip::rotateYaw(value);
         }
-        SpaceShip::rotateYaw(value);
+        return;
     }
 
     void Turret::rotateRoll(const Vector2& value)
     {
-        SpaceShip::rotateRoll(value);
-    }
-
-    void Turret::setAlertnessRadius(float value)
-    {
-        this->controller_->setAlertnessRadius(value);
-    }
-    float Turret::getAlertnessRadius()
-    {
-        return this->controller_->getAlertnessRadius();
+        return; //Standard turrets don't roll
     }
 
     void Turret::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     {
+        XMLPortParam(Turret, "maxPitch", setMaxPitch, getMaxPitch, xmlelement, mode);
+        XMLPortParam(Turret, "maxYaw", setMaxYaw, getMaxYaw, xmlelement, mode);
         SUPER(Turret, XMLPort, xmlelement, mode);
-        XMLPortParam(Turret, "alertnessRadius", setAlertnessRadius, getAlertnessRadius, xmlelement, mode).defaultValues("400");
     }
 
     void Turret::tick(float dt)
     {
-        orxout() << "Pitch: " << this->getOrientation().getPitch() << "\tYaw: " << this->getOrientation().getYaw() << "\tRoll: " << this->getOrientation().getRoll() << endl;
         if(!gotOrient_)
         {
-            startOrient_ = this->getOrientation();
+            startOrientInv_ = this->getOrientation().Inverse();
             gotOrient_ = true;
         }
+        Quaternion drot = startOrientInv_ * this->getOrientation();
+        orxout() << "Pitch: " << drot.getPitch(false).valueDegrees() << "\tYaw: " << drot.getYaw(false).valueDegrees() << "\tRoll: " << drot.getRoll(false).valueDegrees() << endl;
         SUPER(Turret, tick, dt);
     }
 
+
+    Ogre::Real Turret::boundBetween(Ogre::Real val, Ogre::Real lowerBound, Ogre::Real upperBound)
+    {
+        if (lowerBound > upperBound){ std::swap(lowerBound, upperBound); }
+        val -= lowerBound; //adjust to 0
+        Ogre::Real rangeSize = upperBound - lowerBound;
+        if (rangeSize == 0){ return upperBound; } //avoid dividing by 0
+        return val - (rangeSize * std::floor(val / rangeSize)) + lowerBound;
+    }
+
 }
Index: code/branches/turretFS14/src/modules/objects/Turret.h
===================================================================
--- code/branches/turretFS14/src/modules/objects/Turret.h	(revision 10008)
+++ code/branches/turretFS14/src/modules/objects/Turret.h	(revision 10018)
@@ -40,4 +40,5 @@
 
 #include "worldentities/pawns/SpaceShip.h"
+#include "controllers/TurretController.h"
 
 namespace orxonox
@@ -53,16 +54,26 @@
             virtual void rotateRoll(const Vector2& value);
 
-            void setAlertnessRadius(float value);
-            float getAlertnessRadius();
-
             virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
             virtual void tick(float dt);
 
+            inline void setMaxPitch(Ogre::Real pitch)
+                {this->maxPitch_ = pitch;}
 
-        protected:
-            WaypointPatrolController* controller_;
+            inline Ogre::Real getMaxPitch()
+                {return this->maxPitch_;}
+
+            inline void setMaxYaw(Ogre::Real yaw)
+                {this->maxYaw_ = yaw;}
+
+            inline Ogre::Real getMaxYaw()
+                {return this->maxYaw_;}
+
         private:
             bool gotOrient_;
-            Quaternion startOrient_;
+            Ogre::Real maxPitch_;
+            Ogre::Real maxYaw_;
+            Quaternion startOrientInv_;
+
+            Ogre::Real boundBetween(float val, float lowerBound, float upperBound);
     };
 }
Index: code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc
===================================================================
--- code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc	(revision 10008)
+++ code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc	(revision 10018)
@@ -36,4 +36,6 @@
  	{
  		RegisterObject(TurretController);
+ 		counter = 0;
+ 		flag = false;
  	}
 
@@ -45,5 +47,21 @@
  	void TurretController::tick(float dt)
  	{
- 		this->getControllableEntity()->rotateYaw(10*dt);
+ 		counter += dt;
+ 		if(counter >= 10)
+ 		{
+ 			counter = 0;
+ 			flag = !flag;
+ 			orxout() << "Direction change" << endl;
+ 		}
+ 		if(flag)
+ 		{
+ 			this->getControllableEntity()->rotatePitch(10*dt);
+ 			//this->getControllableEntity()->rotateYaw(10*dt);
+ 		}
+ 		else
+ 		{
+ 			this->getControllableEntity()->rotatePitch(-10*dt);
+ 			//this->getControllableEntity()->rotateYaw(-10*dt);
+ 		}
  	}
  }
Index: code/branches/turretFS14/src/modules/objects/controllers/TurretController.h
===================================================================
--- code/branches/turretFS14/src/modules/objects/controllers/TurretController.h	(revision 10008)
+++ code/branches/turretFS14/src/modules/objects/controllers/TurretController.h	(revision 10018)
@@ -42,4 +42,8 @@
 
  			virtual void tick(float dt);
+
+ 		private:
+ 			float counter;
+ 			bool flag;
  	};
  }
