| [10719] | 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 | * Fabian 'x3n' Landau |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * Dominik Solenicki |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | #include "controllers/CommonController.h" |
|---|
| 29 | |
|---|
| 30 | namespace orxonox |
|---|
| 31 | { |
|---|
| 32 | |
|---|
| 33 | RegisterClass(CommonController); |
|---|
| [10729] | 34 | static const float SPEED = 0.6f; |
|---|
| 35 | static const float ROTATEFACTOR = 0.2f; |
|---|
| [10719] | 36 | |
|---|
| [10731] | 37 | CommonController::CommonController(Context* context) : Controller(context) |
|---|
| [10719] | 38 | { |
|---|
| [10731] | 39 | |
|---|
| 40 | RegisterObject(CommonController); |
|---|
| [10719] | 41 | } |
|---|
| [10731] | 42 | |
|---|
| 43 | |
|---|
| 44 | CommonController::~CommonController() |
|---|
| [10719] | 45 | { |
|---|
| [10731] | 46 | } |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | bool CommonController::setWingman (CommonController* wingman) |
|---|
| 52 | { |
|---|
| [10719] | 53 | return false; |
|---|
| 54 | } |
|---|
| [10731] | 55 | |
|---|
| [10722] | 56 | bool CommonController::hasWingman() |
|---|
| 57 | { |
|---|
| 58 | return true; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| [10731] | 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | void CommonController::setTargetPosition(const Vector3& target) |
|---|
| [10719] | 65 | { |
|---|
| [10731] | 66 | this->targetPosition_ = target; |
|---|
| 67 | this->bHasTargetPosition_ = true; |
|---|
| 68 | } |
|---|
| [10719] | 69 | |
|---|
| [10731] | 70 | void CommonController::setTargetOrientation(const Quaternion& orient) |
|---|
| 71 | { |
|---|
| 72 | this->targetOrientation_=orient; |
|---|
| 73 | this->bHasTargetOrientation_=true; |
|---|
| [10719] | 74 | } |
|---|
| 75 | |
|---|
| [10731] | 76 | void CommonController::setTargetOrientation(ControllableEntity* target) |
|---|
| 77 | { |
|---|
| 78 | if (target) |
|---|
| 79 | setTargetOrientation(target->getOrientation()); |
|---|
| 80 | } |
|---|
| [10719] | 81 | |
|---|
| [10731] | 82 | /*void CommonController::spin() |
|---|
| [10719] | 83 | { |
|---|
| [10731] | 84 | this->moveToTargetPosition(); |
|---|
| 85 | this->getControllableEntity()->rotateRoll(8.0f); |
|---|
| [10719] | 86 | } |
|---|
| [10731] | 87 | void CommonController::turn180() |
|---|
| 88 | { |
|---|
| 89 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_); |
|---|
| 90 | |
|---|
| 91 | this->getControllableEntity()->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x); |
|---|
| 92 | this->getControllableEntity()->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y); |
|---|
| 93 | |
|---|
| 94 | this->getControllableEntity()->moveFrontBack(SPEED); |
|---|
| 95 | }*/ |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| [10729] | 99 | //copy the Roll orientation of given Quaternion. |
|---|
| 100 | void CommonController::copyOrientation(const Quaternion& orient) |
|---|
| 101 | { |
|---|
| 102 | //roll angle difference in radian |
|---|
| 103 | float diff=orient.getRoll(false).valueRadians()-(this->getControllableEntity()->getOrientation().getRoll(false).valueRadians()); |
|---|
| 104 | while(diff>math::twoPi) diff-=math::twoPi; |
|---|
| 105 | while(diff<-math::twoPi) diff+=math::twoPi; |
|---|
| 106 | this->getControllableEntity()->rotateRoll(-diff); |
|---|
| 107 | } |
|---|
| 108 | void CommonController::copyTargetOrientation() |
|---|
| 109 | { |
|---|
| 110 | if (bHasTargetOrientation_) |
|---|
| 111 | { |
|---|
| 112 | copyOrientation(targetOrientation_); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| [10731] | 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | void CommonController::moveToTargetPosition() |
|---|
| [10729] | 120 | { |
|---|
| [10731] | 121 | this->moveToPosition(this->targetPosition_); |
|---|
| [10729] | 122 | } |
|---|
| [10725] | 123 | void CommonController::moveToPosition(const Vector3& target) |
|---|
| 124 | { |
|---|
| 125 | if (!this->getControllableEntity()) |
|---|
| 126 | return; |
|---|
| [10729] | 127 | |
|---|
| 128 | //100 is (so far) the smallest tolerance (empirically found) that can be reached, |
|---|
| 129 | //with smaller distance spaceships can't reach position and go circles around it instead |
|---|
| 130 | int tolerance = 100; |
|---|
| [10725] | 131 | |
|---|
| [10729] | 132 | ControllableEntity* entity = this->getControllableEntity(); |
|---|
| 133 | Vector2 coord = get2DViewCoordinates |
|---|
| 134 | (entity->getPosition(), |
|---|
| 135 | entity->getOrientation() * WorldEntity::FRONT, |
|---|
| 136 | entity->getOrientation() * WorldEntity::UP, |
|---|
| 137 | target); |
|---|
| 138 | |
|---|
| [10725] | 139 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
|---|
| [10729] | 140 | |
|---|
| 141 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
|---|
| [10725] | 142 | float rotateX = clamp(coord.x * 10, -1.0f, 1.0f); |
|---|
| 143 | float rotateY = clamp(coord.y * 10, -1.0f, 1.0f); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| [10729] | 146 | if (distance > tolerance) |
|---|
| [10725] | 147 | { |
|---|
| [10729] | 148 | //Yaw and Pitch are enough to start facing the target |
|---|
| 149 | this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR * rotateX); |
|---|
| 150 | this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR * rotateY); |
|---|
| 151 | |
|---|
| 152 | //300 works, maybe less is better |
|---|
| 153 | if (distance < 300) |
|---|
| 154 | { |
|---|
| 155 | //Change roll when close. When Spaceship faces target, roll doesn't affect it's trajectory |
|---|
| 156 | //It's important that roll is not changed in the process of changing yaw and pitch |
|---|
| 157 | //Wingmen won't face same direction as Leaders, but when Leaders start moving |
|---|
| 158 | //Yaw and Pitch will adapt. |
|---|
| 159 | if (bHasTargetOrientation_) |
|---|
| 160 | { |
|---|
| 161 | copyTargetOrientation(); |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | this->getControllableEntity()->moveFrontBack(1.2f*SPEED); |
|---|
| [10725] | 165 | } |
|---|
| [10729] | 166 | else |
|---|
| 167 | { |
|---|
| 168 | bHasTargetPosition_ = false; |
|---|
| 169 | bHasTargetOrientation_ = false; |
|---|
| 170 | } |
|---|
| [10731] | 171 | } |
|---|
| 172 | void CommonController::doFire() |
|---|
| 173 | { |
|---|
| 174 | if (this->isLookingAtTarget(math::pi / 20.0f)) |
|---|
| 175 | this->getControllableEntity()->fire(0); //ai uses lens flare if they're close enough to the target |
|---|
| 176 | } |
|---|
| 177 | bool CommonController::isLookingAtTarget(float angle) const |
|---|
| 178 | { |
|---|
| 179 | if (!this->getControllableEntity()) |
|---|
| 180 | return false; |
|---|
| [10725] | 181 | |
|---|
| [10731] | 182 | return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle); |
|---|
| [10725] | 183 | } |
|---|
| [10731] | 184 | |
|---|
| 185 | void CommonController::aimAtTarget() |
|---|
| [10729] | 186 | { |
|---|
| [10731] | 187 | if (!this->target_ || !this->getControllableEntity()) |
|---|
| 188 | return; |
|---|
| 189 | |
|---|
| 190 | static const float hardcoded_projectile_speed = 750; |
|---|
| 191 | |
|---|
| 192 | Vector3 aimPosition = getPredictedPosition(this->getControllableEntity()->getWorldPosition(), |
|---|
| 193 | hardcoded_projectile_speed, this->target_->getWorldPosition(), this->target_->getVelocity()); |
|---|
| 194 | |
|---|
| 195 | Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); |
|---|
| 196 | if (pawn) |
|---|
| 197 | pawn->setAimPosition(aimPosition); |
|---|
| [10729] | 198 | } |
|---|
| [10731] | 199 | |
|---|
| [10719] | 200 | |
|---|
| 201 | |
|---|
| 202 | } |
|---|