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