[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: |
---|
[8397] | 25 | * Kevin Young |
---|
[2954] | 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"; |
---|
[7677] | 46 | /*static*/ const std::string ForceField::modeInvertedSphere_s = "invertedSphere"; |
---|
[8397] | 47 | /*static*/ const std::string ForceField::modeNewtonianGravity_s = "newtonianGravity"; |
---|
| 48 | /*static*/ const float ForceField::gravConstant_ = 6.673e-11; |
---|
| 49 | /*static*/ const float ForceField::attenFactor_ = 1; |
---|
[2954] | 50 | |
---|
[7676] | 51 | /** |
---|
| 52 | @brief |
---|
| 53 | Constructor. Registers the object and initializes some values. |
---|
| 54 | */ |
---|
[2954] | 55 | ForceField::ForceField(BaseObject* creator) : StaticEntity(creator) |
---|
| 56 | { |
---|
[3064] | 57 | RegisterObject(ForceField); |
---|
[2954] | 58 | |
---|
[3064] | 59 | //Standard Values |
---|
| 60 | this->setDirection(Vector3::ZERO); |
---|
[7676] | 61 | this->setVelocity(100); |
---|
| 62 | this->setDiameter(500); |
---|
[8397] | 63 | this->setMassDiameter(0); //! We allow point-masses |
---|
[7676] | 64 | this->setLength(2000); |
---|
[7674] | 65 | this->mode_ = forceFieldMode::tube; |
---|
[7801] | 66 | |
---|
| 67 | this->registerVariables(); |
---|
[2954] | 68 | } |
---|
| 69 | |
---|
[7676] | 70 | /** |
---|
| 71 | @brief |
---|
| 72 | Destructor. |
---|
| 73 | */ |
---|
[7673] | 74 | ForceField::~ForceField() |
---|
| 75 | { |
---|
| 76 | } |
---|
[2954] | 77 | |
---|
[7676] | 78 | /** |
---|
| 79 | @brief |
---|
| 80 | Creates a ForceField object through XML. |
---|
| 81 | */ |
---|
[2954] | 82 | void ForceField::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 83 | { |
---|
[3064] | 84 | SUPER(ForceField, XMLPort, xmlelement, mode); |
---|
[5735] | 85 | |
---|
[3064] | 86 | XMLPortParam(ForceField, "velocity", setVelocity, getVelocity, xmlelement, mode).defaultValues(100); |
---|
| 87 | XMLPortParam(ForceField, "diameter", setDiameter, getDiameter, xmlelement, mode).defaultValues(500); |
---|
[8397] | 88 | XMLPortParam(ForceField, "massDiameter", setMassDiameter, getMassDiameter, xmlelement, mode).defaultValues(0); |
---|
[7677] | 89 | XMLPortParam(ForceField, "length", setLength , getLength , xmlelement, mode).defaultValues(2000); |
---|
[7673] | 90 | XMLPortParam(ForceField, "mode", setMode, getMode, xmlelement, mode); |
---|
[2954] | 91 | } |
---|
[7801] | 92 | |
---|
| 93 | void ForceField::registerVariables() |
---|
| 94 | { |
---|
| 95 | registerVariable(this->velocity_, VariableDirection::ToClient); |
---|
| 96 | registerVariable(this->radius_, VariableDirection::ToClient); |
---|
[8397] | 97 | registerVariable(this->massRadius_, VariableDirection::ToClient); |
---|
[7801] | 98 | registerVariable(this->halfLength_, VariableDirection::ToClient); |
---|
| 99 | registerVariable(this->mode_, VariableDirection::ToClient); |
---|
| 100 | } |
---|
[3020] | 101 | |
---|
[7801] | 102 | |
---|
[7676] | 103 | /** |
---|
| 104 | @brief |
---|
| 105 | A method that is called every tick. |
---|
[8397] | 106 | Implements the behavior of the ForceField. |
---|
[7676] | 107 | @param dt |
---|
| 108 | The amount of time that elapsed since the last tick. |
---|
| 109 | */ |
---|
[2954] | 110 | void ForceField::tick(float dt) |
---|
| 111 | { |
---|
[7674] | 112 | if(this->mode_ == forceFieldMode::tube) |
---|
[3064] | 113 | { |
---|
[7674] | 114 | // Iterate over all objects that could possibly be affected by the ForceField. |
---|
| 115 | for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it) |
---|
[7673] | 116 | { |
---|
| 117 | // The direction of the orientation of the force field. |
---|
| 118 | Vector3 direction = this->getOrientation() * WorldEntity::FRONT; |
---|
| 119 | direction.normalise(); |
---|
[3020] | 120 | |
---|
[7673] | 121 | // Vector from the center of the force field to the object its acting on. |
---|
| 122 | Vector3 distanceVector = it->getWorldPosition() - (this->getWorldPosition() + (this->halfLength_ * direction)); |
---|
[7676] | 123 | |
---|
[7677] | 124 | // The object is outside a ball around the center with radius length/2 of the ForceField. |
---|
[7673] | 125 | if(distanceVector.length() > this->halfLength_) |
---|
[7674] | 126 | continue; |
---|
[3020] | 127 | |
---|
[7673] | 128 | // The distance of the object form the orientation vector. (Or rather the smallest distance from the orientation vector) |
---|
| 129 | float distanceFromDirectionVector = ((it->getWorldPosition() - this->getWorldPosition()).crossProduct(direction)).length(); |
---|
[7676] | 130 | |
---|
| 131 | // If the object in a tube of radius 'radius' around the direction of orientation. |
---|
[7673] | 132 | if(distanceFromDirectionVector >= this->radius_) |
---|
[7674] | 133 | continue; |
---|
[2954] | 134 | |
---|
[7673] | 135 | // Apply a force to the object in the direction of the orientation. |
---|
| 136 | // The force is highest when the object is directly on the direction vector, with a linear decrease, finally reaching zero, when distanceFromDirectionVector = radius. |
---|
[7674] | 137 | it->applyCentralForce((this->radius_ - distanceFromDirectionVector)/this->radius_ * this->velocity_ * direction); |
---|
[7673] | 138 | } |
---|
[7674] | 139 | } |
---|
| 140 | else if(this->mode_ == forceFieldMode::sphere) |
---|
| 141 | { |
---|
| 142 | // Iterate over all objects that could possibly be affected by the ForceField. |
---|
| 143 | for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it) |
---|
[3064] | 144 | { |
---|
[7673] | 145 | Vector3 distanceVector = it->getWorldPosition() - this->getWorldPosition(); |
---|
| 146 | float distance = distanceVector.length(); |
---|
[7676] | 147 | // If the object is within 'radius' distance. |
---|
[7673] | 148 | if (distance < this->radius_) |
---|
| 149 | { |
---|
| 150 | distanceVector.normalise(); |
---|
[7676] | 151 | // 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] | 152 | it->applyCentralForce((this->radius_ - distance)/this->radius_ * this->velocity_ * distanceVector); |
---|
| 153 | } |
---|
[3064] | 154 | } |
---|
[2954] | 155 | } |
---|
[7677] | 156 | else if(this->mode_ == forceFieldMode::invertedSphere) |
---|
| 157 | { |
---|
| 158 | // Iterate over all objects that could possibly be affected by the ForceField. |
---|
| 159 | for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it) |
---|
| 160 | { |
---|
| 161 | Vector3 distanceVector = this->getWorldPosition() - it->getWorldPosition(); |
---|
| 162 | float distance = distanceVector.length(); |
---|
| 163 | // If the object is within 'radius' distance and no more than 'length' away from the boundary of the sphere. |
---|
| 164 | float range = this->radius_ - this->halfLength_*2; |
---|
| 165 | if (distance < this->radius_ && distance > range) |
---|
| 166 | { |
---|
| 167 | distanceVector.normalise(); |
---|
| 168 | // Apply a force proportional to the velocity, with highest force at the boundary of the sphere, linear decreasing until reaching a distance of 'radius-length' from the origin, where the force reaches zero. |
---|
| 169 | it->applyCentralForce((distance-range)/range * this->velocity_ * distanceVector); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | } |
---|
[8397] | 173 | else if(this->mode_ == forceFieldMode::newtonianGravity) |
---|
| 174 | { |
---|
| 175 | // Iterate over all objects that could possibly be affected by the ForceField. |
---|
| 176 | for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it) |
---|
| 177 | { |
---|
| 178 | Vector3 distanceVector = it->getWorldPosition() - this->getWorldPosition(); |
---|
| 179 | float distance = distanceVector.length(); |
---|
| 180 | // If the object is within 'radius' distance and especially further away than massRadius_ |
---|
| 181 | if (distance < this->radius_ && distance > this->massRadius_) |
---|
| 182 | { |
---|
| 183 | distanceVector.normalise(); |
---|
| 184 | /* Apply a central force that follows the newtownian law of gravity, ie.: |
---|
| 185 | * F = G * (M*m) / D^2, |
---|
| 186 | * while M is the mass of the stellar body and m is the mass of the affected object. |
---|
| 187 | * D is the distance from the center of mass of both bodies |
---|
| 188 | * and it should be noted that massRadius_ denotes the radius of the stellar body, |
---|
| 189 | * at which point the force vanishes (you can use this to dictate the size of the body). |
---|
| 190 | * attenFactor_ weakens the field by a constant factor. The -1 is needed for an attractive force. |
---|
| 191 | */ |
---|
| 192 | |
---|
| 193 | // Note: this so called force is actually an acceleration! |
---|
| 194 | it->applyCentralForce((-1) * (ForceField::attenFactor_ * ForceField::gravConstant_ * this->getMass()) / (distance * distance) * distanceVector); |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | } |
---|
[3064] | 198 | } |
---|
[7676] | 199 | |
---|
| 200 | /** |
---|
| 201 | @brief |
---|
| 202 | Set the mode of the ForceField. |
---|
| 203 | @param mode |
---|
| 204 | The mode as a string. |
---|
| 205 | */ |
---|
[7673] | 206 | void ForceField::setMode(const std::string& mode) |
---|
| 207 | { |
---|
[7674] | 208 | if(mode == ForceField::modeTube_s) |
---|
| 209 | this->mode_ = forceFieldMode::tube; |
---|
| 210 | else if(mode == ForceField::modeSphere_s) |
---|
| 211 | this->mode_ = forceFieldMode::sphere; |
---|
[7677] | 212 | else if(mode == ForceField::modeInvertedSphere_s) |
---|
| 213 | this->mode_ = forceFieldMode::invertedSphere; |
---|
[8397] | 214 | else if(mode == ForceField::modeNewtonianGravity_s) |
---|
| 215 | this->mode_ = forceFieldMode::newtonianGravity; |
---|
[7673] | 216 | else |
---|
| 217 | { |
---|
| 218 | COUT(2) << "Wrong mode '" << mode << "' in ForceField. Setting to 'tube'." << std::endl; |
---|
[7674] | 219 | this->mode_ = forceFieldMode::tube; |
---|
[7673] | 220 | } |
---|
| 221 | } |
---|
[7676] | 222 | |
---|
| 223 | /** |
---|
| 224 | @brief |
---|
| 225 | Get the mode of the ForceField. |
---|
| 226 | @return |
---|
| 227 | Returns the mode of the ForceField as a string. |
---|
| 228 | */ |
---|
| 229 | const std::string& ForceField::getMode(void) |
---|
[7673] | 230 | { |
---|
| 231 | switch(this->mode_) |
---|
| 232 | { |
---|
[7674] | 233 | case forceFieldMode::tube: |
---|
| 234 | return ForceField::modeTube_s; |
---|
| 235 | case forceFieldMode::sphere: |
---|
| 236 | return ForceField::modeSphere_s; |
---|
[7677] | 237 | case forceFieldMode::invertedSphere: |
---|
| 238 | return ForceField::modeInvertedSphere_s; |
---|
[8397] | 239 | case forceFieldMode::newtonianGravity: |
---|
| 240 | return ForceField::modeNewtonianGravity_s; |
---|
[7673] | 241 | default: |
---|
[7674] | 242 | return ForceField::modeTube_s; |
---|
[7673] | 243 | } |
---|
| 244 | } |
---|
[7676] | 245 | |
---|
[2954] | 246 | } |
---|