Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9015 was 8858, checked in by landauf, 13 years ago

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

  • orxout levelname message
  • orxout_context levelname context message
  • shortcuts: log message, error message, warning message, status message, info message, debug message
  • Property svn:eol-style set to native
File size: 10.6 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 *      Kevin Young
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    /*static*/ const std::string ForceField::modeNewtonianGravity_s = "newtonianGravity";
48    /*static*/ const float ForceField::gravConstant_ = 6.673e-11;
49    /*static*/ const float ForceField::attenFactor_ = 1;
50
51    /**
52    @brief
53        Constructor. Registers the object and initializes some values.
54    */
55    ForceField::ForceField(BaseObject* creator) : StaticEntity(creator)
56    {
57        RegisterObject(ForceField);
58
59        //Standard Values
60        this->setDirection(Vector3::ZERO);
61        this->setVelocity(100);
62        this->setDiameter(500);
63        this->setMassDiameter(0);   //! We allow point-masses
64        this->setLength(2000);
65        this->mode_ = forceFieldMode::tube;
66       
67        this->registerVariables();
68    }
69
70    /**
71    @brief
72        Destructor.
73    */
74    ForceField::~ForceField()
75    {
76    }
77
78    /**
79    @brief
80        Creates a ForceField object through XML.
81    */
82    void ForceField::XMLPort(Element& xmlelement, XMLPort::Mode mode)
83    {
84        SUPER(ForceField, XMLPort, xmlelement, mode);
85
86        XMLPortParam(ForceField, "velocity", setVelocity, getVelocity, xmlelement, mode).defaultValues(100);
87        XMLPortParam(ForceField, "diameter", setDiameter, getDiameter, xmlelement, mode).defaultValues(500);
88        XMLPortParam(ForceField, "massDiameter", setMassDiameter, getMassDiameter, xmlelement, mode).defaultValues(0);
89        XMLPortParam(ForceField, "length", setLength  , getLength  , xmlelement, mode).defaultValues(2000);
90        XMLPortParam(ForceField, "mode", setMode, getMode, xmlelement, mode);
91    }
92   
93    void ForceField::registerVariables()
94    {
95        registerVariable(this->velocity_, VariableDirection::ToClient);
96        registerVariable(this->radius_, VariableDirection::ToClient);
97        registerVariable(this->massRadius_, VariableDirection::ToClient);
98        registerVariable(this->halfLength_, VariableDirection::ToClient);
99        registerVariable(this->mode_, VariableDirection::ToClient);
100    }
101
102
103    /**
104    @brief
105        A method that is called every tick.
106        Implements the behavior of the ForceField.
107    @param dt
108        The amount of time that elapsed since the last tick.
109    */
110    void ForceField::tick(float dt)
111    {
112        if(this->mode_ == forceFieldMode::tube)
113        {
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)
116            {
117                // The direction of the orientation of the force field.
118                Vector3 direction = this->getOrientation() * WorldEntity::FRONT;
119                direction.normalise();
120
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));
123
124                // The object is outside a ball around the center with radius length/2 of the ForceField.
125                if(distanceVector.length() > this->halfLength_)
126                    continue;
127
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();
130
131                // If the object in a tube of radius 'radius' around the direction of orientation.
132                if(distanceFromDirectionVector >= this->radius_)
133                    continue;
134
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.
137                it->applyCentralForce((this->radius_ - distanceFromDirectionVector)/this->radius_ * this->velocity_ * direction);
138            }
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)
144            {
145                Vector3 distanceVector = it->getWorldPosition() - this->getWorldPosition();
146                float distance = distanceVector.length();
147                // If the object is within 'radius' distance.
148                if (distance < this->radius_)
149                {
150                    distanceVector.normalise();
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.
152                    it->applyCentralForce((this->radius_ - distance)/this->radius_ * this->velocity_ * distanceVector);
153                }
154            }
155        }
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        }
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        }
198    }
199
200    /**
201    @brief
202        Set the mode of the ForceField.
203    @param mode
204        The mode as a string.
205    */
206    void ForceField::setMode(const std::string& mode)
207    {
208        if(mode == ForceField::modeTube_s)
209            this->mode_ = forceFieldMode::tube;
210        else if(mode == ForceField::modeSphere_s)
211            this->mode_ = forceFieldMode::sphere;
212        else if(mode == ForceField::modeInvertedSphere_s)
213            this->mode_ = forceFieldMode::invertedSphere;
214        else if(mode == ForceField::modeNewtonianGravity_s)
215            this->mode_ = forceFieldMode::newtonianGravity;
216        else
217        {
218            orxout(internal_warning) << "Wrong mode '" << mode << "' in ForceField. Setting to 'tube'." << endl;
219            this->mode_ = forceFieldMode::tube;
220        }
221    }
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)
230    {
231        switch(this->mode_)
232        {
233            case forceFieldMode::tube:
234                return ForceField::modeTube_s;
235            case forceFieldMode::sphere:
236                return ForceField::modeSphere_s;
237            case forceFieldMode::invertedSphere:
238                return ForceField::modeInvertedSphere_s;
239            case forceFieldMode::newtonianGravity:
240                return ForceField::modeNewtonianGravity_s;
241            default:
242                return ForceField::modeTube_s;
243        }
244    }
245
246}
Note: See TracBrowser for help on using the repository browser.