| [9460] | 1 | /* | 
|---|
|  | 2 | *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
|  | 3 | *                    > www.orxonox.net < | 
|---|
|  | 4 | * | 
|---|
|  | 5 | * | 
|---|
|  | 6 | *   License notice: | 
|---|
|  | 7 | * | 
|---|
|  | 8 | *   This program is free software; you can redistribute it and/or | 
|---|
|  | 9 | *   modify it under the terms of the GNU General Public License | 
|---|
|  | 10 | *   as published by the Free Software Foundation; either version 2 | 
|---|
|  | 11 | *   of the License, or (at your option) any later version. | 
|---|
|  | 12 | * | 
|---|
|  | 13 | *   This program is distributed in the hope that it will be useful, | 
|---|
|  | 14 | *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 15 | *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 16 | *   GNU General Public License for more details. | 
|---|
|  | 17 | * | 
|---|
|  | 18 | *   You should have received a copy of the GNU General Public License | 
|---|
|  | 19 | *   along with this program; if not, write to the Free Software | 
|---|
|  | 20 | *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
|  | 21 | * | 
|---|
|  | 22 | *   Author: | 
|---|
|  | 23 | *      Marian Runo | 
|---|
|  | 24 | *   Co-authors: | 
|---|
|  | 25 | *      ... | 
|---|
|  | 26 | * | 
|---|
|  | 27 | */ | 
|---|
|  | 28 |  | 
|---|
|  | 29 | #include "Turret.h" | 
|---|
|  | 30 | #include "core/CoreIncludes.h" | 
|---|
|  | 31 | #include "core/XMLPort.h" | 
|---|
| [10021] | 32 | #include "BulletDynamics/Dynamics/btRigidBody.h" | 
|---|
| [9460] | 33 |  | 
|---|
|  | 34 | namespace orxonox | 
|---|
|  | 35 | { | 
|---|
| [9667] | 36 | RegisterClass(Turret); | 
|---|
| [9460] | 37 |  | 
|---|
| [10004] | 38 |  | 
|---|
|  | 39 |  | 
|---|
| [9460] | 40 | /** | 
|---|
|  | 41 | * @brief Constructor | 
|---|
|  | 42 | */ | 
|---|
| [10021] | 43 | Turret::Turret(Context* context) : Pawn(context) | 
|---|
| [9460] | 44 | { | 
|---|
|  | 45 | RegisterObject(Turret); | 
|---|
| [10018] | 46 | this->startOrientInv_ = Quaternion::IDENTITY; | 
|---|
|  | 47 | this->maxPitch_ = 0; | 
|---|
|  | 48 | this->maxYaw_ = 0; | 
|---|
|  | 49 | this->gotOrient_ = false; | 
|---|
| [10021] | 50 | this->rotationThrust_ = 50; | 
|---|
|  | 51 |  | 
|---|
|  | 52 | this->localAngularAcceleration_.setValue(0, 0, 0); | 
|---|
| [9460] | 53 | } | 
|---|
|  | 54 |  | 
|---|
|  | 55 | /** | 
|---|
|  | 56 | * @brief Destructor | 
|---|
|  | 57 | */ | 
|---|
|  | 58 | Turret::~Turret() | 
|---|
|  | 59 | { | 
|---|
|  | 60 |  | 
|---|
|  | 61 | } | 
|---|
|  | 62 |  | 
|---|
|  | 63 |  | 
|---|
|  | 64 | void Turret::rotatePitch(const Vector2& value) | 
|---|
|  | 65 | { | 
|---|
| [10018] | 66 | if (this->maxPitch_ == 0) | 
|---|
| [10004] | 67 | { | 
|---|
| [10018] | 68 | return; | 
|---|
| [10004] | 69 | } | 
|---|
| [10018] | 70 | if (this->maxPitch_ >= 180) //no need to check, if the limit too big | 
|---|
| [10004] | 71 | { | 
|---|
| [10021] | 72 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f); | 
|---|
|  | 73 | return; | 
|---|
| [10004] | 74 | } | 
|---|
| [10018] | 75 |  | 
|---|
|  | 76 | Quaternion drot = startOrientInv_ * this->getOrientation(); | 
|---|
|  | 77 |  | 
|---|
|  | 78 | Ogre::Real val = boundBetween(drot.getPitch(false).valueDegrees(), -180, 180); | 
|---|
|  | 79 | Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180); | 
|---|
|  | 80 | Ogre::Real lowerBound = offset - this->maxPitch_; | 
|---|
|  | 81 | Ogre::Real upperBound = offset + this->maxPitch_; | 
|---|
|  | 82 | if (lowerBound < -180) //Avoid wrapping around of the boundaries | 
|---|
| [10004] | 83 | { | 
|---|
| [10018] | 84 | lowerBound += this->maxPitch_; | 
|---|
|  | 85 | upperBound += this->maxPitch_; | 
|---|
|  | 86 | val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here | 
|---|
| [10004] | 87 | } | 
|---|
| [10018] | 88 | else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side) | 
|---|
|  | 89 | { | 
|---|
|  | 90 | lowerBound -= this->maxPitch_; | 
|---|
|  | 91 | upperBound -= this->maxPitch_; | 
|---|
|  | 92 | val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here | 
|---|
|  | 93 | } | 
|---|
|  | 94 | if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0)) | 
|---|
|  | 95 | { | 
|---|
| [10021] | 96 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f); | 
|---|
| [10018] | 97 | } | 
|---|
|  | 98 | return; | 
|---|
| [10004] | 99 | } | 
|---|
| [9499] | 100 |  | 
|---|
| [10004] | 101 | void Turret::rotateYaw(const Vector2& value) | 
|---|
|  | 102 | { | 
|---|
| [10018] | 103 | if (this->maxPitch_ == 0) | 
|---|
|  | 104 | { | 
|---|
|  | 105 | return; | 
|---|
|  | 106 | } | 
|---|
|  | 107 | if (this->maxPitch_ >= 180) //no need to check, if the limit too big | 
|---|
|  | 108 | { | 
|---|
| [10021] | 109 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f); | 
|---|
|  | 110 | return; | 
|---|
| [10018] | 111 | } | 
|---|
| [9499] | 112 |  | 
|---|
| [10018] | 113 | Quaternion drot = startOrientInv_ * this->getOrientation(); | 
|---|
|  | 114 |  | 
|---|
|  | 115 | Ogre::Real val = boundBetween(drot.getYaw(false).valueDegrees(), -180, 180); | 
|---|
|  | 116 | Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180); | 
|---|
|  | 117 | Ogre::Real lowerBound = offset - this->maxPitch_; | 
|---|
|  | 118 | Ogre::Real upperBound = offset + this->maxPitch_; | 
|---|
|  | 119 | if (lowerBound < -180) //Avoid wrapping around of the boundaries | 
|---|
| [10004] | 120 | { | 
|---|
| [10018] | 121 | lowerBound += this->maxPitch_; | 
|---|
|  | 122 | upperBound += this->maxPitch_; | 
|---|
|  | 123 | val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here | 
|---|
| [10004] | 124 | } | 
|---|
| [10018] | 125 | else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side) | 
|---|
| [10004] | 126 | { | 
|---|
| [10018] | 127 | lowerBound -= this->maxPitch_; | 
|---|
|  | 128 | upperBound -= this->maxPitch_; | 
|---|
|  | 129 | val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here | 
|---|
|  | 130 | } | 
|---|
|  | 131 | if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0)) | 
|---|
| [10004] | 132 | { | 
|---|
| [10021] | 133 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f); | 
|---|
| [10004] | 134 | } | 
|---|
| [10018] | 135 | return; | 
|---|
| [9460] | 136 | } | 
|---|
|  | 137 |  | 
|---|
| [10004] | 138 | void Turret::rotateRoll(const Vector2& value) | 
|---|
|  | 139 | { | 
|---|
| [10018] | 140 | return; //Standard turrets don't roll | 
|---|
| [10004] | 141 | } | 
|---|
| [9460] | 142 |  | 
|---|
| [9469] | 143 | void Turret::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| [9460] | 144 | { | 
|---|
| [10018] | 145 | XMLPortParam(Turret, "maxPitch", setMaxPitch, getMaxPitch, xmlelement, mode); | 
|---|
|  | 146 | XMLPortParam(Turret, "maxYaw", setMaxYaw, getMaxYaw, xmlelement, mode); | 
|---|
| [9460] | 147 | SUPER(Turret, XMLPort, xmlelement, mode); | 
|---|
| [9469] | 148 | } | 
|---|
| [9460] | 149 |  | 
|---|
| [10004] | 150 | void Turret::tick(float dt) | 
|---|
|  | 151 | { | 
|---|
| [10021] | 152 | SUPER(Turret, tick, dt); | 
|---|
|  | 153 |  | 
|---|
| [10004] | 154 | if(!gotOrient_) | 
|---|
|  | 155 | { | 
|---|
| [10018] | 156 | startOrientInv_ = this->getOrientation().Inverse(); | 
|---|
| [10004] | 157 | gotOrient_ = true; | 
|---|
|  | 158 | } | 
|---|
| [10018] | 159 | Quaternion drot = startOrientInv_ * this->getOrientation(); | 
|---|
|  | 160 | orxout() << "Pitch: " << drot.getPitch(false).valueDegrees() << "\tYaw: " << drot.getYaw(false).valueDegrees() << "\tRoll: " << drot.getRoll(false).valueDegrees() << endl; | 
|---|
| [10021] | 161 |  | 
|---|
|  | 162 | this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; | 
|---|
|  | 163 | this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); | 
|---|
|  | 164 | this->localAngularAcceleration_.setValue(0, 0, 0); | 
|---|
| [10004] | 165 | } | 
|---|
| [9460] | 166 |  | 
|---|
| [10018] | 167 |  | 
|---|
|  | 168 | Ogre::Real Turret::boundBetween(Ogre::Real val, Ogre::Real lowerBound, Ogre::Real upperBound) | 
|---|
|  | 169 | { | 
|---|
|  | 170 | if (lowerBound > upperBound){ std::swap(lowerBound, upperBound); } | 
|---|
|  | 171 | val -= lowerBound; //adjust to 0 | 
|---|
|  | 172 | Ogre::Real rangeSize = upperBound - lowerBound; | 
|---|
|  | 173 | if (rangeSize == 0){ return upperBound; } //avoid dividing by 0 | 
|---|
|  | 174 | return val - (rangeSize * std::floor(val / rangeSize)) + lowerBound; | 
|---|
|  | 175 | } | 
|---|
|  | 176 |  | 
|---|
| [9460] | 177 | } | 
|---|