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 | * Gani Aliguzhinov |
---|
24 | |
---|
25 | * Co-authors: |
---|
26 | * ... |
---|
27 | * |
---|
28 | */ |
---|
29 | #include "controllers/FlyingController.h" |
---|
30 | #include "core/XMLPort.h" |
---|
31 | #include "worldentities/pawns/SpaceShip.h" |
---|
32 | #include "util/Math.h" |
---|
33 | #include <OgreMatrix3.h> |
---|
34 | |
---|
35 | namespace orxonox |
---|
36 | { |
---|
37 | RegisterClass (FlyingController); |
---|
38 | |
---|
39 | FlyingController::FlyingController(Context* context): CommonController(context) |
---|
40 | { |
---|
41 | RegisterObject(FlyingController); |
---|
42 | this->rotationProgress_ = 0; |
---|
43 | this->spread_ = 200; |
---|
44 | this->tolerance_ = 80; |
---|
45 | } |
---|
46 | FlyingController::~FlyingController() |
---|
47 | { |
---|
48 | } |
---|
49 | |
---|
50 | void FlyingController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
51 | { |
---|
52 | XMLPortParam(FlyingController, "spread", setSpread, getSpread, xmlelement, mode); |
---|
53 | XMLPortParam(FlyingController, "formationMode", setFormationModeXML, getFormationModeXML, xmlelement, mode); |
---|
54 | SUPER(FlyingController, XMLPort, xmlelement, mode); |
---|
55 | } |
---|
56 | |
---|
57 | void FlyingController::setFormationModeXML(std::string val) |
---|
58 | { |
---|
59 | const std::string valUpper = getUppercase(val); |
---|
60 | FormationMode::Value value; |
---|
61 | |
---|
62 | if ( valUpper == "WALL" ) |
---|
63 | value = FormationMode::WALL; |
---|
64 | else if ( valUpper == "FINGER4" ) |
---|
65 | value = FormationMode::FINGER4; |
---|
66 | else if ( valUpper == "DIAMOND" ) |
---|
67 | value = FormationMode::DIAMOND; |
---|
68 | else |
---|
69 | ThrowException(ParseError, std::string( "Attempting to set an unknown FormationMode: '" )+ val + "'."); |
---|
70 | this->setFormationMode(value); |
---|
71 | } |
---|
72 | std::string FlyingController::getFormationModeXML() const |
---|
73 | { |
---|
74 | switch ( this->formationMode_ ) |
---|
75 | { |
---|
76 | case FormationMode::WALL: |
---|
77 | { return "WALL"; } |
---|
78 | case FormationMode::FINGER4: |
---|
79 | { return "FINGER4"; } |
---|
80 | case FormationMode::DIAMOND: |
---|
81 | { return "DIAMOND"; } |
---|
82 | default: |
---|
83 | return "DIAMOND"; |
---|
84 | } |
---|
85 | } |
---|
86 | void FlyingController::stopMoving() |
---|
87 | { |
---|
88 | this->bHasTargetPosition_ = false; |
---|
89 | } |
---|
90 | void FlyingController::moveToPosition(const Vector3& targetPosition, float dt) |
---|
91 | { |
---|
92 | if (!this->getControllableEntity()) |
---|
93 | return; |
---|
94 | ControllableEntity* entity = this->getControllableEntity(); |
---|
95 | |
---|
96 | float distance = ( targetPosition - entity->getPosition() ).length(); |
---|
97 | |
---|
98 | if ( distance >= this->tolerance_ ) |
---|
99 | { |
---|
100 | Vector2 coord = get2DViewCoordinates |
---|
101 | ( entity->getPosition() , |
---|
102 | entity->getOrientation() * WorldEntity::FRONT, |
---|
103 | entity->getOrientation() * WorldEntity::UP, |
---|
104 | targetPosition ); |
---|
105 | float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f ); |
---|
106 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
---|
107 | entity->rotateYaw( ROTATEFACTOR * rotateX * dt ); |
---|
108 | entity->rotatePitch( ROTATEFACTOR * rotateY * dt ); |
---|
109 | |
---|
110 | if (distance > this->tolerance_*1.5f || (rotateX > -0.03 && rotateX < 0.03 && rotateY > -0.03 && rotateY < 0.03)) |
---|
111 | entity->moveFrontBack( SPEED * dt ); |
---|
112 | copyTargetOrientation(dt); |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | bHasTargetPosition_ = false; |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | void FlyingController::moveToTargetPosition(float dt) |
---|
121 | { |
---|
122 | this->moveToPosition (this->targetPosition_, dt); |
---|
123 | } |
---|
124 | void FlyingController::copyOrientation(const Quaternion& orient, float dt) |
---|
125 | { |
---|
126 | //copied from |
---|
127 | //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_ |
---|
128 | //how can I make my objects rotate smoothly? |
---|
129 | if (!this->getControllableEntity()) |
---|
130 | return; |
---|
131 | Quaternion myOrient = this->getControllableEntity()->getOrientation(); |
---|
132 | this->rotationProgress_ += dt; |
---|
133 | |
---|
134 | if (this->rotationProgress_ > 1) |
---|
135 | { |
---|
136 | this->rotationProgress_ = 0; |
---|
137 | this->bHasTargetOrientation_ = false; |
---|
138 | } |
---|
139 | else |
---|
140 | { |
---|
141 | Quaternion deltaOrientation = Quaternion::Slerp(rotationProgress_, myOrient, orient, true); |
---|
142 | |
---|
143 | Matrix3 deltaMatrix, myMatrix; |
---|
144 | |
---|
145 | deltaOrientation.ToRotationMatrix(deltaMatrix); |
---|
146 | myOrient.ToRotationMatrix (myMatrix); |
---|
147 | |
---|
148 | Radian yawDelta, pitchDelta, rollDelta, yawMy, pitchMy, rollMy; |
---|
149 | deltaMatrix.ToEulerAnglesYXZ(yawDelta, pitchDelta, rollDelta); |
---|
150 | myMatrix.ToEulerAnglesYXZ (yawMy, pitchMy, rollMy); |
---|
151 | |
---|
152 | if (!this->getControllableEntity()) |
---|
153 | return; |
---|
154 | this->getControllableEntity()->rotateRoll ((rollDelta.valueRadians() - rollMy.valueRadians())*ROTATEFACTOR*dt); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | void FlyingController::copyTargetOrientation(float dt) |
---|
159 | { |
---|
160 | if (bHasTargetOrientation_) |
---|
161 | { |
---|
162 | this->copyOrientation(targetOrientation_, dt); |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | void FlyingController::setTargetPosition(const Vector3& target) |
---|
167 | { |
---|
168 | this->targetPosition_ = target; |
---|
169 | this->bHasTargetPosition_ = true; |
---|
170 | } |
---|
171 | |
---|
172 | void FlyingController::setTargetOrientation(const Quaternion& orient) |
---|
173 | { |
---|
174 | this->targetOrientation_=orient; |
---|
175 | this->bHasTargetOrientation_=true; |
---|
176 | } |
---|
177 | |
---|
178 | void FlyingController::setTargetOrientation(ControllableEntity* target) |
---|
179 | { |
---|
180 | if (target) |
---|
181 | this->setTargetOrientation(target->getOrientation()); |
---|
182 | } |
---|
183 | void FlyingController::boostControl() |
---|
184 | { |
---|
185 | if (!this->getControllableEntity()) |
---|
186 | return; |
---|
187 | SpaceShip* ship = orxonox_cast<SpaceShip*>(this->getControllableEntity()); |
---|
188 | if(ship == NULL) return; |
---|
189 | if(ship->getBoostPower()*1.5f > ship->getInitialBoostPower() ) //upper limit ->boost |
---|
190 | { |
---|
191 | this->getControllableEntity()->boost(true); |
---|
192 | } |
---|
193 | else if(ship->getBoostPower()*4.0f < ship->getInitialBoostPower()) //lower limit ->do not boost |
---|
194 | { |
---|
195 | this->getControllableEntity()->boost(false); |
---|
196 | } |
---|
197 | } |
---|
198 | void FlyingController::keepFormation(const ControllableEntity* leaderEntity, Vector3& targetRelativePosition) |
---|
199 | { |
---|
200 | ControllableEntity* myEntity = this->getControllableEntity(); |
---|
201 | Vector3 myPosition = myEntity->getWorldPosition(); |
---|
202 | |
---|
203 | if (!leaderEntity) |
---|
204 | { |
---|
205 | return; |
---|
206 | } |
---|
207 | Quaternion orient = leaderEntity->getWorldOrientation(); |
---|
208 | Vector3 leaderPosition = leaderEntity->getWorldPosition(); |
---|
209 | |
---|
210 | if (!leaderEntity) |
---|
211 | { |
---|
212 | return; |
---|
213 | } |
---|
214 | Vector3 targetAbsolutePosition = |
---|
215 | (leaderPosition + (orient*WorldEntity::FRONT) * (leaderEntity->getVelocity().length()/5) |
---|
216 | + (orient* (targetRelativePosition))); |
---|
217 | //let ship finish rotating. also don't call copyOrientation to often as it is a slow function. |
---|
218 | if (static_cast<int>(rnd(1.0f) * 100) % 3 == 0) |
---|
219 | this->setTargetOrientation (orient); |
---|
220 | this->setTargetPosition (targetAbsolutePosition); |
---|
221 | if ((targetAbsolutePosition - myPosition).length() > this->tolerance_ * 1.5f) |
---|
222 | { |
---|
223 | this->boostControl(); |
---|
224 | } |
---|
225 | else |
---|
226 | { |
---|
227 | this->getControllableEntity()->boost(false); |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|