[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); |
---|
[10031] | 46 | this->startOrient_ = Quaternion::IDENTITY; |
---|
| 47 | this->startDir_ = Vector3::ZERO; |
---|
| 48 | this->localZ_ = Vector3::UNIT_Z; |
---|
| 49 | this->localY_ = Vector3::UNIT_Y; |
---|
| 50 | this->localX_ = Vector3::UNIT_X; |
---|
[10018] | 51 | this->maxPitch_ = 0; |
---|
| 52 | this->maxYaw_ = 0; |
---|
[10031] | 53 | this->attackRadius_ = 200; |
---|
[10018] | 54 | this->gotOrient_ = false; |
---|
[10021] | 55 | this->rotationThrust_ = 50; |
---|
| 56 | |
---|
| 57 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
[9460] | 58 | } |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * @brief Destructor |
---|
| 62 | */ |
---|
| 63 | Turret::~Turret() |
---|
| 64 | { |
---|
| 65 | |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | void Turret::rotatePitch(const Vector2& value) |
---|
[10031] | 70 | { |
---|
| 71 | //This is a failed attempt at limiting the turret's rotation. It's handled in the controller (for now?) |
---|
| 72 | /* |
---|
| 73 | Vector3 currentDir = getTransformedVector(this->getOrientation() * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_); |
---|
| 74 | Vector3 currentDirProjected = currentDir; |
---|
| 75 | currentDirProjected.x = 0; |
---|
| 76 | Vector3 startDirProjected = this->startDir_; |
---|
| 77 | startDirProjected.x = 0; |
---|
| 78 | Ogre::Real angle = startDirProjected.angleBetween(currentDirProjected).valueDegrees(); |
---|
| 79 | //orxout() << "Pitch: " << angle << endl; |
---|
| 80 | //if(angle < this->maxPitch_ || (currentDirProjected.y <= 0 && value.x > 0) || (currentDirProjected.y > 0 && value.x < 0) ) |
---|
[10004] | 81 | { |
---|
[10021] | 82 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f); |
---|
[10004] | 83 | } |
---|
[10031] | 84 | */ |
---|
| 85 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f); |
---|
[10004] | 86 | } |
---|
[9499] | 87 | |
---|
[10004] | 88 | void Turret::rotateYaw(const Vector2& value) |
---|
| 89 | { |
---|
[10031] | 90 | //This is a failed attempt at limiting the turret's rotation. It's handled in the controller (for now?) |
---|
| 91 | /* |
---|
| 92 | Vector3 currentDir = getTransformedVector(this->getOrientation() * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_); |
---|
| 93 | Vector3 currentDirProjected = currentDir; |
---|
| 94 | currentDirProjected.y = 0; |
---|
| 95 | Vector3 startDirProjected = this->startDir_; |
---|
| 96 | startDirProjected.y = 0; |
---|
| 97 | Ogre::Real angle = startDirProjected.angleBetween(currentDirProjected).valueDegrees(); |
---|
| 98 | orxout() << "Yaw: " << angle << endl; |
---|
| 99 | if(angle < this->maxYaw_ || (currentDirProjected.x <= 0 && value.x < 0) || (currentDirProjected.x > 0 && value.x > 0)) |
---|
[10018] | 100 | { |
---|
[10021] | 101 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f); |
---|
[10018] | 102 | } |
---|
[10031] | 103 | */ |
---|
| 104 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f); |
---|
| 105 | } |
---|
[9499] | 106 | |
---|
[10031] | 107 | void Turret::rotateRoll(const Vector2& value) |
---|
| 108 | { |
---|
| 109 | this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x*0.8f); |
---|
| 110 | } |
---|
[10018] | 111 | |
---|
[10031] | 112 | bool Turret::isInRange(Vector3 position) |
---|
| 113 | { |
---|
| 114 | Vector3 distance = position - this->getPosition(); |
---|
| 115 | if(distance.squaredLength() > (this->attackRadius_ * this->attackRadius_)) |
---|
[10004] | 116 | { |
---|
[10031] | 117 | return false; |
---|
[10004] | 118 | } |
---|
[10031] | 119 | |
---|
| 120 | Vector3 dir = getTransformedVector(distance, this->localX_, this->localY_, this->localZ_); |
---|
| 121 | Vector3 dirProjected = dir; |
---|
| 122 | dirProjected.x = 0; |
---|
| 123 | Vector3 startDirProjected = this->startDir_; |
---|
| 124 | startDirProjected.x = 0; |
---|
| 125 | Ogre::Real angle = startDirProjected.angleBetween(dirProjected).valueDegrees(); |
---|
| 126 | if(angle > this->maxPitch_) |
---|
[10004] | 127 | { |
---|
[10031] | 128 | return false; |
---|
[10018] | 129 | } |
---|
[10031] | 130 | |
---|
| 131 | dirProjected = dir; |
---|
| 132 | dirProjected.y = 0; |
---|
| 133 | startDirProjected = this->startDir_; |
---|
| 134 | startDirProjected.y = 0; |
---|
| 135 | angle = startDirProjected.angleBetween(dirProjected).valueDegrees(); |
---|
| 136 | if(angle > this->maxYaw_) |
---|
[10004] | 137 | { |
---|
[10031] | 138 | return false; |
---|
[10004] | 139 | } |
---|
[10031] | 140 | return true; |
---|
[9460] | 141 | } |
---|
| 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); |
---|
[10031] | 147 | XMLPortParam(Turret, "attackRadius", setAttackRadius, getAttackRadius, xmlelement, mode); |
---|
[9460] | 148 | SUPER(Turret, XMLPort, xmlelement, mode); |
---|
[9469] | 149 | } |
---|
[9460] | 150 | |
---|
[10004] | 151 | void Turret::tick(float dt) |
---|
| 152 | { |
---|
[10021] | 153 | SUPER(Turret, tick, dt); |
---|
| 154 | |
---|
[10004] | 155 | if(!gotOrient_) |
---|
| 156 | { |
---|
[10031] | 157 | this->startOrient_ = this->getOrientation(); |
---|
| 158 | this->localX_ = this->startOrient_ * this->localX_; |
---|
| 159 | this->localX_.normalise(); |
---|
| 160 | this->localY_ = this->startOrient_ * this->localY_; |
---|
| 161 | this->localY_.normalise(); |
---|
| 162 | this->localZ_ = this->startOrient_ * this->localZ_; |
---|
| 163 | this->localZ_.normalise(); |
---|
| 164 | |
---|
| 165 | //startDir should always be (0,0,-1) |
---|
| 166 | this->startDir_ = getTransformedVector(this->startOrient_ * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_); |
---|
| 167 | |
---|
| 168 | this->gotOrient_ = true; |
---|
[10004] | 169 | } |
---|
[10031] | 170 | |
---|
[10021] | 171 | this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; |
---|
| 172 | this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); |
---|
| 173 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
[10004] | 174 | } |
---|
[9460] | 175 | |
---|
| 176 | } |
---|