Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/objects/ForceField.cc @ 11730

Last change on this file since 11730 was 11083, checked in by muemart, 9 years ago

Fix some clang-tidy warnings.
Also, Serialise.h was doing some C-style casts that ended up being const casts. I moved those const casts as close to the source as possible and changed the loadAndIncrease functions to not do that.

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