[2954] | 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 | * Aurelian Jaggi |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[7673] | 29 | /** |
---|
| 30 | @file ForceField.cc |
---|
| 31 | @brief Implementation of the ForceField class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[2954] | 34 | #include "ForceField.h" |
---|
| 35 | |
---|
| 36 | #include "core/CoreIncludes.h" |
---|
[3196] | 37 | #include "core/XMLPort.h" |
---|
[5735] | 38 | #include "worldentities/MobileEntity.h" |
---|
[2954] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
[3064] | 42 | CreateFactory(ForceField); |
---|
[7673] | 43 | |
---|
| 44 | /*static*/ const std::string modeStringNormal_s = "tube"; |
---|
| 45 | /*static*/ const std::string modeStringSphere_s = "sphere"; |
---|
[2954] | 46 | |
---|
| 47 | ForceField::ForceField(BaseObject* creator) : StaticEntity(creator) |
---|
| 48 | { |
---|
[3064] | 49 | RegisterObject(ForceField); |
---|
[2954] | 50 | |
---|
[3064] | 51 | //Standard Values |
---|
| 52 | this->setDirection(Vector3::ZERO); |
---|
[7673] | 53 | this->velocity_ = 100; |
---|
| 54 | this->diameter_ = 500; |
---|
| 55 | this->length_ = 5000; |
---|
| 56 | this->mode_ = ForceFieldMode::tube; |
---|
[2954] | 57 | } |
---|
| 58 | |
---|
[7673] | 59 | ForceField::~ForceField() |
---|
| 60 | { |
---|
| 61 | } |
---|
[2954] | 62 | |
---|
| 63 | void ForceField::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 64 | { |
---|
[3064] | 65 | SUPER(ForceField, XMLPort, xmlelement, mode); |
---|
[5735] | 66 | |
---|
[3064] | 67 | //For correct xml import use: position, direction, velocity, scale |
---|
| 68 | XMLPortParam(ForceField, "velocity", setVelocity, getVelocity, xmlelement, mode).defaultValues(100); |
---|
| 69 | XMLPortParam(ForceField, "diameter", setDiameter, getDiameter, xmlelement, mode).defaultValues(500); |
---|
| 70 | XMLPortParam(ForceField, "length" , setLength , getLength , xmlelement, mode).defaultValues(2000); |
---|
[7673] | 71 | XMLPortParam(ForceField, "mode", setMode, getMode, xmlelement, mode); |
---|
[2954] | 72 | } |
---|
[3020] | 73 | |
---|
[2954] | 74 | void ForceField::tick(float dt) |
---|
| 75 | { |
---|
[7673] | 76 | // Iterate over all objects that could possibly be affected by the ForceField. |
---|
[3064] | 77 | for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it) |
---|
| 78 | { |
---|
[7673] | 79 | if(this->mode_ == ForceFieldMode::tube) |
---|
| 80 | { |
---|
| 81 | // The direction of the orientation of the force field. |
---|
| 82 | Vector3 direction = this->getOrientation() * WorldEntity::FRONT; |
---|
| 83 | direction.normalise(); |
---|
[3020] | 84 | |
---|
[7673] | 85 | // Vector from the center of the force field to the object its acting on. |
---|
| 86 | // TODO: This could probably be simplified. |
---|
| 87 | Vector3 distanceVector = it->getWorldPosition() - (this->getWorldPosition() + (this->halfLength_ * direction)); |
---|
| 88 | |
---|
| 89 | // The object is outside of the length of the ForceField. |
---|
| 90 | if(distanceVector.length() > this->halfLength_) |
---|
| 91 | return; |
---|
[3020] | 92 | |
---|
[7673] | 93 | // The distance of the object form the orientation vector. (Or rather the smallest distance from the orientation vector) |
---|
| 94 | float distanceFromDirectionVector = ((it->getWorldPosition() - this->getWorldPosition()).crossProduct(direction)).length(); |
---|
| 95 | |
---|
| 96 | // If the object in a tube of radius diameter/2 around the direction of orientation. |
---|
| 97 | if(distanceFromDirectionVector >= this->radius_) |
---|
| 98 | return; |
---|
[2954] | 99 | |
---|
[7673] | 100 | // Apply a force to the object in the direction of the orientation. |
---|
| 101 | // The force is highest when the object is directly on the direction vector, with a linear decrease, finally reaching zero, when distanceFromDirectionVector = radius. |
---|
| 102 | it->applyCentralForce(((this->radius_ - distanceFromDirectionVector)/(this->radius_)) * this->velocity_ * direction); |
---|
| 103 | } |
---|
| 104 | else if(this->mode_ == ForceFieldMode::sphere) |
---|
[3064] | 105 | { |
---|
[7673] | 106 | Vector3 distanceVector = it->getWorldPosition() - this->getWorldPosition(); |
---|
| 107 | float distance = distanceVector.length(); |
---|
| 108 | if (distance < this->radius_) |
---|
| 109 | { |
---|
| 110 | distanceVector.normalise(); |
---|
| 111 | it->applyCentralForce((this->radius_ - distance)/this->radius_ * this->velocity_ * distanceVector); |
---|
| 112 | } |
---|
[3064] | 113 | } |
---|
[2954] | 114 | } |
---|
[3064] | 115 | } |
---|
[7673] | 116 | |
---|
| 117 | void ForceField::setMode(const std::string& mode) |
---|
| 118 | { |
---|
| 119 | if(mode == ForceField::modeStringTube_s) |
---|
| 120 | this->mode_ = ForceFieldMode::tube; |
---|
| 121 | else if(mode == ForceField::modeStringSphere_s) |
---|
| 122 | this->mode_ = ForceFieldMode::sphere; |
---|
| 123 | else |
---|
| 124 | { |
---|
| 125 | COUT(2) << "Wrong mode '" << mode << "' in ForceField. Setting to 'tube'." << std::endl; |
---|
| 126 | this->mode_ = ForceFieldMode::tube; |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | inline const std::string& ForceField::getMode(void) |
---|
| 131 | { |
---|
| 132 | switch(this->mode_) |
---|
| 133 | { |
---|
| 134 | case ForceFieldMode::tube: |
---|
| 135 | return ForceField::modeStringTube_s; |
---|
| 136 | case ForceFieldMode::sphere: |
---|
| 137 | return ForceField::modeStringSphere_s; |
---|
| 138 | default: |
---|
| 139 | return ForceField::modeStringTube_s; |
---|
| 140 | } |
---|
| 141 | } |
---|
[2954] | 142 | } |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | |
---|