Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7864 was 7801, checked in by dafrick, 14 years ago

Merging presentation2 branch back to trunk.

  • Property svn:eol-style set to native
File size: 8.2 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:
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
40namespace 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";
[2954]47
[7676]48    /**
49    @brief
50        Constructor. Registers the object and initializes some values.
51    */
[2954]52    ForceField::ForceField(BaseObject* creator) : StaticEntity(creator)
53    {
[3064]54        RegisterObject(ForceField);
[2954]55
[3064]56        //Standard Values
57        this->setDirection(Vector3::ZERO);
[7676]58        this->setVelocity(100);
59        this->setDiameter(500);
60        this->setLength(2000);
[7674]61        this->mode_ = forceFieldMode::tube;
[7801]62       
63        this->registerVariables();
[2954]64    }
65
[7676]66    /**
67    @brief
68        Destructor.
69    */
[7673]70    ForceField::~ForceField()
71    {
72    }
[2954]73
[7676]74    /**
75    @brief
76        Creates a ForceField object through XML.
77    */
[2954]78    void ForceField::XMLPort(Element& xmlelement, XMLPort::Mode mode)
79    {
[3064]80        SUPER(ForceField, XMLPort, xmlelement, mode);
[5735]81
[3064]82        XMLPortParam(ForceField, "velocity", setVelocity, getVelocity, xmlelement, mode).defaultValues(100);
83        XMLPortParam(ForceField, "diameter", setDiameter, getDiameter, xmlelement, mode).defaultValues(500);
[7677]84        XMLPortParam(ForceField, "length", setLength  , getLength  , xmlelement, mode).defaultValues(2000);
[7673]85        XMLPortParam(ForceField, "mode", setMode, getMode, xmlelement, mode);
[2954]86    }
[7801]87   
88    void ForceField::registerVariables()
89    {
90        registerVariable(this->velocity_, VariableDirection::ToClient);
91        registerVariable(this->radius_, VariableDirection::ToClient);
92        registerVariable(this->halfLength_, VariableDirection::ToClient);
93        registerVariable(this->mode_, VariableDirection::ToClient);
94    }
[3020]95
[7801]96
[7676]97    /**
98    @brief
99        A method that is called every tick.
100        Implements the behavior of teh ForceField.
101    @param dt
102        The amount of time that elapsed since the last tick.
103    */
[2954]104    void ForceField::tick(float dt)
105    {
[7674]106        if(this->mode_ == forceFieldMode::tube)
[3064]107        {
[7674]108            // Iterate over all objects that could possibly be affected by the ForceField.
109            for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it)
[7673]110            {
111                // The direction of the orientation of the force field.
112                Vector3 direction = this->getOrientation() * WorldEntity::FRONT;
113                direction.normalise();
[3020]114
[7673]115                // Vector from the center of the force field to the object its acting on.
116                Vector3 distanceVector = it->getWorldPosition() - (this->getWorldPosition() + (this->halfLength_ * direction));
[7676]117
[7677]118                // The object is outside a ball around the center with radius length/2 of the ForceField.
[7673]119                if(distanceVector.length() > this->halfLength_)
[7674]120                    continue;
[3020]121
[7673]122                // The distance of the object form the orientation vector. (Or rather the smallest distance from the orientation vector)
123                float distanceFromDirectionVector = ((it->getWorldPosition() - this->getWorldPosition()).crossProduct(direction)).length();
[7676]124
125                // If the object in a tube of radius 'radius' around the direction of orientation.
[7673]126                if(distanceFromDirectionVector >= this->radius_)
[7674]127                    continue;
[2954]128
[7673]129                // Apply a force to the object in the direction of the orientation.
130                // The force is highest when the object is directly on the direction vector, with a linear decrease, finally reaching zero, when distanceFromDirectionVector = radius.
[7674]131                it->applyCentralForce((this->radius_ - distanceFromDirectionVector)/this->radius_ * this->velocity_ * direction);
[7673]132            }
[7674]133        }
134        else if(this->mode_ == forceFieldMode::sphere)
135        {
136            // Iterate over all objects that could possibly be affected by the ForceField.
137            for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it)
[3064]138            {
[7673]139                Vector3 distanceVector = it->getWorldPosition() - this->getWorldPosition();
140                float distance = distanceVector.length();
[7676]141                // If the object is within 'radius' distance.
[7673]142                if (distance < this->radius_)
143                {
144                    distanceVector.normalise();
[7676]145                    // 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]146                    it->applyCentralForce((this->radius_ - distance)/this->radius_ * this->velocity_ * distanceVector);
147                }
[3064]148            }
[2954]149        }
[7677]150        else if(this->mode_ == forceFieldMode::invertedSphere)
151        {
152            // Iterate over all objects that could possibly be affected by the ForceField.
153            for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it)
154            {
155                Vector3 distanceVector = this->getWorldPosition() - it->getWorldPosition();
156                float distance = distanceVector.length();
157                // If the object is within 'radius' distance and no more than 'length' away from the boundary of the sphere.
158                float range = this->radius_ - this->halfLength_*2;
159                if (distance < this->radius_ && distance > range)
160                {
161                    distanceVector.normalise();
162                    // 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.
163                    it->applyCentralForce((distance-range)/range * this->velocity_ * distanceVector);
164                }
165            }
166        }
[3064]167    }
[7676]168
169    /**
170    @brief
171        Set the mode of the ForceField.
172    @param mode
173        The mode as a string.
174    */
[7673]175    void ForceField::setMode(const std::string& mode)
176    {
[7674]177        if(mode == ForceField::modeTube_s)
178            this->mode_ = forceFieldMode::tube;
179        else if(mode == ForceField::modeSphere_s)
180            this->mode_ = forceFieldMode::sphere;
[7677]181        else if(mode == ForceField::modeInvertedSphere_s)
182            this->mode_ = forceFieldMode::invertedSphere;
[7673]183        else
184        {
185            COUT(2) << "Wrong mode '" << mode << "' in ForceField. Setting to 'tube'." << std::endl;
[7674]186            this->mode_ = forceFieldMode::tube;
[7673]187        }
188    }
[7676]189
190    /**
191    @brief
192        Get the mode of the ForceField.
193    @return
194        Returns the mode of the ForceField as a string.
195    */
196    const std::string& ForceField::getMode(void)
[7673]197    {
198        switch(this->mode_)
199        {
[7674]200            case forceFieldMode::tube:
201                return ForceField::modeTube_s;
202            case forceFieldMode::sphere:
203                return ForceField::modeSphere_s;
[7677]204            case forceFieldMode::invertedSphere:
205                return ForceField::modeInvertedSphere_s;
[7673]206            default:
[7674]207                return ForceField::modeTube_s;
[7673]208        }
209    }
[7676]210
[2954]211}
Note: See TracBrowser for help on using the repository browser.