[10871] | 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 |
---|
[10946] | 11 | * of the License, or (at your option)any later version. |
---|
[10871] | 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: |
---|
[10885] | 23 | * Gani Aliguzhinov |
---|
| 24 | |
---|
[10871] | 25 | * Co-authors: |
---|
[10923] | 26 | * ... |
---|
[10871] | 27 | * |
---|
| 28 | */ |
---|
[10946] | 29 | #include <OgreMatrix3.h> //for Quaternion manipulations |
---|
| 30 | |
---|
| 31 | #include "util/Math.h" |
---|
| 32 | #include "core/XMLPort.h" |
---|
[10871] | 33 | #include "controllers/FlyingController.h" |
---|
[10885] | 34 | |
---|
[10946] | 35 | #include "worldentities/pawns/SpaceShip.h" //for boosting |
---|
| 36 | |
---|
[10871] | 37 | namespace orxonox |
---|
| 38 | { |
---|
| 39 | RegisterClass (FlyingController); |
---|
| 40 | |
---|
[10885] | 41 | FlyingController::FlyingController(Context* context): CommonController(context) |
---|
[10871] | 42 | { |
---|
[10885] | 43 | RegisterObject(FlyingController); |
---|
[10879] | 44 | this->rotationProgress_ = 0; |
---|
[10871] | 45 | this->spread_ = 200; |
---|
[10888] | 46 | this->tolerance_ = 80; |
---|
[10871] | 47 | } |
---|
| 48 | FlyingController::~FlyingController() |
---|
| 49 | { |
---|
| 50 | } |
---|
[10877] | 51 | |
---|
[10885] | 52 | void FlyingController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[10871] | 53 | { |
---|
[10885] | 54 | XMLPortParam(FlyingController, "spread", setSpread, getSpread, xmlelement, mode); |
---|
| 55 | XMLPortParam(FlyingController, "formationMode", setFormationModeXML, getFormationModeXML, xmlelement, mode); |
---|
| 56 | SUPER(FlyingController, XMLPort, xmlelement, mode); |
---|
[10871] | 57 | } |
---|
| 58 | |
---|
[10885] | 59 | void FlyingController::setFormationModeXML(std::string val) |
---|
[10871] | 60 | { |
---|
[10885] | 61 | const std::string valUpper = getUppercase(val); |
---|
[10871] | 62 | FormationMode::Value value; |
---|
| 63 | |
---|
[10946] | 64 | if (valUpper == "WALL") |
---|
[10871] | 65 | value = FormationMode::WALL; |
---|
[10946] | 66 | else if (valUpper == "FINGER4") |
---|
[10871] | 67 | value = FormationMode::FINGER4; |
---|
[10946] | 68 | else if (valUpper == "DIAMOND") |
---|
[10871] | 69 | value = FormationMode::DIAMOND; |
---|
| 70 | else |
---|
[10946] | 71 | ThrowException(ParseError, std::string("Attempting to set an unknown FormationMode: '")+ val + "'."); |
---|
[10885] | 72 | this->setFormationMode(value); |
---|
[10871] | 73 | } |
---|
[10885] | 74 | std::string FlyingController::getFormationModeXML() const |
---|
[10871] | 75 | { |
---|
[10946] | 76 | switch (this->formationMode_) |
---|
[10871] | 77 | { |
---|
| 78 | case FormationMode::WALL: |
---|
[10923] | 79 | { return "WALL"; } |
---|
[10871] | 80 | case FormationMode::FINGER4: |
---|
[10923] | 81 | { return "FINGER4"; } |
---|
[10871] | 82 | case FormationMode::DIAMOND: |
---|
[10923] | 83 | { return "DIAMOND"; } |
---|
[10871] | 84 | default: |
---|
[10923] | 85 | return "DIAMOND"; |
---|
[10871] | 86 | } |
---|
| 87 | } |
---|
| 88 | void FlyingController::stopMoving() |
---|
| 89 | { |
---|
| 90 | this->bHasTargetPosition_ = false; |
---|
| 91 | } |
---|
[10946] | 92 | /** |
---|
| 93 | @brief |
---|
| 94 | if distance to targetPosition is smaller than this->tolerance_, no moving should be made, otherwise |
---|
| 95 | find amount of yaw and pitch that have to be applied, so that ship looks at targetPosition, then |
---|
| 96 | ship is moved forward towards targetPosition. Also target orientation is being applied. |
---|
| 97 | */ |
---|
[10923] | 98 | void FlyingController::moveToPosition(const Vector3& targetPosition, float dt) |
---|
[10871] | 99 | { |
---|
[10923] | 100 | if (!this->getControllableEntity()) |
---|
| 101 | return; |
---|
[10871] | 102 | ControllableEntity* entity = this->getControllableEntity(); |
---|
| 103 | |
---|
[10946] | 104 | float distance = (targetPosition - entity->getPosition()).length(); |
---|
[10871] | 105 | |
---|
[10946] | 106 | if (distance >= this->tolerance_) |
---|
[10871] | 107 | { |
---|
[10946] | 108 | //function that calculates how much yaw and pitch are to be applied |
---|
[10880] | 109 | Vector2 coord = get2DViewCoordinates |
---|
[10946] | 110 | (entity->getPosition() , |
---|
[10880] | 111 | entity->getOrientation() * WorldEntity::FRONT, |
---|
| 112 | entity->getOrientation() * WorldEntity::UP, |
---|
[10946] | 113 | targetPosition); |
---|
| 114 | //limit yaw and pitch by [-1,1] |
---|
| 115 | float rotateX = -clamp(coord.x * 10, -1.0f, 1.0f); |
---|
| 116 | float rotateY = clamp(coord.y * 10, -1.0f, 1.0f); |
---|
| 117 | |
---|
| 118 | if (!entity) |
---|
| 119 | return; |
---|
| 120 | |
---|
| 121 | //apply yaw and pitch |
---|
| 122 | entity->rotateYaw(ROTATEFACTOR * rotateX * dt); |
---|
| 123 | entity->rotatePitch(ROTATEFACTOR * rotateY * dt); |
---|
| 124 | |
---|
| 125 | if (!entity) |
---|
| 126 | return; |
---|
| 127 | |
---|
| 128 | //only move either if ship looks at target with a certain tolerance, or if ship is far enough for it to be ok to move in a curve. |
---|
[10923] | 129 | if (distance > this->tolerance_*1.5f || (rotateX > -0.03 && rotateX < 0.03 && rotateY > -0.03 && rotateY < 0.03)) |
---|
[10946] | 130 | entity->moveFrontBack(SPEED * dt); |
---|
| 131 | //roll |
---|
| 132 | copyTargetOrientation(dt); |
---|
[10871] | 133 | } |
---|
| 134 | else |
---|
| 135 | { |
---|
| 136 | bHasTargetPosition_ = false; |
---|
| 137 | } |
---|
| 138 | } |
---|
[10946] | 139 | /** |
---|
| 140 | @brief |
---|
| 141 | fly towards a preset targetPosition_ |
---|
| 142 | */ |
---|
[10871] | 143 | void FlyingController::moveToTargetPosition(float dt) |
---|
| 144 | { |
---|
| 145 | this->moveToPosition (this->targetPosition_, dt); |
---|
| 146 | } |
---|
[10946] | 147 | /** |
---|
| 148 | @brief |
---|
| 149 | roll ship so that it has same roll as orient |
---|
| 150 | */ |
---|
[10885] | 151 | void FlyingController::copyOrientation(const Quaternion& orient, float dt) |
---|
[10871] | 152 | { |
---|
[10923] | 153 | //copied from |
---|
[10879] | 154 | //http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Quaternion+and+Rotation+Primer&structure=Tutorials#Q._How_can_I_make_my_objects_rotate_smoothly_You_mentioned_slerp_etc_ |
---|
| 155 | //how can I make my objects rotate smoothly? |
---|
[10923] | 156 | if (!this->getControllableEntity()) |
---|
| 157 | return; |
---|
[10879] | 158 | Quaternion myOrient = this->getControllableEntity()->getOrientation(); |
---|
[10881] | 159 | this->rotationProgress_ += dt; |
---|
[10875] | 160 | |
---|
[10879] | 161 | if (this->rotationProgress_ > 1) |
---|
| 162 | { |
---|
| 163 | this->rotationProgress_ = 0; |
---|
[10881] | 164 | this->bHasTargetOrientation_ = false; |
---|
[10879] | 165 | } |
---|
| 166 | else |
---|
| 167 | { |
---|
[10923] | 168 | Quaternion deltaOrientation = Quaternion::Slerp(rotationProgress_, myOrient, orient, true); |
---|
[10881] | 169 | |
---|
[10923] | 170 | Matrix3 deltaMatrix, myMatrix; |
---|
[10881] | 171 | |
---|
[10923] | 172 | deltaOrientation.ToRotationMatrix(deltaMatrix); |
---|
[10879] | 173 | myOrient.ToRotationMatrix (myMatrix); |
---|
[10875] | 174 | |
---|
[10923] | 175 | Radian yawDelta, pitchDelta, rollDelta, yawMy, pitchMy, rollMy; |
---|
| 176 | deltaMatrix.ToEulerAnglesYXZ(yawDelta, pitchDelta, rollDelta); |
---|
| 177 | myMatrix.ToEulerAnglesYXZ (yawMy, pitchMy, rollMy); |
---|
[10881] | 178 | |
---|
[10923] | 179 | if (!this->getControllableEntity()) |
---|
| 180 | return; |
---|
| 181 | this->getControllableEntity()->rotateRoll ((rollDelta.valueRadians() - rollMy.valueRadians())*ROTATEFACTOR*dt); |
---|
[10879] | 182 | } |
---|
[10871] | 183 | } |
---|
[10946] | 184 | /** |
---|
| 185 | @brief |
---|
| 186 | roll ship so that it has same roll as a preset targetOrientation_ |
---|
| 187 | */ |
---|
[10885] | 188 | void FlyingController::copyTargetOrientation(float dt) |
---|
[10871] | 189 | { |
---|
[10885] | 190 | if (bHasTargetOrientation_) |
---|
[10871] | 191 | { |
---|
[10923] | 192 | this->copyOrientation(targetOrientation_, dt); |
---|
[10871] | 193 | } |
---|
| 194 | } |
---|
[10946] | 195 | /** |
---|
| 196 | @brief |
---|
| 197 | set Vector to fly to |
---|
| 198 | */ |
---|
[10885] | 199 | void FlyingController::setTargetPosition(const Vector3& target) |
---|
[10871] | 200 | { |
---|
| 201 | this->targetPosition_ = target; |
---|
| 202 | this->bHasTargetPosition_ = true; |
---|
| 203 | } |
---|
[10946] | 204 | /** |
---|
| 205 | @brief |
---|
| 206 | set orientation to apply |
---|
| 207 | */ |
---|
[10885] | 208 | void FlyingController::setTargetOrientation(const Quaternion& orient) |
---|
[10871] | 209 | { |
---|
| 210 | this->targetOrientation_=orient; |
---|
| 211 | this->bHasTargetOrientation_=true; |
---|
| 212 | } |
---|
[10946] | 213 | /** |
---|
| 214 | @brief |
---|
| 215 | set orientation to apply |
---|
| 216 | */ |
---|
[10885] | 217 | void FlyingController::setTargetOrientation(ControllableEntity* target) |
---|
[10871] | 218 | { |
---|
[10885] | 219 | if (target) |
---|
[10923] | 220 | this->setTargetOrientation(target->getOrientation()); |
---|
[10871] | 221 | } |
---|
[10946] | 222 | /** |
---|
| 223 | @brief |
---|
| 224 | boost if you can |
---|
| 225 | */ |
---|
[10871] | 226 | void FlyingController::boostControl() |
---|
| 227 | { |
---|
[10923] | 228 | if (!this->getControllableEntity()) |
---|
| 229 | return; |
---|
[10871] | 230 | SpaceShip* ship = orxonox_cast<SpaceShip*>(this->getControllableEntity()); |
---|
| 231 | if(ship == NULL) return; |
---|
[10946] | 232 | if(ship->getBoostPower()*1.5f > ship->getInitialBoostPower()) //upper limit ->boost |
---|
[10871] | 233 | { |
---|
| 234 | this->getControllableEntity()->boost(true); |
---|
| 235 | } |
---|
| 236 | else if(ship->getBoostPower()*4.0f < ship->getInitialBoostPower()) //lower limit ->do not boost |
---|
| 237 | { |
---|
| 238 | this->getControllableEntity()->boost(false); |
---|
| 239 | } |
---|
| 240 | } |
---|
[10946] | 241 | /** |
---|
| 242 | @brief |
---|
| 243 | keep this ship in a formation with its division |
---|
| 244 | */ |
---|
[10886] | 245 | void FlyingController::keepFormation(const ControllableEntity* leaderEntity, Vector3& targetRelativePosition) |
---|
| 246 | { |
---|
[10946] | 247 | if (!this->getControllableEntity()) |
---|
| 248 | return; |
---|
[10886] | 249 | ControllableEntity* myEntity = this->getControllableEntity(); |
---|
| 250 | Vector3 myPosition = myEntity->getWorldPosition(); |
---|
| 251 | |
---|
| 252 | if (!leaderEntity) |
---|
| 253 | { |
---|
| 254 | return; |
---|
| 255 | } |
---|
| 256 | Quaternion orient = leaderEntity->getWorldOrientation(); |
---|
| 257 | Vector3 leaderPosition = leaderEntity->getWorldPosition(); |
---|
| 258 | |
---|
| 259 | if (!leaderEntity) |
---|
| 260 | { |
---|
| 261 | return; |
---|
| 262 | } |
---|
[10946] | 263 | //calculate where in world coordinates this ship should fly |
---|
[10886] | 264 | Vector3 targetAbsolutePosition = |
---|
| 265 | (leaderPosition + (orient*WorldEntity::FRONT) * (leaderEntity->getVelocity().length()/5) |
---|
| 266 | + (orient* (targetRelativePosition))); |
---|
[10946] | 267 | //let ship finish rotating. also don't call copyOrientation too often as it is a slow function. Don't know how to do it different |
---|
[10923] | 268 | if (static_cast<int>(rnd(1.0f) * 100) % 3 == 0) |
---|
[10886] | 269 | this->setTargetOrientation (orient); |
---|
[10946] | 270 | //set a position to fly to |
---|
[10886] | 271 | this->setTargetPosition (targetAbsolutePosition); |
---|
[10946] | 272 | |
---|
| 273 | //boost if too far |
---|
[10886] | 274 | if ((targetAbsolutePosition - myPosition).length() > this->tolerance_ * 1.5f) |
---|
| 275 | { |
---|
| 276 | this->boostControl(); |
---|
| 277 | } |
---|
| 278 | else |
---|
| 279 | { |
---|
[10946] | 280 | if (!this->getControllableEntity()) |
---|
| 281 | return; |
---|
| 282 | this->getControllableEntity()->boost(false); |
---|
[10886] | 283 | } |
---|
| 284 | } |
---|
[10871] | 285 | } |
---|