[8087] | 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 | * Maurus Kaufmann |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "SpaceBoundaries.h" |
---|
| 30 | |
---|
[8632] | 31 | #include <OgreBillboardSet.h> |
---|
| 32 | |
---|
| 33 | #include "core/CoreIncludes.h" |
---|
[8110] | 34 | #include "core/ObjectListIterator.h" |
---|
[8201] | 35 | #include "core/XMLPort.h" |
---|
[8632] | 36 | |
---|
[8710] | 37 | #include "util/Math.h" |
---|
| 38 | |
---|
[8632] | 39 | #include "graphics/Billboard.h" |
---|
| 40 | #include "infos/PlayerInfo.h" |
---|
| 41 | #include "worldentities/WorldEntity.h" |
---|
[8201] | 42 | #include "worldentities/pawns/Pawn.h" |
---|
[8087] | 43 | |
---|
[8710] | 44 | #ifndef PI2 |
---|
| 45 | #define PI2 1.570796327 |
---|
| 46 | #endif |
---|
| 47 | #ifndef PI |
---|
| 48 | #define PI 3.141592654 |
---|
| 49 | #endif |
---|
| 50 | |
---|
[8087] | 51 | namespace orxonox |
---|
| 52 | { |
---|
| 53 | CreateFactory(SpaceBoundaries); |
---|
| 54 | |
---|
| 55 | SpaceBoundaries::SpaceBoundaries(BaseObject* creator) : StaticEntity(creator) |
---|
| 56 | { |
---|
[8614] | 57 | RegisterObject(SpaceBoundaries); |
---|
| 58 | |
---|
[8201] | 59 | this->setMaxDistance(3000); |
---|
[8614] | 60 | this->setWarnDistance(this->getMaxDistance()); |
---|
| 61 | this->setShowDistance(this->getMaxDistance()); |
---|
[8404] | 62 | this->setReaction(0); |
---|
[8087] | 63 | } |
---|
| 64 | SpaceBoundaries::~SpaceBoundaries() |
---|
| 65 | { |
---|
[8470] | 66 | if (this->isInitialized()) |
---|
| 67 | { |
---|
| 68 | this->pawnsIn_.clear(); |
---|
[8281] | 69 | |
---|
[8660] | 70 | for( std::vector<BillboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++) |
---|
[8301] | 71 | { |
---|
[8470] | 72 | if( current->billy != NULL) |
---|
| 73 | { |
---|
| 74 | delete current->billy; |
---|
| 75 | } |
---|
[8301] | 76 | } |
---|
[8470] | 77 | this->billboards_.clear(); |
---|
[8301] | 78 | } |
---|
[8087] | 79 | } |
---|
[8110] | 80 | |
---|
[8281] | 81 | void SpaceBoundaries::checkWhoIsIn() |
---|
| 82 | { |
---|
| 83 | pawnsIn_.clear(); |
---|
| 84 | for(ObjectListIterator<Pawn> current = ObjectList<Pawn>::begin(); current != ObjectList<Pawn>::end(); ++current) |
---|
| 85 | { |
---|
| 86 | Pawn* currentPawn = *current; |
---|
[8614] | 87 | if( this->reaction_ == 0 ) |
---|
[8281] | 88 | { |
---|
[8614] | 89 | float distance = this->computeDistance(currentPawn); |
---|
| 90 | if(distance <= this->maxDistance_) |
---|
| 91 | { |
---|
| 92 | pawnsIn_.push_back(currentPawn); |
---|
| 93 | } |
---|
| 94 | } else if (this->reaction_ == 2) { |
---|
| 95 | float distance = this->computeDistance(currentPawn); |
---|
| 96 | if(distance >= this->maxDistance_) |
---|
| 97 | { |
---|
| 98 | pawnsIn_.push_back(currentPawn); |
---|
| 99 | } |
---|
| 100 | } else { |
---|
[8281] | 101 | pawnsIn_.push_back(currentPawn); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
[8660] | 106 | void SpaceBoundaries::positionBillboard(const Vector3& position, float alpha) |
---|
[8301] | 107 | { |
---|
[8660] | 108 | size_t current; |
---|
| 109 | for (current = 0; current < this->billboards_.size(); ++current) |
---|
| 110 | if (!this->billboards_[current].usedYet) |
---|
[8301] | 111 | break; |
---|
[8660] | 112 | |
---|
| 113 | if (current == this->billboards_.size()) |
---|
[8301] | 114 | { |
---|
[8660] | 115 | Billboard* billboard = new Billboard(this); |
---|
| 116 | billboard->setPosition(position); |
---|
[8694] | 117 | billboard->setSyncMode(ObjectDirection::None); |
---|
[8660] | 118 | this->setBillboardOptions(billboard); |
---|
| 119 | BillboardAdministration ba = {true, billboard}; |
---|
| 120 | this->billboards_.push_back(ba); |
---|
[8301] | 121 | } |
---|
[8660] | 122 | |
---|
| 123 | this->billboards_[current].billy->setPosition(position); |
---|
| 124 | this->billboards_[current].billy->setVisible(true); |
---|
| 125 | this->billboards_[current].billy->setColour(ColourValue(1, 1, 1, alpha)); |
---|
| 126 | this->billboards_[current].usedYet = true; |
---|
| 127 | |
---|
[8710] | 128 | // in order to animate the billboard, we use a |
---|
| 129 | // transform to spherical coordinates according to |
---|
| 130 | // http://de.wikipedia.org/wiki/Kugelkoordinaten convention |
---|
[8660] | 131 | |
---|
[8710] | 132 | Vector3 dirVect = position - this->getPosition(); |
---|
| 133 | |
---|
| 134 | float phi; // this is the longitude on the sphere (-pi, pi) |
---|
| 135 | if (dirVect.x > 0) phi = atan(dirVect.y/dirVect.x); |
---|
| 136 | else if (dirVect.x == 0) phi = sgn(dirVect.y) * PI2; |
---|
| 137 | else if (dirVect.y >= 0) phi = atan(dirVect.y/dirVect.x) + PI; |
---|
| 138 | else if (dirVect.y < 0) phi = atan(dirVect.y/dirVect.x) - PI; |
---|
| 139 | |
---|
| 140 | float theta; // this is the latitude measured from z axis (0, pi) |
---|
| 141 | theta = acos(dirVect.z / dirVect.length()); |
---|
| 142 | |
---|
| 143 | Vector3 e_r = dirVect.normalisedCopy(); |
---|
| 144 | //Vector3 e_phi = Vector3(-sin(phi), cos(phi), 0); |
---|
| 145 | Vector3 e_theta = Vector3(cos(theta)*cos(phi), cos(theta)*sin(phi), -sin(theta)); |
---|
| 146 | |
---|
| 147 | // set billboard orientation |
---|
| 148 | this->billboards_[current].billy->setCommonDirection(e_r); |
---|
| 149 | this->billboards_[current].billy->setCommonUpVector(e_theta); |
---|
| 150 | |
---|
| 151 | // set billboard texture coordinates |
---|
| 152 | float u = (phi - floor(phi)) * this->maxDistance_ / 200; |
---|
| 153 | float v = (theta -floor(theta)) * this->maxDistance_ / 200; |
---|
| 154 | Ogre::FloatRect textureCoords = Ogre::FloatRect(0.5-u, 0.5-v, 1.0-u, 1.0-v); |
---|
| 155 | this->billboards_[current].billy->setTextureCoords(&textureCoords, 1); |
---|
| 156 | |
---|
| 157 | // debug output |
---|
| 158 | //COUT(0) << "dirVect: " << dirVect << " len: " << dirVect.length() << std::endl; |
---|
| 159 | //COUT(0) << "phi: " << phi << std::endl; |
---|
| 160 | //COUT(0) << "theta: " << theta << std::endl; |
---|
| 161 | //COUT(0) << "e_r: " << e_r << " len: " << e_r.length() << std::endl; |
---|
| 162 | //COUT(0) << "e_phi: " << e_phi << " len: " << e_phi.length() << std::endl; |
---|
| 163 | //COUT(0) << "e_theta: " << e_theta << " len: " << e_theta.length() << std::endl; |
---|
[8301] | 164 | } |
---|
| 165 | |
---|
| 166 | void SpaceBoundaries::setBillboardOptions(Billboard *billy) |
---|
| 167 | { |
---|
[8710] | 168 | if(billy) |
---|
[8301] | 169 | { |
---|
[8710] | 170 | billy->setMaterial("Grid02"); |
---|
[8614] | 171 | billy->setBillboardType(Ogre::BBT_PERPENDICULAR_COMMON); |
---|
[8710] | 172 | billy->setDefaultDimensions(100, 100); |
---|
[8301] | 173 | billy->setVisible(true); |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | void SpaceBoundaries::removeAllBillboards() |
---|
| 178 | { |
---|
[8660] | 179 | for( std::vector<BillboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++ ) |
---|
[8301] | 180 | { |
---|
| 181 | current->usedYet = false; |
---|
| 182 | current->billy->setVisible(false); |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
[8110] | 186 | void SpaceBoundaries::setMaxDistance(float r) |
---|
| 187 | { |
---|
[8166] | 188 | this->maxDistance_ = r; |
---|
[8110] | 189 | } |
---|
| 190 | float SpaceBoundaries::getMaxDistance() |
---|
| 191 | { |
---|
[8166] | 192 | return this->maxDistance_; |
---|
[8110] | 193 | } |
---|
| 194 | |
---|
| 195 | void SpaceBoundaries::setWarnDistance(float r) |
---|
| 196 | { |
---|
[8166] | 197 | this->warnDistance_ = r; |
---|
[8110] | 198 | } |
---|
| 199 | float SpaceBoundaries::getWarnDistance() |
---|
| 200 | { |
---|
[8166] | 201 | return this->warnDistance_; |
---|
[8110] | 202 | } |
---|
[8201] | 203 | |
---|
[8244] | 204 | void SpaceBoundaries::setShowDistance(float r) |
---|
| 205 | { |
---|
| 206 | this->showDistance_ = r; |
---|
| 207 | } |
---|
| 208 | float SpaceBoundaries::getShowDistance() |
---|
| 209 | { |
---|
| 210 | return this->showDistance_; |
---|
| 211 | } |
---|
| 212 | |
---|
[8201] | 213 | void SpaceBoundaries::setHealthDecrease(float amount) |
---|
| 214 | { |
---|
| 215 | this->healthDecrease_ = amount/1000; |
---|
| 216 | } |
---|
| 217 | float SpaceBoundaries::getHealthDecrease() |
---|
| 218 | { |
---|
| 219 | return this->healthDecrease_; |
---|
| 220 | } |
---|
[8404] | 221 | |
---|
| 222 | void SpaceBoundaries::setReaction(int mode) |
---|
| 223 | { |
---|
| 224 | this->reaction_ = mode; |
---|
| 225 | } |
---|
| 226 | int SpaceBoundaries::getReaction() |
---|
| 227 | { |
---|
| 228 | return this->reaction_; |
---|
| 229 | } |
---|
[8087] | 230 | |
---|
| 231 | void SpaceBoundaries::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 232 | { |
---|
[8110] | 233 | SUPER(SpaceBoundaries, XMLPort, xmlelement, mode); |
---|
[8087] | 234 | |
---|
[8110] | 235 | XMLPortParam(SpaceBoundaries, "maxDistance", setMaxDistance, getMaxDistance, xmlelement, mode); |
---|
| 236 | XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode); |
---|
[8614] | 237 | XMLPortParam(SpaceBoundaries, "showDistance", setShowDistance, getShowDistance, xmlelement, mode); |
---|
[8201] | 238 | XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode); |
---|
[8404] | 239 | XMLPortParam(SpaceBoundaries, "reactionMode", setReaction, getReaction, xmlelement, mode); |
---|
[8087] | 240 | } |
---|
[8110] | 241 | |
---|
| 242 | void SpaceBoundaries::tick(float dt) |
---|
| 243 | { |
---|
[8404] | 244 | this->checkWhoIsIn(); |
---|
[8301] | 245 | this->removeAllBillboards(); |
---|
[8281] | 246 | |
---|
[8301] | 247 | float distance; |
---|
| 248 | bool humanItem; |
---|
[8404] | 249 | for( std::list<WeakPtr<Pawn> >::iterator current = pawnsIn_.begin(); current != pawnsIn_.end(); current++ ) |
---|
[8110] | 250 | { |
---|
[8404] | 251 | Pawn* currentPawn = current->get(); |
---|
| 252 | if( currentPawn && currentPawn->getNode() ) |
---|
[8110] | 253 | { |
---|
[8404] | 254 | distance = this->computeDistance(currentPawn); |
---|
| 255 | humanItem = this->isHumanPlayer(currentPawn); |
---|
[8614] | 256 | COUT(5) << "Distance:" << distance << std::endl; // message for debugging |
---|
| 257 | if(distance > this->warnDistance_ && distance < this->maxDistance_) // Display warning |
---|
[8164] | 258 | { |
---|
[8404] | 259 | if(humanItem) |
---|
| 260 | { |
---|
[8614] | 261 | this->displayWarning("Attention! You are close to the boundary!"); |
---|
[8404] | 262 | } |
---|
[8201] | 263 | } |
---|
[8614] | 264 | if(/* humanItem &&*/ abs(this->maxDistance_ - distance) < this->showDistance_ ) |
---|
[8164] | 265 | { |
---|
[8660] | 266 | this->displayBoundaries(currentPawn, 1.0f - fabs(this->maxDistance_ - distance) / this->showDistance_); // Show the boundary |
---|
[8164] | 267 | } |
---|
[8404] | 268 | if(distance > this->maxDistance_ && (this->reaction_ == 1) ) |
---|
| 269 | { |
---|
| 270 | if( humanItem ) |
---|
| 271 | { |
---|
[8614] | 272 | COUT(5) << "Health should be decreasing!" << std::endl; |
---|
[8404] | 273 | this->displayWarning("You are out of the area now!"); |
---|
| 274 | } |
---|
| 275 | currentPawn->removeHealth( (distance - this->maxDistance_) * this->healthDecrease_); |
---|
| 276 | } |
---|
[8614] | 277 | if( (this->reaction_ == 0) && (distance + 100 > this->maxDistance_)) // Exception: A Pawn can't move more than 100 units per tick. |
---|
[8404] | 278 | { |
---|
| 279 | this->conditionalBounceBack(currentPawn, distance, dt); |
---|
| 280 | } |
---|
[8614] | 281 | if( this->reaction_ == 2 && (distance - 100 < this->maxDistance_) ) |
---|
| 282 | { |
---|
| 283 | this->conditionalBounceBack(currentPawn, distance, dt); |
---|
| 284 | } |
---|
[8110] | 285 | } |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | float SpaceBoundaries::computeDistance(WorldEntity *item) |
---|
| 290 | { |
---|
[8301] | 291 | if(item != NULL) |
---|
| 292 | { |
---|
| 293 | Vector3 itemPosition = item->getPosition(); |
---|
| 294 | return (itemPosition.distance(this->getPosition())); |
---|
| 295 | } else { |
---|
| 296 | return -1; |
---|
| 297 | } |
---|
[8110] | 298 | } |
---|
| 299 | |
---|
[8164] | 300 | void SpaceBoundaries::displayWarning(const std::string warnText) |
---|
[8301] | 301 | { |
---|
[8614] | 302 | // TODO |
---|
[8164] | 303 | } |
---|
[8110] | 304 | |
---|
[8660] | 305 | void SpaceBoundaries::displayBoundaries(Pawn *item, float alpha) |
---|
[8244] | 306 | { |
---|
| 307 | |
---|
| 308 | Vector3 direction = item->getPosition() - this->getPosition(); |
---|
| 309 | direction.normalise(); |
---|
| 310 | |
---|
| 311 | Vector3 boundaryPosition = this->getPosition() + direction * this->maxDistance_; |
---|
| 312 | |
---|
[8660] | 313 | this->positionBillboard(boundaryPosition, alpha); |
---|
[8244] | 314 | } |
---|
| 315 | |
---|
[8404] | 316 | void SpaceBoundaries::conditionalBounceBack(Pawn *item, float currentDistance, float dt) |
---|
[8244] | 317 | { |
---|
| 318 | Vector3 normal = item->getPosition() - this->getPosition(); |
---|
[8404] | 319 | normal.normalise(); |
---|
| 320 | Vector3 velocity = item->getVelocity(); |
---|
| 321 | float normalSpeed = item->getVelocity().dotProduct(normal); |
---|
| 322 | |
---|
[8614] | 323 | /* Check, whether the Pawn would leave the boundary in the next tick, if so send it back. */ |
---|
| 324 | if( this->reaction_ == 0 && currentDistance + normalSpeed * dt > this->maxDistance_ - 10 ) // -10: "security measure" |
---|
[8244] | 325 | { |
---|
[8614] | 326 | bounceBack(item, &normal, &velocity); |
---|
| 327 | } else if (this->reaction_ == 2 && currentDistance - normalSpeed * dt < this->maxDistance_ + 10 ) // 10: "security measure" |
---|
| 328 | { |
---|
| 329 | normal = normal * (-1); |
---|
| 330 | bounceBack(item, &normal, &velocity); |
---|
[8244] | 331 | } |
---|
| 332 | } |
---|
| 333 | |
---|
[8614] | 334 | void SpaceBoundaries::bounceBack(Pawn *item, Vector3 *normal, Vector3 *velocity) |
---|
| 335 | { |
---|
| 336 | float dampingFactor = 0.5; |
---|
| 337 | *velocity = velocity->reflect(*normal); |
---|
| 338 | Vector3 acceleration = item->getAcceleration(); |
---|
| 339 | acceleration = acceleration.reflect(*normal); |
---|
| 340 | |
---|
| 341 | item->lookAt( *velocity + this->getPosition() ); |
---|
| 342 | |
---|
| 343 | item->setAcceleration(acceleration * dampingFactor); |
---|
| 344 | item->setVelocity(*velocity * dampingFactor); |
---|
| 345 | |
---|
| 346 | item->setPosition( item->getPosition() - *normal * 10 ); // Set the position of the Pawn to be well inside the boundary. |
---|
| 347 | } |
---|
| 348 | |
---|
[8164] | 349 | bool SpaceBoundaries::isHumanPlayer(Pawn *item) |
---|
| 350 | { |
---|
| 351 | if(item != NULL) |
---|
| 352 | { |
---|
[8201] | 353 | if(item->getPlayer()) |
---|
| 354 | { |
---|
| 355 | return item->getPlayer()->isHumanPlayer(); |
---|
| 356 | } |
---|
[8164] | 357 | } |
---|
[8201] | 358 | return false; |
---|
[8164] | 359 | } |
---|
[8110] | 360 | |
---|
[8087] | 361 | } |
---|