| 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, Martin Mueller |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * ... |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #include "Turret.h" |
|---|
| 30 | #include "core/CoreIncludes.h" |
|---|
| 31 | #include "core/XMLPort.h" |
|---|
| 32 | |
|---|
| 33 | namespace orxonox |
|---|
| 34 | { |
|---|
| 35 | RegisterClass(Turret); |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * @brief Constructor |
|---|
| 41 | */ |
|---|
| 42 | Turret::Turret(Context* context) : Pawn(context) |
|---|
| 43 | { |
|---|
| 44 | RegisterObject(Turret); |
|---|
| 45 | this->rotationThrust_ = 50; |
|---|
| 46 | this->startDir_ = Vector3::ZERO; |
|---|
| 47 | this->localZ_ = Vector3::UNIT_Z; |
|---|
| 48 | this->localY_ = Vector3::UNIT_Y; |
|---|
| 49 | this->localX_ = Vector3::UNIT_X; |
|---|
| 50 | this->attackRadius_ = 200; |
|---|
| 51 | this->maxPitch_ = 90; |
|---|
| 52 | this->maxYaw_ = 90; |
|---|
| 53 | this->once_ = false; |
|---|
| 54 | this->rotation_ = Quaternion::IDENTITY; |
|---|
| 55 | |
|---|
| 56 | this->setRadarVisibility(false); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * @brief Destructor |
|---|
| 61 | */ |
|---|
| 62 | Turret::~Turret() |
|---|
| 63 | { |
|---|
| 64 | |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | bool Turret::isInRange(const Vector3 &position) |
|---|
| 69 | { |
|---|
| 70 | //Check distance |
|---|
| 71 | Vector3 distance = position - this->getWorldPosition(); |
|---|
| 72 | if(distance.squaredLength() > (this->attackRadius_ * this->attackRadius_)) |
|---|
| 73 | { |
|---|
| 74 | return false; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | //Check pitch |
|---|
| 78 | Vector3 dir = getTransformedVector(distance, this->localX_, this->localY_, this->localZ_); |
|---|
| 79 | Vector3 dirProjected = dir; |
|---|
| 80 | dirProjected.x = 0; |
|---|
| 81 | Vector3 startDirProjected = this->startDir_; |
|---|
| 82 | startDirProjected.x = 0; |
|---|
| 83 | Ogre::Real angle = startDirProjected.angleBetween(dirProjected).valueDegrees(); |
|---|
| 84 | if(angle > this->maxPitch_) |
|---|
| 85 | { |
|---|
| 86 | return false; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | //Check yaw |
|---|
| 90 | dirProjected = dir; |
|---|
| 91 | dirProjected.y = 0; |
|---|
| 92 | startDirProjected = this->startDir_; |
|---|
| 93 | startDirProjected.y = 0; |
|---|
| 94 | angle = startDirProjected.angleBetween(dirProjected).valueDegrees(); |
|---|
| 95 | if(angle > this->maxYaw_) |
|---|
| 96 | { |
|---|
| 97 | return false; |
|---|
| 98 | } |
|---|
| 99 | return true; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | void Turret::aimAtPosition(const Vector3& position) |
|---|
| 103 | { |
|---|
| 104 | Vector3 currDir = this->getWorldOrientation() * WorldEntity::FRONT; |
|---|
| 105 | Vector3 targetDir = position - this->getWorldPosition(); |
|---|
| 106 | |
|---|
| 107 | this->rotation_ = currDir.getRotationTo(targetDir); |
|---|
| 108 | |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | void Turret::rotatePitch(const Vector2& value) |
|---|
| 112 | { |
|---|
| 113 | //This is a failed attempt at limiting the turret's rotation. It's handled in the controller (for now?) |
|---|
| 114 | /* |
|---|
| 115 | Vector3 currentDir = getTransformedVector(this->getOrientation() * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_); |
|---|
| 116 | Vector3 currentDirProjected = currentDir; |
|---|
| 117 | currentDirProjected.x = 0; |
|---|
| 118 | Vector3 startDirProjected = this->startDir_; |
|---|
| 119 | startDirProjected.x = 0; |
|---|
| 120 | Ogre::Real angle = startDirProjected.angleBetween(currentDirProjected).valueDegrees(); |
|---|
| 121 | //orxout() << "Pitch: " << angle << endl; |
|---|
| 122 | //if(angle < this->maxPitch_ || (currentDirProjected.y <= 0 && value.x > 0) || (currentDirProjected.y > 0 && value.x < 0) ) |
|---|
| 123 | { |
|---|
| 124 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f); |
|---|
| 125 | } |
|---|
| 126 | */ |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | void Turret::rotateYaw(const Vector2& value) |
|---|
| 130 | { |
|---|
| 131 | //This is a failed attempt at limiting the turret's rotation. It's handled in the controller (for now?) |
|---|
| 132 | /* |
|---|
| 133 | Vector3 currentDir = getTransformedVector(this->getOrientation() * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_); |
|---|
| 134 | Vector3 currentDirProjected = currentDir; |
|---|
| 135 | currentDirProjected.y = 0; |
|---|
| 136 | Vector3 startDirProjected = this->startDir_; |
|---|
| 137 | startDirProjected.y = 0; |
|---|
| 138 | Ogre::Real angle = startDirProjected.angleBetween(currentDirProjected).valueDegrees(); |
|---|
| 139 | orxout() << "Yaw: " << angle << endl; |
|---|
| 140 | if(angle < this->maxYaw_ || (currentDirProjected.x <= 0 && value.x < 0) || (currentDirProjected.x > 0 && value.x > 0)) |
|---|
| 141 | { |
|---|
| 142 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f); |
|---|
| 143 | } |
|---|
| 144 | */ |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | void Turret::rotateRoll(const Vector2& value) |
|---|
| 148 | { |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | void Turret::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 152 | { |
|---|
| 153 | SUPER(Turret, XMLPort, xmlelement, mode); |
|---|
| 154 | |
|---|
| 155 | XMLPortParamVariable(Turret, "rotationThrust", rotationThrust_, xmlelement, mode); |
|---|
| 156 | XMLPortParam(Turret, "attackRadius", setAttackRadius, getAttackRadius, xmlelement, mode); |
|---|
| 157 | XMLPortParam(Turret, "maxYaw", setMaxYaw, getMaxYaw, xmlelement, mode); |
|---|
| 158 | XMLPortParam(Turret, "maxPitch", setMaxPitch, getMaxPitch, xmlelement, mode); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | void Turret::tick(float dt) |
|---|
| 162 | { |
|---|
| 163 | SUPER(Turret, tick, dt); |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | if(!this->once_) |
|---|
| 167 | { |
|---|
| 168 | |
|---|
| 169 | Quaternion startOrient = this->getOrientation(); |
|---|
| 170 | this->localXStart_ = startOrient * this->localX_; |
|---|
| 171 | this->localXStart_.normalise(); |
|---|
| 172 | this->localX_ = this->localXStart_; |
|---|
| 173 | this->localYStart_ = startOrient * this->localY_; |
|---|
| 174 | this->localYStart_.normalise(); |
|---|
| 175 | this->localY_ = this->localYStart_; |
|---|
| 176 | this->localZStart_ = startOrient * this->localZ_; |
|---|
| 177 | this->localZStart_.normalise(); |
|---|
| 178 | this->localZ_ = this->localZStart_; |
|---|
| 179 | |
|---|
| 180 | //startDir should always be (0,0,-1) |
|---|
| 181 | this->startDir_ = getTransformedVector(startOrient * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_); |
|---|
| 182 | |
|---|
| 183 | this->once_ = true; |
|---|
| 184 | |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | //Adjust local axes to parent's rotation |
|---|
| 188 | WorldEntity* parent = this->getParent(); |
|---|
| 189 | if(parent) |
|---|
| 190 | { |
|---|
| 191 | Quaternion parentrot = parent->getWorldOrientation(); |
|---|
| 192 | this->localX_ = parentrot * this->localXStart_; |
|---|
| 193 | this->localY_ = parentrot * this->localYStart_; |
|---|
| 194 | this->localZ_ = parentrot * this->localZStart_; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | //rotate |
|---|
| 198 | if(this->rotation_ != Quaternion::IDENTITY) |
|---|
| 199 | { |
|---|
| 200 | //Don't make the rotation instantaneous |
|---|
| 201 | Quaternion drot = Quaternion::Slerp(dt*this->rotationThrust_/20.f, Quaternion::IDENTITY, this->rotation_); |
|---|
| 202 | this->rotate(drot, WorldEntity::World); |
|---|
| 203 | this->rotation_ = Quaternion::IDENTITY; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | } |
|---|
| 207 | } |
|---|