Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/lastmanstanding2/src/modules/objects/ForceField.cc @ 8740

Last change on this file since 8740 was 7677, checked in by dafrick, 14 years ago

Adding yet another class of ForceFields and using it in The Time Machine.

  • Property svn:eol-style set to native
File size: 7.9 KB
Line 
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
29/**
30    @file ForceField.cc
31    @brief Implementation of the ForceField class.
32*/
33
34#include "ForceField.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "worldentities/MobileEntity.h"
39
40namespace orxonox
41{
42    CreateFactory(ForceField);
43
44    /*static*/ const std::string ForceField::modeTube_s = "tube";
45    /*static*/ const std::string ForceField::modeSphere_s = "sphere";
46    /*static*/ const std::string ForceField::modeInvertedSphere_s = "invertedSphere";
47
48    /**
49    @brief
50        Constructor. Registers the object and initializes some values.
51    */
52    ForceField::ForceField(BaseObject* creator) : StaticEntity(creator)
53    {
54        RegisterObject(ForceField);
55
56        //Standard Values
57        this->setDirection(Vector3::ZERO);
58        this->setVelocity(100);
59        this->setDiameter(500);
60        this->setLength(2000);
61        this->mode_ = forceFieldMode::tube;
62    }
63
64    /**
65    @brief
66        Destructor.
67    */
68    ForceField::~ForceField()
69    {
70    }
71
72    /**
73    @brief
74        Creates a ForceField object through XML.
75    */
76    void ForceField::XMLPort(Element& xmlelement, XMLPort::Mode mode)
77    {
78        SUPER(ForceField, XMLPort, xmlelement, mode);
79
80        XMLPortParam(ForceField, "velocity", setVelocity, getVelocity, xmlelement, mode).defaultValues(100);
81        XMLPortParam(ForceField, "diameter", setDiameter, getDiameter, xmlelement, mode).defaultValues(500);
82        XMLPortParam(ForceField, "length", setLength  , getLength  , xmlelement, mode).defaultValues(2000);
83        XMLPortParam(ForceField, "mode", setMode, getMode, xmlelement, mode);
84    }
85
86    /**
87    @brief
88        A method that is called every tick.
89        Implements the behavior of teh ForceField.
90    @param dt
91        The amount of time that elapsed since the last tick.
92    */
93    void ForceField::tick(float dt)
94    {
95        if(this->mode_ == forceFieldMode::tube)
96        {
97            // Iterate over all objects that could possibly be affected by the ForceField.
98            for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it)
99            {
100                // The direction of the orientation of the force field.
101                Vector3 direction = this->getOrientation() * WorldEntity::FRONT;
102                direction.normalise();
103
104                // Vector from the center of the force field to the object its acting on.
105                Vector3 distanceVector = it->getWorldPosition() - (this->getWorldPosition() + (this->halfLength_ * direction));
106
107                // The object is outside a ball around the center with radius length/2 of the ForceField.
108                if(distanceVector.length() > this->halfLength_)
109                    continue;
110
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();
113
114                // If the object in a tube of radius 'radius' around the direction of orientation.
115                if(distanceFromDirectionVector >= this->radius_)
116                    continue;
117
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.
120                it->applyCentralForce((this->radius_ - distanceFromDirectionVector)/this->radius_ * this->velocity_ * direction);
121            }
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)
127            {
128                Vector3 distanceVector = it->getWorldPosition() - this->getWorldPosition();
129                float distance = distanceVector.length();
130                // If the object is within 'radius' distance.
131                if (distance < this->radius_)
132                {
133                    distanceVector.normalise();
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.
135                    it->applyCentralForce((this->radius_ - distance)/this->radius_ * this->velocity_ * distanceVector);
136                }
137            }
138        }
139        else if(this->mode_ == forceFieldMode::invertedSphere)
140        {
141            // Iterate over all objects that could possibly be affected by the ForceField.
142            for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it)
143            {
144                Vector3 distanceVector = this->getWorldPosition() - it->getWorldPosition();
145                float distance = distanceVector.length();
146                // If the object is within 'radius' distance and no more than 'length' away from the boundary of the sphere.
147                float range = this->radius_ - this->halfLength_*2;
148                if (distance < this->radius_ && distance > range)
149                {
150                    distanceVector.normalise();
151                    // 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.
152                    it->applyCentralForce((distance-range)/range * this->velocity_ * distanceVector);
153                }
154            }
155        }
156    }
157
158    /**
159    @brief
160        Set the mode of the ForceField.
161    @param mode
162        The mode as a string.
163    */
164    void ForceField::setMode(const std::string& mode)
165    {
166        if(mode == ForceField::modeTube_s)
167            this->mode_ = forceFieldMode::tube;
168        else if(mode == ForceField::modeSphere_s)
169            this->mode_ = forceFieldMode::sphere;
170        else if(mode == ForceField::modeInvertedSphere_s)
171            this->mode_ = forceFieldMode::invertedSphere;
172        else
173        {
174            COUT(2) << "Wrong mode '" << mode << "' in ForceField. Setting to 'tube'." << std::endl;
175            this->mode_ = forceFieldMode::tube;
176        }
177    }
178
179    /**
180    @brief
181        Get the mode of the ForceField.
182    @return
183        Returns the mode of the ForceField as a string.
184    */
185    const std::string& ForceField::getMode(void)
186    {
187        switch(this->mode_)
188        {
189            case forceFieldMode::tube:
190                return ForceField::modeTube_s;
191            case forceFieldMode::sphere:
192                return ForceField::modeSphere_s;
193            case forceFieldMode::invertedSphere:
194                return ForceField::modeInvertedSphere_s;
195            default:
196                return ForceField::modeTube_s;
197        }
198    }
199
200}
Note: See TracBrowser for help on using the repository browser.