[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 | /** |
---|
[7676] | 30 | @file ForceField.cc |
---|
| 31 | @brief Implementation of the ForceField class. |
---|
[7673] | 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); |
---|
[7676] | 43 | |
---|
[7674] | 44 | /*static*/ const std::string ForceField::modeTube_s = "tube"; |
---|
| 45 | /*static*/ const std::string ForceField::modeSphere_s = "sphere"; |
---|
[2954] | 46 | |
---|
[7676] | 47 | /** |
---|
| 48 | @brief |
---|
| 49 | Constructor. Registers the object and initializes some values. |
---|
| 50 | */ |
---|
[2954] | 51 | ForceField::ForceField(BaseObject* creator) : StaticEntity(creator) |
---|
| 52 | { |
---|
[3064] | 53 | RegisterObject(ForceField); |
---|
[2954] | 54 | |
---|
[3064] | 55 | //Standard Values |
---|
| 56 | this->setDirection(Vector3::ZERO); |
---|
[7676] | 57 | this->setVelocity(100); |
---|
| 58 | this->setDiameter(500); |
---|
| 59 | this->setLength(2000); |
---|
[7674] | 60 | this->mode_ = forceFieldMode::tube; |
---|
[2954] | 61 | } |
---|
| 62 | |
---|
[7676] | 63 | /** |
---|
| 64 | @brief |
---|
| 65 | Destructor. |
---|
| 66 | */ |
---|
[7673] | 67 | ForceField::~ForceField() |
---|
| 68 | { |
---|
| 69 | } |
---|
[2954] | 70 | |
---|
[7676] | 71 | /** |
---|
| 72 | @brief |
---|
| 73 | Creates a ForceField object through XML. |
---|
| 74 | */ |
---|
[2954] | 75 | void ForceField::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 76 | { |
---|
[3064] | 77 | SUPER(ForceField, XMLPort, xmlelement, mode); |
---|
[5735] | 78 | |
---|
[3064] | 79 | XMLPortParam(ForceField, "velocity", setVelocity, getVelocity, xmlelement, mode).defaultValues(100); |
---|
| 80 | XMLPortParam(ForceField, "diameter", setDiameter, getDiameter, xmlelement, mode).defaultValues(500); |
---|
| 81 | XMLPortParam(ForceField, "length" , setLength , getLength , xmlelement, mode).defaultValues(2000); |
---|
[7673] | 82 | XMLPortParam(ForceField, "mode", setMode, getMode, xmlelement, mode); |
---|
[2954] | 83 | } |
---|
[3020] | 84 | |
---|
[7676] | 85 | /** |
---|
| 86 | @brief |
---|
| 87 | A method that is called every tick. |
---|
| 88 | Implements the behavior of teh ForceField. |
---|
| 89 | @param dt |
---|
| 90 | The amount of time that elapsed since the last tick. |
---|
| 91 | */ |
---|
[2954] | 92 | void ForceField::tick(float dt) |
---|
| 93 | { |
---|
[7674] | 94 | if(this->mode_ == forceFieldMode::tube) |
---|
[3064] | 95 | { |
---|
[7674] | 96 | // Iterate over all objects that could possibly be affected by the ForceField. |
---|
| 97 | for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it) |
---|
[7673] | 98 | { |
---|
| 99 | // The direction of the orientation of the force field. |
---|
| 100 | Vector3 direction = this->getOrientation() * WorldEntity::FRONT; |
---|
| 101 | direction.normalise(); |
---|
[3020] | 102 | |
---|
[7673] | 103 | // Vector from the center of the force field to the object its acting on. |
---|
| 104 | // TODO: This could probably be simplified. |
---|
| 105 | Vector3 distanceVector = it->getWorldPosition() - (this->getWorldPosition() + (this->halfLength_ * direction)); |
---|
[7676] | 106 | |
---|
[7673] | 107 | // The object is outside of the length of the ForceField. |
---|
| 108 | if(distanceVector.length() > this->halfLength_) |
---|
[7674] | 109 | continue; |
---|
[3020] | 110 | |
---|
[7673] | 111 | // The distance of the object form the orientation vector. (Or rather the smallest distance from the orientation vector) |
---|
| 112 | float distanceFromDirectionVector = ((it->getWorldPosition() - this->getWorldPosition()).crossProduct(direction)).length(); |
---|
[7676] | 113 | |
---|
| 114 | // If the object in a tube of radius 'radius' around the direction of orientation. |
---|
[7673] | 115 | if(distanceFromDirectionVector >= this->radius_) |
---|
[7674] | 116 | continue; |
---|
[2954] | 117 | |
---|
[7673] | 118 | // Apply a force to the object in the direction of the orientation. |
---|
| 119 | // The force is highest when the object is directly on the direction vector, with a linear decrease, finally reaching zero, when distanceFromDirectionVector = radius. |
---|
[7674] | 120 | it->applyCentralForce((this->radius_ - distanceFromDirectionVector)/this->radius_ * this->velocity_ * direction); |
---|
[7673] | 121 | } |
---|
[7674] | 122 | } |
---|
| 123 | else if(this->mode_ == forceFieldMode::sphere) |
---|
| 124 | { |
---|
| 125 | // Iterate over all objects that could possibly be affected by the ForceField. |
---|
| 126 | for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it) |
---|
[3064] | 127 | { |
---|
[7673] | 128 | Vector3 distanceVector = it->getWorldPosition() - this->getWorldPosition(); |
---|
| 129 | float distance = distanceVector.length(); |
---|
[7676] | 130 | // If the object is within 'radius' distance. |
---|
[7673] | 131 | if (distance < this->radius_) |
---|
| 132 | { |
---|
| 133 | distanceVector.normalise(); |
---|
[7676] | 134 | // Apply a force proportional to the velocity, with highest force at the origin of the sphere, linear decreasing until reaching a distance of 'radius' from the origin, where the force reaches zero. |
---|
[7673] | 135 | it->applyCentralForce((this->radius_ - distance)/this->radius_ * this->velocity_ * distanceVector); |
---|
| 136 | } |
---|
[3064] | 137 | } |
---|
[2954] | 138 | } |
---|
[3064] | 139 | } |
---|
[7676] | 140 | |
---|
| 141 | /** |
---|
| 142 | @brief |
---|
| 143 | Set the mode of the ForceField. |
---|
| 144 | @param mode |
---|
| 145 | The mode as a string. |
---|
| 146 | */ |
---|
[7673] | 147 | void ForceField::setMode(const std::string& mode) |
---|
| 148 | { |
---|
[7674] | 149 | if(mode == ForceField::modeTube_s) |
---|
| 150 | this->mode_ = forceFieldMode::tube; |
---|
| 151 | else if(mode == ForceField::modeSphere_s) |
---|
| 152 | this->mode_ = forceFieldMode::sphere; |
---|
[7673] | 153 | else |
---|
| 154 | { |
---|
| 155 | COUT(2) << "Wrong mode '" << mode << "' in ForceField. Setting to 'tube'." << std::endl; |
---|
[7674] | 156 | this->mode_ = forceFieldMode::tube; |
---|
[7673] | 157 | } |
---|
| 158 | } |
---|
[7676] | 159 | |
---|
| 160 | /** |
---|
| 161 | @brief |
---|
| 162 | Get the mode of the ForceField. |
---|
| 163 | @return |
---|
| 164 | Returns the mode of the ForceField as a string. |
---|
| 165 | */ |
---|
| 166 | const std::string& ForceField::getMode(void) |
---|
[7673] | 167 | { |
---|
| 168 | switch(this->mode_) |
---|
| 169 | { |
---|
[7674] | 170 | case forceFieldMode::tube: |
---|
| 171 | return ForceField::modeTube_s; |
---|
| 172 | case forceFieldMode::sphere: |
---|
| 173 | return ForceField::modeSphere_s; |
---|
[7673] | 174 | default: |
---|
[7674] | 175 | return ForceField::modeTube_s; |
---|
[7673] | 176 | } |
---|
| 177 | } |
---|
[7676] | 178 | |
---|
[2954] | 179 | } |
---|