Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/modules/objects/SpaceBoundaries.cc @ 8657

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

Fixing singularity bug in SpaceBoundaries.

  • Property svn:eol-style set to native
File size: 11.2 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 *      Maurus Kaufmann
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "SpaceBoundaries.h"
30
31#include <OgreBillboardSet.h>
32
33#include "core/CoreIncludes.h"
34#include "core/ObjectListIterator.h"
35#include "core/XMLPort.h"
36
37#include "graphics/Billboard.h"
38#include "infos/PlayerInfo.h"
39#include "worldentities/WorldEntity.h"
40#include "worldentities/pawns/Pawn.h"
41
42namespace orxonox
43{
44    CreateFactory(SpaceBoundaries);
45
46    SpaceBoundaries::SpaceBoundaries(BaseObject* creator) : StaticEntity(creator)
47    {
48        RegisterObject(SpaceBoundaries);
49
50        this->setMaxDistance(3000);
51        this->setWarnDistance(this->getMaxDistance());
52        this->setShowDistance(this->getMaxDistance());
53        this->setReaction(0);
54    }
55    SpaceBoundaries::~SpaceBoundaries()
56    {
57        if (this->isInitialized())
58        {
59            this->pawnsIn_.clear();
60       
61            for( std::vector<billboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++)
62            {
63                if( current->billy != NULL)
64                {
65                    delete current->billy;
66                }
67            }
68            this->billboards_.clear();
69        }
70    }
71   
72    void SpaceBoundaries::checkWhoIsIn()
73    {
74        pawnsIn_.clear();
75        for(ObjectListIterator<Pawn> current = ObjectList<Pawn>::begin(); current != ObjectList<Pawn>::end(); ++current)
76        {
77            Pawn* currentPawn = *current;
78            if( this->reaction_ == 0 )
79            {
80                float distance = this->computeDistance(currentPawn);
81                if(distance <= this->maxDistance_)
82                {
83                    pawnsIn_.push_back(currentPawn);
84                }
85            } else if (this->reaction_ == 2) {
86                float distance = this->computeDistance(currentPawn);
87                if(distance >= this->maxDistance_)
88                {
89                    pawnsIn_.push_back(currentPawn);
90                }
91            } else {
92                pawnsIn_.push_back(currentPawn);
93            }
94        }
95    }
96   
97    void SpaceBoundaries::positionBillboard(const Vector3 position)
98    {
99        std::vector<billboardAdministration>::iterator current;
100        for( current = this->billboards_.begin(); current != this->billboards_.end(); current++)
101        {
102            if(!current->usedYet)
103            {
104                break;
105            }
106        }
107        if( current == this->billboards_.end() )
108        {
109            Billboard *tmp = new Billboard(this);
110            tmp->setPosition(position);
111            this->setBillboardOptions( tmp );
112            Vector3 normalisedVec = (position - this->getPosition()).normalisedCopy(); /* Vektor von Kugelmitte nach aussen */
113            tmp->setCommonDirection ( -1.0 * normalisedVec );
114            Vector3 upVector = Vector3(normalisedVec.z, normalisedVec.z, -(normalisedVec.x+normalisedVec.y));
115            upVector.normalise();
116            tmp->setCommonUpVector( upVector );
117            billboardAdministration tmp2 = { true, tmp };
118            this->billboards_.push_back( tmp2 );
119        } else {
120            current->billy->setPosition(position);
121            current->billy->setVisible(true);
122            current->usedYet = true;
123            Vector3 normalisedVec = (position - this->getPosition()).normalisedCopy(); /* Vektor von Kugelmitte nach aussen */
124            current->billy->setCommonDirection ( -1.0 * normalisedVec );
125            Vector3 upVector = Vector3(normalisedVec.z, normalisedVec.z, -(normalisedVec.x+normalisedVec.y));
126            upVector.normalise();
127            current->billy->setCommonUpVector( upVector );
128        }
129    }
130   
131    void SpaceBoundaries::setBillboardOptions(Billboard *billy)
132    {
133        if(billy != NULL)
134        {
135            billy->setMaterial("Grid");
136            billy->setBillboardType(Ogre::BBT_PERPENDICULAR_COMMON);
137            billy->setDefaultDimensions(150, 150);
138            billy->setVisible(true);
139        }
140    }
141   
142    void SpaceBoundaries::removeAllBillboards()
143    {
144        for( std::vector<billboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++ )
145        {
146            current->usedYet = false;
147            current->billy->setVisible(false);
148        }
149    }
150   
151    void SpaceBoundaries::setMaxDistance(float r)
152    {
153        this->maxDistance_ = r;
154    }
155    float SpaceBoundaries::getMaxDistance()
156    {
157        return this->maxDistance_;
158    }
159   
160    void SpaceBoundaries::setWarnDistance(float r)
161    {
162        this->warnDistance_ = r;
163    }
164    float SpaceBoundaries::getWarnDistance()
165    {
166        return this->warnDistance_;
167    }
168   
169    void SpaceBoundaries::setShowDistance(float r)
170    {
171        this->showDistance_ = r;
172    }
173    float SpaceBoundaries::getShowDistance()
174    {
175        return this->showDistance_;
176    }
177   
178    void SpaceBoundaries::setHealthDecrease(float amount)
179    {
180        this->healthDecrease_ = amount/1000;
181    }
182    float SpaceBoundaries::getHealthDecrease()
183    {
184        return this->healthDecrease_;
185    }
186   
187    void SpaceBoundaries::setReaction(int mode)
188    {
189        this->reaction_ = mode;
190    }
191    int SpaceBoundaries::getReaction()
192    {
193        return this->reaction_;
194    }
195
196    void SpaceBoundaries::XMLPort(Element& xmlelement, XMLPort::Mode mode)
197    {
198        SUPER(SpaceBoundaries, XMLPort, xmlelement, mode);
199
200        XMLPortParam(SpaceBoundaries, "maxDistance", setMaxDistance, getMaxDistance, xmlelement, mode);
201        XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode);
202        XMLPortParam(SpaceBoundaries, "showDistance", setShowDistance, getShowDistance, xmlelement, mode);
203        XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode);
204        XMLPortParam(SpaceBoundaries, "reactionMode", setReaction, getReaction, xmlelement, mode);
205    }
206   
207    void SpaceBoundaries::tick(float dt)
208    {
209        this->checkWhoIsIn();
210        this->removeAllBillboards();
211       
212        float distance;
213        bool humanItem;
214        for( std::list<WeakPtr<Pawn> >::iterator current = pawnsIn_.begin(); current != pawnsIn_.end(); current++ )
215        {
216            Pawn* currentPawn = current->get();
217            if( currentPawn && currentPawn->getNode() ) 
218            {
219                distance = this->computeDistance(currentPawn);
220                humanItem = this->isHumanPlayer(currentPawn);
221                COUT(5) << "Distance:" << distance << std::endl; // message for debugging
222                if(distance > this->warnDistance_ && distance < this->maxDistance_) // Display warning
223                {
224                    if(humanItem)
225                    {
226                        this->displayWarning("Attention! You are close to the boundary!");
227                    }
228                }
229                if(/* humanItem &&*/ abs(this->maxDistance_ - distance) < this->showDistance_ )
230                {
231                    this->displayBoundaries(currentPawn); // Show the boundary
232                }
233                if(distance > this->maxDistance_ && (this->reaction_ == 1) )
234                {
235                    if( humanItem )
236                    {
237                        COUT(5) << "Health should be decreasing!" << std::endl;
238                        this->displayWarning("You are out of the area now!");
239                    }
240                    currentPawn->removeHealth( (distance - this->maxDistance_) * this->healthDecrease_);
241                }
242                if( (this->reaction_ == 0) && (distance + 100 > this->maxDistance_)) // Exception: A Pawn can't move more than 100 units per tick.
243                {
244                    this->conditionalBounceBack(currentPawn, distance, dt);
245                }
246                if( this->reaction_ == 2 && (distance - 100 < this->maxDistance_) )
247                {
248                    this->conditionalBounceBack(currentPawn, distance, dt);
249                }
250            }
251        }
252    }
253   
254    float SpaceBoundaries::computeDistance(WorldEntity *item)
255    {
256        if(item != NULL)
257        {
258            Vector3 itemPosition = item->getPosition();
259            return (itemPosition.distance(this->getPosition()));
260        } else {
261            return -1;
262        }
263    }
264   
265    void SpaceBoundaries::displayWarning(const std::string warnText)
266    {   
267        // TODO
268    }
269   
270    void SpaceBoundaries::displayBoundaries(Pawn *item)
271    {
272       
273        Vector3 direction = item->getPosition() - this->getPosition();
274        direction.normalise();
275       
276        Vector3 boundaryPosition = this->getPosition() + direction * this->maxDistance_;
277       
278        this->positionBillboard(boundaryPosition);
279    }
280   
281    void SpaceBoundaries::conditionalBounceBack(Pawn *item, float currentDistance, float dt)
282    {
283        Vector3 normal = item->getPosition() - this->getPosition();
284        normal.normalise();
285        Vector3 velocity = item->getVelocity();
286        float normalSpeed = item->getVelocity().dotProduct(normal);
287       
288        /* Check, whether the Pawn would leave the boundary in the next tick, if so send it back. */
289        if( this->reaction_ == 0 && currentDistance + normalSpeed * dt > this->maxDistance_ - 10 ) // -10: "security measure"
290        {
291            bounceBack(item, &normal, &velocity);
292        } else if (this->reaction_ == 2 && currentDistance - normalSpeed * dt < this->maxDistance_ + 10 ) // 10: "security measure"
293        {
294            normal = normal * (-1);
295            bounceBack(item, &normal, &velocity);
296        }
297    }
298   
299    void SpaceBoundaries::bounceBack(Pawn *item, Vector3 *normal, Vector3 *velocity)
300    {
301        float dampingFactor = 0.5;
302        *velocity = velocity->reflect(*normal);
303        Vector3 acceleration = item->getAcceleration();
304        acceleration = acceleration.reflect(*normal);
305       
306        item->lookAt( *velocity + this->getPosition() );
307       
308        item->setAcceleration(acceleration * dampingFactor);
309        item->setVelocity(*velocity * dampingFactor);
310       
311        item->setPosition( item->getPosition() - *normal * 10 ); // Set the position of the Pawn to be well inside the boundary.
312    }
313   
314    bool SpaceBoundaries::isHumanPlayer(Pawn *item)
315    {
316        if(item != NULL)
317        {
318            if(item->getPlayer())
319            {
320                return item->getPlayer()->isHumanPlayer();
321            }
322        }
323        return false;
324    }
325   
326}
Note: See TracBrowser for help on using the repository browser.