[Orxonox-commit 5355] r10018 - in code/branches/turretFS14: data/levels data/levels/templates src/modules/objects src/modules/objects/controllers

muemart at orxonox.net muemart at orxonox.net
Thu Apr 3 14:57:06 CEST 2014


Author: muemart
Date: 2014-04-03 14:57:05 +0200 (Thu, 03 Apr 2014)
New Revision: 10018

Modified:
   code/branches/turretFS14/data/levels/templates/spaceshipTurretTest.oxt
   code/branches/turretFS14/data/levels/turretTest.oxw
   code/branches/turretFS14/src/modules/objects/Turret.cc
   code/branches/turretFS14/src/modules/objects/Turret.h
   code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc
   code/branches/turretFS14/src/modules/objects/controllers/TurretController.h
Log:
Turret: Limit turret's rotation (not really precise though) & implement xml stuff

Modified: code/branches/turretFS14/data/levels/templates/spaceshipTurretTest.oxt
===================================================================
--- code/branches/turretFS14/data/levels/templates/spaceshipTurretTest.oxt	2014-04-03 12:35:54 UTC (rev 10017)
+++ code/branches/turretFS14/data/levels/templates/spaceshipTurretTest.oxt	2014-04-03 12:57:05 UTC (rev 10018)
@@ -38,7 +38,7 @@
    angularDamping    = 0.9999999
   >
     <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>
         <SphereCollisionShape radius="10"   position = "0,0,0"/>

Modified: code/branches/turretFS14/data/levels/turretTest.oxw
===================================================================
--- code/branches/turretFS14/data/levels/turretTest.oxw	2014-04-03 12:35:54 UTC (rev 10017)
+++ code/branches/turretFS14/data/levels/turretTest.oxw	2014-04-03 12:57:05 UTC (rev 10018)
@@ -41,7 +41,7 @@
         <Model position="0,0,0" mesh="turretSocketLeft.mesh" scale3D="10,10,10" />
         <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 />
             </templates>

Modified: code/branches/turretFS14/src/modules/objects/Turret.cc
===================================================================
--- code/branches/turretFS14/src/modules/objects/Turret.cc	2014-04-03 12:35:54 UTC (rev 10017)
+++ code/branches/turretFS14/src/modules/objects/Turret.cc	2014-04-03 12:57:05 UTC (rev 10018)
@@ -44,8 +44,10 @@
     Turret::Turret(Context* context) : SpaceShip(context)
     {
         RegisterObject(Turret);
-        this->controller_ = new WaypointPatrolController(this->getContext());
-        gotOrient_ = false;
+        this->startOrientInv_ = Quaternion::IDENTITY;
+        this->maxPitch_ = 0;
+        this->maxYaw_ = 0;
+        this->gotOrient_ = false;
     }
 
     /**
@@ -59,89 +61,110 @@
 
     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);
+        return; //Standard turrets don't roll
     }
 
-    void Turret::setAlertnessRadius(float value)
-    {
-        this->controller_->setAlertnessRadius(value);
-    }
-    float Turret::getAlertnessRadius()
-    {
-        return this->controller_->getAlertnessRadius();
-    }
-
     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;
+    }
+
 }

Modified: code/branches/turretFS14/src/modules/objects/Turret.h
===================================================================
--- code/branches/turretFS14/src/modules/objects/Turret.h	2014-04-03 12:35:54 UTC (rev 10017)
+++ code/branches/turretFS14/src/modules/objects/Turret.h	2014-04-03 12:57:05 UTC (rev 10018)
@@ -39,6 +39,7 @@
 #include "OgreQuaternion.h"
 
 #include "worldentities/pawns/SpaceShip.h"
+#include "controllers/TurretController.h"
 
 namespace orxonox
 {
@@ -52,18 +53,28 @@
             virtual void rotateYaw(const Vector2& value);
             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);
     };
 }
 

Modified: code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc
===================================================================
--- code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc	2014-04-03 12:35:54 UTC (rev 10017)
+++ code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc	2014-04-03 12:57:05 UTC (rev 10018)
@@ -35,6 +35,8 @@
  	TurretController::TurretController(Context* context) : ArtificialController(context)
  	{
  		RegisterObject(TurretController);
+ 		counter = 0;
+ 		flag = false;
  	}
 
  	TurretController::~TurretController()
@@ -44,6 +46,22 @@
 
  	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);
+ 		}
  	}
  }
\ No newline at end of file

Modified: code/branches/turretFS14/src/modules/objects/controllers/TurretController.h
===================================================================
--- code/branches/turretFS14/src/modules/objects/controllers/TurretController.h	2014-04-03 12:35:54 UTC (rev 10017)
+++ code/branches/turretFS14/src/modules/objects/controllers/TurretController.h	2014-04-03 12:57:05 UTC (rev 10018)
@@ -41,6 +41,10 @@
  			virtual ~TurretController();
 
  			virtual void tick(float dt);
+
+ 		private:
+ 			float counter;
+ 			bool flag;
  	};
  }
 




More information about the Orxonox-commit mailing list