Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud/src/orxonox/overlays/hud/HUDRadar.cc @ 1613

Last change on this file since 1613 was 1613, checked in by rgrieder, 17 years ago
  • Radar now working like before
  • but more of a svn save..

There is class called Radar which takes care of the focus and all objects that can be displayed on a Radar.
The actual visual implementation is in HUDRadar.
To make a object radar viewable, simply implement RadarViewable and set the WorldEntitiy pointer in the constructor (assertation will fail otherwise!).
You can also set a camouflage value between 0 and 1 that tells how good a radar can see an object.
The HUDRadar (or another one) on the other side has a sensitivity between 0 and 1. So only if the sensitivity is higher than the camouflage value, the object gets displayed.

  • Property svn:eol-style set to native
File size: 7.5 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 *      Yuning Chai
24 *   Co-authors:
25 *      Felix Schulthess
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "HUDRadar.h"
31
32#include <assert.h>
33#include <OgreOverlayManager.h>
34#include <OgreMaterialManager.h>
35
36#include "core/ConsoleCommand.h"
37#include "objects/SpaceShip.h"
38#include "objects/WorldEntity.h"
39#include "tools/TextureGenerator.h"
40#include "RadarViewable.h"
41
42namespace orxonox
43{
44    CreateFactory(HUDRadar);
45    CreateFactory(RadarShape);
46
47    using namespace Ogre;
48
49    HUDRadar::HUDRadar()
50      : background_(0)
51      , marker_(0)
52      , sensitivity_(1.0f)
53    {
54        RegisterObject(HUDRadar);
55    }
56
57    HUDRadar::~HUDRadar()
58    {
59        if (this->isInitialized())
60        {
61            /*if (this->background_)
62                OverlayManager::getSingleton().destroyOverlayElement(this->background_);
63            while (this->radarDots_.size() > 0)
64            {
65                OverlayManager::getSingleton().destroyOverlayElement(this->radarDots_[this->radarDots_.size() - 1]);
66                this->radarDots_.pop_back();
67            }*/
68        }
69    }
70
71    void HUDRadar::XMLPort(Element& xmlElement, XMLPort::Mode mode)
72    {
73        OrxonoxOverlay::XMLPort(xmlElement, mode);
74
75        if (mode == XMLPort::LoadObject)
76        {
77            this->sensitivity_ = 1.0f;
78            this->halfDotSizeDistance_ = 3000.0f;
79            this->maximumDotSize_ = 0.1;
80        }
81
82        XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlElement, mode);
83        XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlElement, mode);
84        XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlElement, mode);
85
86        shapeMaterials_[RadarViewable::Dot]      = "RadarSquare.tga";
87        shapeMaterials_[RadarViewable::Triangle] = "RadarSquare.tga";
88        shapeMaterials_[RadarViewable::Square]   = "RadarSquare.tga";
89
90        if (mode == XMLPort::LoadObject)
91        {
92            background_ = (Ogre::PanelOverlayElement*)Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_Background");
93            background_->setMaterialName("Orxonox/Radar");
94            overlay_->add2D(background_);
95
96            marker_ = (Ogre::PanelOverlayElement*)Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_Marker");
97            marker_->setMaterialName("Orxonox/RadarMarker");
98            overlay_->add2D(marker_);
99            marker_->hide();
100        }
101    }
102
103    /*void HUDRadar::addShape(RadarShape* shape)
104    {
105        this->shapes_[shape->getIndex()] = shape;
106    }
107
108    RadarShape* HUDRadar::getShape(unsigned int index) const
109    {
110        if (index < this->shapes_.size())
111        {
112            std::map<unsigned int, RadarShape*>::const_iterator it = shapes_.begin();
113            for (unsigned int i = 0; i != index; ++it, ++i)
114                ;
115            return (*it).second;
116        }
117        else
118            return 0;
119    }*/
120
121    void HUDRadar::displayObject(RadarViewable* object, bool bIsMarked)
122    {
123        const WorldEntity* wePointer = object->getWorldEntity();
124
125        // Just to be sure that we actually have a WorldEntity
126        // We could do a dynamic_cast, but that's a lot slower
127        if (!wePointer)
128        {
129            CCOUT(4) << "Cannot display a non-WorldEntitiy on the radar" << std::endl;
130            return;
131        }
132
133        /*static int counter = 0;
134        if (++counter < 1120 && counter > 120)
135        {
136            // we have to create a new entry
137            Ogre::PanelOverlayElement* panel = static_cast<Ogre::PanelOverlayElement*>(
138                Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberStr()));
139            // get right material
140            panel->setMaterialName("Orxonox/RadarSquare");
141            panel->setDimensions(0.03, 0.03);
142            panel->setPosition((1.0 + (rand() & 0xFF) / 256.0 - 0.001) * 0.5, (1.0 - (rand() & 0xFF) / 256.0 - 0.001) * 0.5);
143            panel->show();
144            this->overlay_->add2D(panel);
145            this->overlay_->show();
146        }*/
147
148        // try to find a panel already created
149        Ogre::PanelOverlayElement* panel;
150        std::map<RadarViewable*, Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.find(object);
151        if (it == this->radarDots_.end())
152        {
153            // we have to create a new entry
154            panel = static_cast<Ogre::PanelOverlayElement*>(
155                Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberStr()));
156            radarDots_[object] = panel;
157            // get right material
158            panel->setMaterialName(TextureGenerator::getMaterialName(
159                shapeMaterials_[object->getRadarObjectType()], object->getRadarObjectColour()));
160            this->overlay_->add2D(panel);
161        }
162        else
163            panel = (*it).second;
164
165        // set size to fit distance...
166        float distance = (wePointer->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length();
167        // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda)
168        float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance);
169        panel->setDimensions(size, size);
170
171        // calc position on radar...
172        Vector2 coord = get2DViewcoordinates(SpaceShip::getLocalShip()->getPosition(), SpaceShip::getLocalShip()->getDir(), SpaceShip::getLocalShip()->getOrth(), wePointer->getWorldPosition());
173        coord *= Ogre::Math::PI / 3.5; // small adjustment to make it fit the texture
174        panel->setPosition((1.0 + coord.x - size) * 0.5, (1.0 - coord.y - size) * 0.5);
175
176        if (bIsMarked)
177        {
178            this->marker_->show();
179            this->marker_->setDimensions(size * 1.2, size * 1.2);
180            this->marker_->setPosition((1.0 + coord.x - size * 1.2) * 0.5, (1.0 - coord.y - size * 1.2) * 0.5);
181        }
182    }
183
184    void HUDRadar::radarTick(float dt)
185    {
186    }
187
188    /*void HUDRadar::tick(float dt)
189    {
190        // iterate through all RadarObjects
191        unsigned int i = 0;
192        for (Iterator<RadarViewable> it = ObjectList<RadarViewable>::start(); it; ++it, ++i)
193        {
194            if ((*it)->isVisibleOnRadar())
195            {
196            }
197        }
198    }*/
199
200    /*void HUDRadar::listObjects()
201    {
202        COUT(3) << "List of RadarObjects:\n";
203        // iterate through all Radar Objects
204        unsigned int i = 0;
205        for (Iterator<RadarViewable> it = ObjectList<RadarViewable>::start(); it; ++it, ++i)
206        {
207            COUT(3) << i++ << ": " << (*it)->getWorldEntity()->getWorldPosition() << std::endl;
208        }
209    }*/
210}
Note: See TracBrowser for help on using the repository browser.