Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/animated_space_boundaries/src/modules/objects/SpaceBoundaries.cc @ 10181

Last change on this file since 10181 was 8710, checked in by FelixSchulthess, 14 years ago

space boundary billboard is now animated. test run with myTestLevel.oxw

  • Property svn:eol-style set to native
File size: 12.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 *      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 "util/Math.h"
38
39#include "graphics/Billboard.h"
40#include "infos/PlayerInfo.h"
41#include "worldentities/WorldEntity.h"
42#include "worldentities/pawns/Pawn.h"
43
44#ifndef PI2
45    #define PI2    1.570796327
46#endif
47#ifndef PI
48    #define PI      3.141592654
49#endif
50
51namespace orxonox
52{
53    CreateFactory(SpaceBoundaries);
54
55    SpaceBoundaries::SpaceBoundaries(BaseObject* creator) : StaticEntity(creator)
56    {
57        RegisterObject(SpaceBoundaries);
58
59        this->setMaxDistance(3000);
60        this->setWarnDistance(this->getMaxDistance());
61        this->setShowDistance(this->getMaxDistance());
62        this->setReaction(0);
63    }
64    SpaceBoundaries::~SpaceBoundaries()
65    {
66        if (this->isInitialized())
67        {
68            this->pawnsIn_.clear();
69       
70            for( std::vector<BillboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++)
71            {
72                if( current->billy != NULL)
73                {
74                    delete current->billy;
75                }
76            }
77            this->billboards_.clear();
78        }
79    }
80   
81    void SpaceBoundaries::checkWhoIsIn()
82    {
83        pawnsIn_.clear();
84        for(ObjectListIterator<Pawn> current = ObjectList<Pawn>::begin(); current != ObjectList<Pawn>::end(); ++current)
85        {
86            Pawn* currentPawn = *current;
87            if( this->reaction_ == 0 )
88            {
89                float distance = this->computeDistance(currentPawn);
90                if(distance <= this->maxDistance_)
91                {
92                    pawnsIn_.push_back(currentPawn);
93                }
94            } else if (this->reaction_ == 2) {
95                float distance = this->computeDistance(currentPawn);
96                if(distance >= this->maxDistance_)
97                {
98                    pawnsIn_.push_back(currentPawn);
99                }
100            } else {
101                pawnsIn_.push_back(currentPawn);
102            }
103        }
104    }
105   
106    void SpaceBoundaries::positionBillboard(const Vector3& position, float alpha)
107    {
108        size_t current;
109        for (current = 0; current < this->billboards_.size(); ++current)
110            if (!this->billboards_[current].usedYet)
111                break;
112
113        if (current == this->billboards_.size())
114        {
115            Billboard* billboard = new Billboard(this);
116            billboard->setPosition(position);
117            billboard->setSyncMode(ObjectDirection::None);
118            this->setBillboardOptions(billboard);
119            BillboardAdministration ba = {true, billboard};
120            this->billboards_.push_back(ba);
121        }
122
123        this->billboards_[current].billy->setPosition(position);
124        this->billboards_[current].billy->setVisible(true);
125        this->billboards_[current].billy->setColour(ColourValue(1, 1, 1, alpha));
126        this->billboards_[current].usedYet = true;
127
128        // in order to animate the billboard, we use a       
129        // transform to spherical coordinates according to
130        // http://de.wikipedia.org/wiki/Kugelkoordinaten convention
131
132        Vector3 dirVect = position - this->getPosition();
133
134        float phi; // this is the longitude on the sphere (-pi, pi)
135        if      (dirVect.x >  0) phi = atan(dirVect.y/dirVect.x);
136        else if (dirVect.x == 0) phi = sgn(dirVect.y) * PI2;
137        else if (dirVect.y >= 0) phi = atan(dirVect.y/dirVect.x) + PI;
138        else if (dirVect.y <  0) phi = atan(dirVect.y/dirVect.x) - PI;
139       
140        float theta; // this is the latitude measured from z axis (0, pi)
141        theta = acos(dirVect.z / dirVect.length());
142
143        Vector3 e_r = dirVect.normalisedCopy();
144        //Vector3 e_phi = Vector3(-sin(phi), cos(phi), 0);
145        Vector3 e_theta = Vector3(cos(theta)*cos(phi), cos(theta)*sin(phi), -sin(theta));
146
147        // set billboard orientation
148        this->billboards_[current].billy->setCommonDirection(e_r);
149        this->billboards_[current].billy->setCommonUpVector(e_theta);
150
151        // set billboard texture coordinates
152        float u = (phi - floor(phi)) * this->maxDistance_ / 200;
153        float v = (theta -floor(theta)) * this->maxDistance_ / 200;
154        Ogre::FloatRect textureCoords = Ogre::FloatRect(0.5-u, 0.5-v, 1.0-u, 1.0-v);
155        this->billboards_[current].billy->setTextureCoords(&textureCoords, 1);
156       
157        // debug output
158        //COUT(0) << "dirVect: " << dirVect << " len: " << dirVect.length() << std::endl;
159        //COUT(0) << "phi: " << phi << std::endl;
160        //COUT(0) << "theta: " << theta << std::endl;
161        //COUT(0) << "e_r: " << e_r  << " len: " << e_r.length() << std::endl;
162        //COUT(0) << "e_phi: " << e_phi  << " len: " << e_phi.length() << std::endl;
163        //COUT(0) << "e_theta: " << e_theta  << " len: " << e_theta.length() << std::endl;
164    }
165   
166    void SpaceBoundaries::setBillboardOptions(Billboard *billy)
167    {
168        if(billy)
169        {
170            billy->setMaterial("Grid02");
171            billy->setBillboardType(Ogre::BBT_PERPENDICULAR_COMMON);
172            billy->setDefaultDimensions(100, 100);
173            billy->setVisible(true);
174        }
175    }
176   
177    void SpaceBoundaries::removeAllBillboards()
178    {
179        for( std::vector<BillboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++ )
180        {
181            current->usedYet = false;
182            current->billy->setVisible(false);
183        }
184    }
185   
186    void SpaceBoundaries::setMaxDistance(float r)
187    {
188        this->maxDistance_ = r;
189    }
190    float SpaceBoundaries::getMaxDistance()
191    {
192        return this->maxDistance_;
193    }
194   
195    void SpaceBoundaries::setWarnDistance(float r)
196    {
197        this->warnDistance_ = r;
198    }
199    float SpaceBoundaries::getWarnDistance()
200    {
201        return this->warnDistance_;
202    }
203   
204    void SpaceBoundaries::setShowDistance(float r)
205    {
206        this->showDistance_ = r;
207    }
208    float SpaceBoundaries::getShowDistance()
209    {
210        return this->showDistance_;
211    }
212   
213    void SpaceBoundaries::setHealthDecrease(float amount)
214    {
215        this->healthDecrease_ = amount/1000;
216    }
217    float SpaceBoundaries::getHealthDecrease()
218    {
219        return this->healthDecrease_;
220    }
221   
222    void SpaceBoundaries::setReaction(int mode)
223    {
224        this->reaction_ = mode;
225    }
226    int SpaceBoundaries::getReaction()
227    {
228        return this->reaction_;
229    }
230
231    void SpaceBoundaries::XMLPort(Element& xmlelement, XMLPort::Mode mode)
232    {
233        SUPER(SpaceBoundaries, XMLPort, xmlelement, mode);
234
235        XMLPortParam(SpaceBoundaries, "maxDistance", setMaxDistance, getMaxDistance, xmlelement, mode);
236        XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode);
237        XMLPortParam(SpaceBoundaries, "showDistance", setShowDistance, getShowDistance, xmlelement, mode);
238        XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode);
239        XMLPortParam(SpaceBoundaries, "reactionMode", setReaction, getReaction, xmlelement, mode);
240    }
241   
242    void SpaceBoundaries::tick(float dt)
243    {
244        this->checkWhoIsIn();
245        this->removeAllBillboards();
246       
247        float distance;
248        bool humanItem;
249        for( std::list<WeakPtr<Pawn> >::iterator current = pawnsIn_.begin(); current != pawnsIn_.end(); current++ )
250        {
251            Pawn* currentPawn = current->get();
252            if( currentPawn && currentPawn->getNode() ) 
253            {
254                distance = this->computeDistance(currentPawn);
255                humanItem = this->isHumanPlayer(currentPawn);
256                COUT(5) << "Distance:" << distance << std::endl; // message for debugging
257                if(distance > this->warnDistance_ && distance < this->maxDistance_) // Display warning
258                {
259                    if(humanItem)
260                    {
261                        this->displayWarning("Attention! You are close to the boundary!");
262                    }
263                }
264                if(/* humanItem &&*/ abs(this->maxDistance_ - distance) < this->showDistance_ )
265                {
266                    this->displayBoundaries(currentPawn, 1.0f - fabs(this->maxDistance_ - distance) / this->showDistance_); // Show the boundary
267                }
268                if(distance > this->maxDistance_ && (this->reaction_ == 1) )
269                {
270                    if( humanItem )
271                    {
272                        COUT(5) << "Health should be decreasing!" << std::endl;
273                        this->displayWarning("You are out of the area now!");
274                    }
275                    currentPawn->removeHealth( (distance - this->maxDistance_) * this->healthDecrease_);
276                }
277                if( (this->reaction_ == 0) && (distance + 100 > this->maxDistance_)) // Exception: A Pawn can't move more than 100 units per tick.
278                {
279                    this->conditionalBounceBack(currentPawn, distance, dt);
280                }
281                if( this->reaction_ == 2 && (distance - 100 < this->maxDistance_) )
282                {
283                    this->conditionalBounceBack(currentPawn, distance, dt);
284                }
285            }
286        }
287    }
288   
289    float SpaceBoundaries::computeDistance(WorldEntity *item)
290    {
291        if(item != NULL)
292        {
293            Vector3 itemPosition = item->getPosition();
294            return (itemPosition.distance(this->getPosition()));
295        } else {
296            return -1;
297        }
298    }
299   
300    void SpaceBoundaries::displayWarning(const std::string warnText)
301    {   
302        // TODO
303    }
304   
305    void SpaceBoundaries::displayBoundaries(Pawn *item, float alpha)
306    {
307       
308        Vector3 direction = item->getPosition() - this->getPosition();
309        direction.normalise();
310       
311        Vector3 boundaryPosition = this->getPosition() + direction * this->maxDistance_;
312       
313        this->positionBillboard(boundaryPosition, alpha);
314    }
315   
316    void SpaceBoundaries::conditionalBounceBack(Pawn *item, float currentDistance, float dt)
317    {
318        Vector3 normal = item->getPosition() - this->getPosition();
319        normal.normalise();
320        Vector3 velocity = item->getVelocity();
321        float normalSpeed = item->getVelocity().dotProduct(normal);
322       
323        /* Check, whether the Pawn would leave the boundary in the next tick, if so send it back. */
324        if( this->reaction_ == 0 && currentDistance + normalSpeed * dt > this->maxDistance_ - 10 ) // -10: "security measure"
325        {
326            bounceBack(item, &normal, &velocity);
327        } else if (this->reaction_ == 2 && currentDistance - normalSpeed * dt < this->maxDistance_ + 10 ) // 10: "security measure"
328        {
329            normal = normal * (-1);
330            bounceBack(item, &normal, &velocity);
331        }
332    }
333   
334    void SpaceBoundaries::bounceBack(Pawn *item, Vector3 *normal, Vector3 *velocity)
335    {
336        float dampingFactor = 0.5;
337        *velocity = velocity->reflect(*normal);
338        Vector3 acceleration = item->getAcceleration();
339        acceleration = acceleration.reflect(*normal);
340       
341        item->lookAt( *velocity + this->getPosition() );
342       
343        item->setAcceleration(acceleration * dampingFactor);
344        item->setVelocity(*velocity * dampingFactor);
345       
346        item->setPosition( item->getPosition() - *normal * 10 ); // Set the position of the Pawn to be well inside the boundary.
347    }
348   
349    bool SpaceBoundaries::isHumanPlayer(Pawn *item)
350    {
351        if(item != NULL)
352        {
353            if(item->getPlayer())
354            {
355                return item->getPlayer()->isHumanPlayer();
356            }
357        }
358        return false;
359    }
360   
361}
Note: See TracBrowser for help on using the repository browser.