Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/spaceNavigation/src/modules/overlays/hud/HUDNavigation.cc @ 9429

Last change on this file since 9429 was 9429, checked in by mottetb, 11 years ago

aimMarkerSize Problem

  • Property svn:eol-style set to native
File size: 20.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 *      Felix Schulthess
24 *   Co-authors:
25 *      Reto Grieder
26 *      Oliver Scheuss
27 *      Matthias Spalinger
28 *
29 */
30
31#include "HUDNavigation.h"
32
33#include <OgreCamera.h>
34#include <OgreFontManager.h>
35#include <OgreOverlayManager.h>
36#include <OgreTextAreaOverlayElement.h>
37#include <OgrePanelOverlayElement.h>
38
39#include "util/Math.h"
40#include "util/Convert.h"
41#include "core/CoreIncludes.h"
42#include "core/XMLPort.h"
43#include "CameraManager.h"
44#include "Scene.h"
45#include "Radar.h"
46#include "graphics/Camera.h"
47#include "controllers/HumanController.h"
48#include "worldentities/pawns/Pawn.h"
49#include "worldentities/WorldEntity.h"
50#include "core/ConfigValueIncludes.h"
51#include "tools/TextureGenerator.h"
52// #include <boost/bind/bind_template.hpp>
53
54
55namespace orxonox
56{
57    static bool compareDistance(std::pair<RadarViewable*, unsigned int> a,
58            std::pair<RadarViewable*, unsigned int> b)
59    {
60        return a.second < b.second;
61    }
62    CreateFactory ( HUDNavigation );
63
64    HUDNavigation::HUDNavigation(BaseObject* creator) :
65        OrxonoxOverlay(creator)
66    {
67        RegisterObject(HUDNavigation)
68;        this->setConfigValues();
69
70        // Set default values
71        this->setFont("Monofur");
72        this->setTextSize(0.05f);
73        this->setNavMarkerSize(0.05f);
74        this->setAimMarkerSize(0.02f);
75
76        this->setDetectionLimit(10000.0f);
77        this->currentMunitionSpeed_ = 2500.0f;
78
79        /*Pawn* ship = orxonox_cast<Pawn*>(this->getOwner());
80        if(ship != NULL)
81            this->ship_ = ship;*/
82    }
83
84    HUDNavigation::~HUDNavigation()
85    {
86        if (this->isInitialized())
87        {
88            for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end();)
89            removeObject((it++)->first);
90        }
91        this->sortedObjectList_.clear();
92    }
93
94    void HUDNavigation::setConfigValues()
95    {
96        SetConfigValue(markerLimit_, 3);
97        SetConfigValue(showDistance_, false);
98    }
99
100    void HUDNavigation::XMLPort(Element& xmlelement, XMLPort::Mode mode)
101    {
102        SUPER(HUDNavigation, XMLPort, xmlelement, mode);
103
104        XMLPortParam(HUDNavigation, "font", setFont, getFont, xmlelement, mode);
105        XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize, xmlelement, mode);
106        XMLPortParam(HUDNavigation, "navMarkerSize", setNavMarkerSize, getNavMarkerSize, xmlelement, mode);
107        XMLPortParam(HUDNavigation, "detectionLimit", setDetectionLimit, getDetectionLimit, xmlelement, mode);
108        XMLPortParam(HUDNavigation, "aimMarkerSize", setAimMarkerSize, getAimMarkerSize, xmlelement, mode);
109    }
110
111    void HUDNavigation::setFont(const std::string& font)
112    {
113        const Ogre::ResourcePtr& fontPtr = Ogre::FontManager::getSingleton().getByName(font);
114        if (fontPtr.isNull())
115        {
116            orxout(internal_warning) << "HUDNavigation: Font '" << font << "' not found" << endl;
117            return;
118        }
119        this->fontName_ = font;
120        for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end(); ++it)
121        {
122            if (it->second.text_ != NULL)
123            it->second.text_->setFontName(this->fontName_);
124        }
125    }
126
127    const std::string& HUDNavigation::getFont() const
128    {
129        return this->fontName_;
130    }
131
132    void HUDNavigation::setTextSize(float size)
133    {
134        if (size <= 0.0f)
135        {
136            orxout(internal_warning) << "HUDNavigation: Negative font size not allowed" << endl;
137            return;
138        }
139        this->textSize_ = size;
140        for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it!=this->activeObjectList_.end(); ++it)
141        {
142            if (it->second.text_)
143            it->second.text_->setCharHeight(size);
144        }
145    }
146
147    float HUDNavigation::getTextSize() const
148    {
149        return this->textSize_;
150    }
151
152    float HUDNavigation::getArrowSizeX(int dist) const
153    {
154        if (dist < 600)
155        dist = 600;
156        return this->getActualSize().x * 900 * this->navMarkerSize_ / dist;
157    }
158
159    float HUDNavigation::getArrowSizeY(int dist) const
160    {
161        if (dist < 600)
162        dist = 600;
163        return this->getActualSize().y * 900 * this->navMarkerSize_ / dist;
164    }
165
166    void HUDNavigation::tick(float dt)
167    {
168        SUPER(HUDNavigation, tick, dt);
169
170        Camera* cam = CameraManager::getInstance().getActiveCamera();
171        if (cam == NULL)
172        return;
173        const Matrix4& camTransform = cam->getOgreCamera()->getProjectionMatrix() * cam->getOgreCamera()->getViewMatrix();
174
175        for (std::list<std::pair<RadarViewable*, unsigned int> >::iterator listIt = this->sortedObjectList_.begin(); listIt != this->sortedObjectList_.end(); ++listIt)
176        listIt->second = (int)((listIt->first->getRVWorldPosition() - HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition()).length() + 0.5f);
177
178        this->sortedObjectList_.sort(compareDistance);
179
180        unsigned int markerCount = 0;
181        bool closeEnough = false; // only display objects that are close enough to be relevant for the player
182
183        for (std::list<std::pair<RadarViewable*, unsigned int> >::iterator listIt = this->sortedObjectList_.begin(); listIt != this->sortedObjectList_.end(); ++markerCount, ++listIt)
184        {
185            std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.find(listIt->first);
186            closeEnough = listIt->second < this->detectionLimit_;
187            // display radarviewables on HUD if the marker limit and max-distance is not exceeded
188            if (markerCount < this->markerLimit_ && (closeEnough || this->detectionLimit_ < 0))
189            {
190                // Get Distance to HumanController and save it in the TextAreaOverlayElement.
191                int dist = listIt->second;
192                float textLength = 0.0f;
193
194                if (this->showDistance_)
195                {
196                    //display distance next to cursor
197                    it->second.text_->setCaption(multi_cast<std::string>(dist));
198                    textLength = multi_cast<std::string>(dist).size() * it->second.text_->getCharHeight() * 0.3f;
199                }
200                else
201                {
202                    //display name next to cursor
203                    it->second.text_->setCaption(it->first->getRadarName());
204                    textLength = it->first->getRadarName().size() * it->second.text_->getCharHeight() * 0.3f;
205                }
206
207                // Transform to screen coordinates
208                Vector3 pos = camTransform * it->first->getRVWorldPosition();
209
210                bool outOfView = true;
211                if (pos.z > 1.0)
212                {
213                    // z > 1.0 means that the object is behind the camera
214                    outOfView = true;
215                    // we have to switch all coordinates (if you don't know why,
216                    // try linear algebra lectures, because I can't explain..)
217                    pos.x = -pos.x;
218                    pos.y = -pos.y;
219                }
220                else
221                outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0;
222
223                if (outOfView)
224                {
225                    // Object is not in view
226
227                    // Change material only if outOfView changed
228                    if (!it->second.wasOutOfView_)
229                    {
230                        it->second.panel_->setMaterialName(TextureGenerator::getMaterialName("arrows.png", it->first->getRadarObjectColour()));
231                        it->second.wasOutOfView_ = true;
232                        it->second.target_->hide();
233                    }
234
235                    //float xDistScale = this->getActualSize().x * 1000.0f * this->navMarkerSize_ / dist;
236                    //float yDistScale = this->getActualSize().y * 1000.0f * this->navMarkerSize_ / dist;
237
238                    // Adjust Arrowsize according to distance
239                    it->second.panel_->setDimensions(getArrowSizeX(dist), getArrowSizeY(dist));
240
241                    // Switch between top, bottom, left and right position of the arrow at the screen border
242                    if (pos.x < pos.y)
243                    {
244                        if (pos.y > -pos.x)
245                        {
246                            // Top
247                            float position = pos.x / pos.y + 1.0f;
248                            it->second.panel_->setPosition((position - it->second.panel_->getWidth()) * 0.5f, 0.0f);
249                            it->second.panel_->setUV(0.5f, 0.0f, 1.0f, 0.5f);
250                            it->second.text_->setLeft((position - textLength) * 0.5f);
251                            it->second.text_->setTop(it->second.panel_->getHeight());
252                        }
253                        else
254                        {
255                            // Left
256                            float position = pos.y / pos.x + 1.0f;
257                            it->second.panel_->setPosition(0.0f, (position - it->second.panel_->getWidth()) * 0.5f);
258                            it->second.panel_->setUV(0.0f, 0.0f, 0.5f, 0.5f);
259                            it->second.text_->setLeft(it->second.panel_->getWidth() + 0.01f);
260                            it->second.text_->setTop((position - it->second.text_->getCharHeight()) * 0.5f);
261                        }
262                    }
263                    else
264                    {
265                        if (pos.y < -pos.x)
266                        {
267                            // Bottom
268                            float position = -pos.x / pos.y + 1.0f;
269                            it->second.panel_->setPosition((position - it->second.panel_->getWidth()) * 0.5f, 1.0f - it->second.panel_->getHeight());
270                            it->second.panel_->setUV(0.0f, 0.5f, 0.5f, 1.0f );
271                            it->second.text_->setLeft((position - textLength) * 0.5f);
272                            it->second.text_->setTop(1.0f - it->second.panel_->getHeight() - it->second.text_->getCharHeight());
273                        }
274                        else
275                        {
276                            // Right
277                            float position = -pos.y / pos.x + 1.0f;
278                            it->second.panel_->setPosition(1.0f - it->second.panel_->getWidth(), (position - it->second.panel_->getHeight()) * 0.5f);
279                            it->second.panel_->setUV(0.5f, 0.5f, 1.0f, 1.0f);
280                            it->second.text_->setLeft(1.0f - it->second.panel_->getWidth() - textLength - 0.01f);
281                            it->second.text_->setTop((position - it->second.text_->getCharHeight()) * 0.5f);
282                        }
283                    }
284                }
285                else
286                {
287                    // Object is in view
288
289                    // Change material only if outOfView changed
290                    if (it->second.wasOutOfView_)
291                    {
292                        //it->second.panel_->setMaterialName("Orxonox/NavTDC");
293                        it->second.panel_->setMaterialName(TextureGenerator::getMaterialName("tdc.png", it->first->getRadarObjectColour()));
294                        it->second.panel_->setDimensions(this->navMarkerSize_ * this->getActualSize().x, this->navMarkerSize_ * this->getActualSize().y);
295                        it->second.target_->setDimensions(aimMarkerSize_ * this->getActualSize().x, this->aimMarkerSize_ * this->getActualSize().y);
296                        it->second.wasOutOfView_ = false;
297                    }
298
299                    // Position marker
300                    it->second.panel_->setUV(0.0f, 0.0f, 1.0f, 1.0f);
301                    it->second.panel_->setLeft((pos.x + 1.0f - it->second.panel_->getWidth()) * 0.5f);
302                    it->second.panel_->setTop((-pos.y + 1.0f - it->second.panel_->getHeight()) * 0.5f);
303
304                    // Position text
305                    it->second.text_->setLeft((pos.x + 1.0f + it->second.panel_->getWidth()) * 0.5f);
306                    it->second.text_->setTop((-pos.y + 1.0f + it->second.panel_->getHeight()) * 0.5f);
307
308                    // Target marker
309                    Vector3* targetPos = this->toAimPosition(it->first);
310                    Vector3 screenPos = camTransform * *targetPos;
311                    // Check if the target marker is in view too
312                    if(screenPos.z > 1 || screenPos.x < -1.0 || screenPos.x > 1.0
313                            || screenPos.y < -1.0 || screenPos.y > 1.0)
314                    {
315                        it->second.target_->hide();
316                    }
317                    else
318                    {
319                        it->second.target_->setLeft((screenPos.x + 1.0f - it->second.target_->getWidth()) * 0.5f);
320                        it->second.target_->setTop((-screenPos.y + 1.0f - it->second.target_->getHeight()) * 0.5f);
321                        it->second.target_->show();
322                    }
323
324                    delete targetPos;
325                }
326
327                // Make sure the overlays are shown
328                it->second.panel_->show();
329                it->second.text_->show();
330            }
331            else // do not display on HUD
332
333            {
334                it->second.panel_->hide();
335                it->second.text_->hide();
336                it->second.target_->hide();
337            }
338        }
339    }
340
341    /** Overridden method of OrxonoxOverlay.
342     @details
343     Usually the entire overlay scales with scale().
344     Here we obviously have to adjust this.
345     */
346    void HUDNavigation::sizeChanged()
347    {
348        // Use size to compensate for aspect ratio if enabled.
349        float xScale = this->getActualSize().x;
350        float yScale = this->getActualSize().y;
351
352        for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end(); ++it)
353        {
354            if (it->second.panel_ != NULL)
355                it->second.panel_->setDimensions(this->navMarkerSize_ * xScale, this->navMarkerSize_ * yScale);
356            if (it->second.text_ != NULL)
357                it->second.text_->setCharHeight(it->second.text_->getCharHeight() * yScale);
358            if (it->second.target_ != NULL)
359                it->second.target_->setDimensions(this->aimMarkerSize_ * xScale, this->aimMarkerSize_ * yScale);
360        }
361    }
362
363    void HUDNavigation::addObject(RadarViewable* object)
364    {
365        if (showObject(object) == false)
366        return;
367
368        if (this->activeObjectList_.size() >= this->markerLimit_)
369        if (object == NULL)
370        return;
371
372        // Object hasn't been added yet (we know that)
373        assert(this->activeObjectList_.find(object) == this->activeObjectList_.end());
374
375        // Scales used for dimensions and text size
376        float xScale = this->getActualSize().x;
377        float yScale = this->getActualSize().y;
378
379        // Create everything needed to display the object on the radar and add it to the map
380
381        // Create arrow/marker
382        Ogre::PanelOverlayElement* panel = static_cast<Ogre::PanelOverlayElement*>( Ogre::OverlayManager::getSingleton()
383                .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberString()));
384        //panel->setMaterialName("Orxonox/NavTDC");
385        panel->setMaterialName(TextureGenerator::getMaterialName("tdc.png", object->getRadarObjectColour()));
386        panel->setDimensions(this->navMarkerSize_ * xScale, this->navMarkerSize_ * yScale);
387        //panel->setColour(object->getRadarObjectColour());
388
389        // Create target marker
390        Ogre::PanelOverlayElement* target = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
391                .createOverlayElement("Panel", "HUDNavigation_targetMarker_" + getUniqueNumberString()));
392        target->setMaterialName(TextureGenerator::getMaterialName("target.png", object->getRadarObjectColour()));
393        target->setDimensions(this->aimMarkerSize_ * xScale, this->aimMarkerSize_ * yScale);
394
395        // Create text
396        Ogre::TextAreaOverlayElement* text = static_cast<Ogre::TextAreaOverlayElement*>( Ogre::OverlayManager::getSingleton()
397                .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberString()));
398        text->setFontName(this->fontName_);
399        text->setCharHeight(text->getCharHeight() * yScale);
400        text->setColour(object->getRadarObjectColour());
401
402        panel->hide();
403        target->hide();
404        text->hide();
405
406        ObjectInfo tempStruct =
407        {   panel, target, text, false /*, TODO: initialize wasOutOfView_ */};
408        this->activeObjectList_[object] = tempStruct;
409
410        this->background_->addChild(panel);
411        this->background_->addChild(target);
412        this->background_->addChild(text);
413
414        this->sortedObjectList_.push_front(std::make_pair(object, (unsigned int)0));
415    }
416
417    void HUDNavigation::removeObject(RadarViewable* viewable)
418    {
419        std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.find(viewable);
420
421        if (this->activeObjectList_.find(viewable) != this->activeObjectList_.end())
422        {
423            // Detach overlays
424            this->background_->removeChild(it->second.panel_->getName());
425            this->background_->removeChild(it->second.target_->getName());
426            this->background_->removeChild(it->second.text_->getName());
427            // Properly destroy the overlay elements (do not use delete!)
428            Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.panel_);
429            Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.target_);
430            Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.text_);
431            // Remove from the list
432            this->activeObjectList_.erase(viewable);
433        }
434
435        for (std::list<std::pair<RadarViewable*, unsigned int> >::iterator listIt = this->sortedObjectList_.begin(); listIt != this->sortedObjectList_.end(); ++listIt)
436        {
437            if ((listIt->first) == viewable)
438            {
439                this->sortedObjectList_.erase(listIt);
440                break;
441            }
442        }
443    }
444
445    void HUDNavigation::objectChanged(RadarViewable* viewable)
446    {
447        // TODO: niceification neccessary ;)
448        removeObject(viewable);
449        addObject(viewable);
450    }
451
452    bool HUDNavigation::showObject(RadarViewable* rv)
453    {
454        if (rv == orxonox_cast<RadarViewable*>(this->getOwner()))
455        return false;
456        assert(rv->getWorldEntity());
457        if (rv->getWorldEntity()->isVisible() == false || rv->getRadarVisibility() == false)
458        return false;
459        return true;
460    }
461
462    void HUDNavigation::changedOwner()
463    {
464        const std::set<RadarViewable*>& respawnObjects = this->getOwner()->getScene()->getRadar()->getRadarObjects();
465        for (std::set<RadarViewable*>::const_iterator it = respawnObjects.begin(); it != respawnObjects.end(); ++it)
466        {
467            if (!(*it)->isHumanShip_)
468            this->addObject(*it);
469        }
470    }
471
472    Vector3* HUDNavigation::toAimPosition(RadarViewable* target) const
473    {
474        Vector3 wePosition = HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition();
475        Vector3 targetPosition = target->getRVWorldPosition();
476        Vector3 targetSpeed = target->getRVOrientedVelocity();
477
478        // munSpeed*time = lengthBetween(wePosition, targetPosition + targetSpeed*time)
479        // from this we extract:
480        float a = pow(targetSpeed.length(),2) - pow(this->currentMunitionSpeed_,2);
481        float b = 2*((targetPosition.x - wePosition.x)*targetSpeed.x
482                    +(targetPosition.y - wePosition.y)*targetSpeed.y
483                    +(targetPosition.z - wePosition.z)*targetSpeed.z);
484        float c = pow((targetPosition-wePosition).length(),2);
485
486        // calculate smallest time solution, in case it exists
487        if(pow(b,2) - 4*a*c < 0)
488            return NULL;
489        float time = (-b - sqrt(pow(b,2) - 4*a*c))/(2*a);
490        if(time < 0)
491            time = (-b + sqrt(pow(b,2) - 4*a*c))/(2*a);
492        if(time < 0)
493            return NULL;
494        Vector3* result = new Vector3(targetPosition + targetSpeed * time);
495        return result;
496    }
497}
Note: See TracBrowser for help on using the repository browser.