Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7674 was 7674, checked in by dafrick, 13 years ago

Resolving some stupid bugs and some adding some minor improvements.

  • Property svn:eol-style set to native
File size: 5.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
47    ForceField::ForceField(BaseObject* creator) : StaticEntity(creator)
48    {
49        RegisterObject(ForceField);
50
51        //Standard Values
52        this->setDirection(Vector3::ZERO);
53        this->velocity_ = 100;
54        this->diameter_ = 500;
55        this->length_ = 5000;
56        this->mode_ = forceFieldMode::tube;
57    }
58
59    ForceField::~ForceField()
60    {
61    }
62
63    void ForceField::XMLPort(Element& xmlelement, XMLPort::Mode mode)
64    {
65        SUPER(ForceField, XMLPort, xmlelement, mode);
66
67        //For correct xml import use: position, direction, velocity, scale
68        XMLPortParam(ForceField, "velocity", setVelocity, getVelocity, xmlelement, mode).defaultValues(100);
69        XMLPortParam(ForceField, "diameter", setDiameter, getDiameter, xmlelement, mode).defaultValues(500);
70        XMLPortParam(ForceField, "length"  , setLength  , getLength  , xmlelement, mode).defaultValues(2000);
71        XMLPortParam(ForceField, "mode", setMode, getMode, xmlelement, mode);
72        COUT(0) << "ForceField created " << this->velocity_ << " " << this->diameter_ << " " << this->radius_ << " " << this->length_ << " " << this->halfLength_ << " " << this->getMode() << std::endl;
73    }
74
75    void ForceField::tick(float dt)
76    {
77        if(this->mode_ == forceFieldMode::tube)
78        {
79            // Iterate over all objects that could possibly be affected by the ForceField.
80            for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it)
81            {
82                // The direction of the orientation of the force field.
83                Vector3 direction = this->getOrientation() * WorldEntity::FRONT;
84                direction.normalise();
85
86                // Vector from the center of the force field to the object its acting on.
87                // TODO: This could probably be simplified.
88                Vector3 distanceVector = it->getWorldPosition() - (this->getWorldPosition() + (this->halfLength_ * direction));
89               
90                // The object is outside of the length of the ForceField.
91                if(distanceVector.length() > this->halfLength_)
92                    continue;
93
94                // The distance of the object form the orientation vector. (Or rather the smallest distance from the orientation vector)
95                float distanceFromDirectionVector = ((it->getWorldPosition() - this->getWorldPosition()).crossProduct(direction)).length();
96               
97                // If the object in a tube of radius diameter/2 around the direction of orientation.
98                if(distanceFromDirectionVector >= this->radius_)
99                    continue;
100
101                // Apply a force to the object in the direction of the orientation.
102                // The force is highest when the object is directly on the direction vector, with a linear decrease, finally reaching zero, when distanceFromDirectionVector = radius.
103                it->applyCentralForce((this->radius_ - distanceFromDirectionVector)/this->radius_ * this->velocity_ * direction);
104            }
105        }
106        else if(this->mode_ == forceFieldMode::sphere)
107        {
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)
110            {
111                Vector3 distanceVector = it->getWorldPosition() - this->getWorldPosition();
112                float distance = distanceVector.length();
113                if (distance < this->radius_)
114                {
115                    distanceVector.normalise();
116                    it->applyCentralForce((this->radius_ - distance)/this->radius_ * this->velocity_ * distanceVector);
117                }
118            }
119        }
120    }
121   
122    void ForceField::setMode(const std::string& mode)
123    {
124        if(mode == ForceField::modeTube_s)
125            this->mode_ = forceFieldMode::tube;
126        else if(mode == ForceField::modeSphere_s)
127            this->mode_ = forceFieldMode::sphere;
128        else
129        {
130            COUT(2) << "Wrong mode '" << mode << "' in ForceField. Setting to 'tube'." << std::endl;
131            this->mode_ = forceFieldMode::tube;
132        }
133    }
134   
135    inline const std::string& ForceField::getMode(void)
136    {
137        switch(this->mode_)
138        {
139            case forceFieldMode::tube:
140                return ForceField::modeTube_s;
141            case forceFieldMode::sphere:
142                return ForceField::modeSphere_s;
143            default:
144                return ForceField::modeTube_s;
145        }
146    }
147}
148
149
150
151
152
Note: See TracBrowser for help on using the repository browser.