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, 12 years ago

aimMarkerSize Problem

  • Property svn:eol-style set to native
File size: 20.9 KB
RevLine 
[1505]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
[1454]4 *
[1505]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 *
[1454]22 *   Author:
23 *      Felix Schulthess
24 *   Co-authors:
[1590]25 *      Reto Grieder
[7801]26 *      Oliver Scheuss
[9016]27 *      Matthias Spalinger
[1454]28 *
29 */
[1393]30
[1601]31#include "HUDNavigation.h"
[1410]32
[6417]33#include <OgreCamera.h>
[7163]34#include <OgreFontManager.h>
[1393]35#include <OgreOverlayManager.h>
[1614]36#include <OgreTextAreaOverlayElement.h>
37#include <OgrePanelOverlayElement.h>
[1410]38
[1614]39#include "util/Math.h"
[1616]40#include "util/Convert.h"
41#include "core/CoreIncludes.h"
42#include "core/XMLPort.h"
[6417]43#include "CameraManager.h"
[5929]44#include "Scene.h"
[5735]45#include "Radar.h"
[6417]46#include "graphics/Camera.h"
47#include "controllers/HumanController.h"
48#include "worldentities/pawns/Pawn.h"
[7163]49#include "worldentities/WorldEntity.h"
50#include "core/ConfigValueIncludes.h"
51#include "tools/TextureGenerator.h"
52// #include <boost/bind/bind_template.hpp>
[1393]53
[7163]54
[1393]55namespace orxonox
56{
[9421]57    static bool compareDistance(std::pair<RadarViewable*, unsigned int> a,
58            std::pair<RadarViewable*, unsigned int> b)
[9348]59    {
60        return a.second < b.second;
61    }
62    CreateFactory ( HUDNavigation );
[2087]63
[9421]64    HUDNavigation::HUDNavigation(BaseObject* creator) :
65        OrxonoxOverlay(creator)
[9348]66    {
[9421]67        RegisterObject(HUDNavigation)
68;        this->setConfigValues();
[2087]69
[9348]70        // Set default values
71        this->setFont("Monofur");
72        this->setTextSize(0.05f);
73        this->setNavMarkerSize(0.05f);
[9429]74        this->setAimMarkerSize(0.02f);
75
[9348]76        this->setDetectionLimit(10000.0f);
[9421]77        this->currentMunitionSpeed_ = 2500.0f;
78
[9429]79        /*Pawn* ship = orxonox_cast<Pawn*>(this->getOwner());
[9421]80        if(ship != NULL)
[9429]81            this->ship_ = ship;*/
[9348]82    }
[2087]83
[9348]84    HUDNavigation::~HUDNavigation()
85    {
86        if (this->isInitialized())
87        {
88            for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end();)
[9421]89            removeObject((it++)->first);
[9348]90        }
91        this->sortedObjectList_.clear();
92    }
[2087]93
[9348]94    void HUDNavigation::setConfigValues()
95    {
96        SetConfigValue(markerLimit_, 3);
97        SetConfigValue(showDistance_, false);
98    }
[2087]99
[9348]100    void HUDNavigation::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[7163]101    {
[9348]102        SUPER(HUDNavigation, XMLPort, xmlelement, mode);
[2087]103
[9421]104        XMLPortParam(HUDNavigation, "font", setFont, getFont, xmlelement, mode);
105        XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize, xmlelement, mode);
106        XMLPortParam(HUDNavigation, "navMarkerSize", setNavMarkerSize, getNavMarkerSize, xmlelement, mode);
[9348]107        XMLPortParam(HUDNavigation, "detectionLimit", setDetectionLimit, getDetectionLimit, xmlelement, mode);
[9429]108        XMLPortParam(HUDNavigation, "aimMarkerSize", setAimMarkerSize, getAimMarkerSize, xmlelement, mode);
[1393]109    }
110
[9348]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)
[9421]123            it->second.text_->setFontName(this->fontName_);
[9348]124        }
125    }
[1564]126
[9348]127    const std::string& HUDNavigation::getFont() const
[1590]128    {
[9348]129        return this->fontName_;
[1590]130    }
[9348]131
132    void HUDNavigation::setTextSize(float size)
[1590]133    {
[9348]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_)
[9421]143            it->second.text_->setCharHeight(size);
[9348]144        }
[1590]145    }
146
[9348]147    float HUDNavigation::getTextSize() const
[1590]148    {
[9348]149        return this->textSize_;
[1590]150    }
[9348]151
152    float HUDNavigation::getArrowSizeX(int dist) const
[7163]153    {
[9348]154        if (dist < 600)
[9421]155        dist = 600;
[9348]156        return this->getActualSize().x * 900 * this->navMarkerSize_ / dist;
[7163]157    }
[1590]158
[9348]159    float HUDNavigation::getArrowSizeY(int dist) const
[1590]160    {
[9348]161        if (dist < 600)
[9421]162        dist = 600;
[9348]163        return this->getActualSize().y * 900 * this->navMarkerSize_ / dist;
[1590]164    }
165
[9348]166    void HUDNavigation::tick(float dt)
[1590]167    {
[9348]168        SUPER(HUDNavigation, tick, dt);
[1400]169
[9348]170        Camera* cam = CameraManager::getInstance().getActiveCamera();
171        if (cam == NULL)
[9421]172        return;
[9348]173        const Matrix4& camTransform = cam->getOgreCamera()->getProjectionMatrix() * cam->getOgreCamera()->getViewMatrix();
[1399]174
[9348]175        for (std::list<std::pair<RadarViewable*, unsigned int> >::iterator listIt = this->sortedObjectList_.begin(); listIt != this->sortedObjectList_.end(); ++listIt)
[9421]176        listIt->second = (int)((listIt->first->getRVWorldPosition() - HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition()).length() + 0.5f);
[1564]177
[9348]178        this->sortedObjectList_.sort(compareDistance);
[9016]179
[9348]180        unsigned int markerCount = 0;
181        bool closeEnough = false; // only display objects that are close enough to be relevant for the player
[1399]182
[9348]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
[9421]188            if (markerCount < this->markerLimit_ && (closeEnough || this->detectionLimit_ < 0))
[1580]189            {
[9348]190                // Get Distance to HumanController and save it in the TextAreaOverlayElement.
191                int dist = listIt->second;
192                float textLength = 0.0f;
[1590]193
[9348]194                if (this->showDistance_)
[1580]195                {
[9348]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;
[1399]199                }
[9348]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                }
[7163]206
[9348]207                // Transform to screen coordinates
208                Vector3 pos = camTransform * it->first->getRVWorldPosition();
[9016]209
[9348]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
[9421]221                outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0;
[9016]222
[9348]223                if (outOfView)
[7163]224                {
[9348]225                    // Object is not in view
226
227                    // Change material only if outOfView changed
228                    if (!it->second.wasOutOfView_)
[7163]229                    {
[9348]230                        it->second.panel_->setMaterialName(TextureGenerator::getMaterialName("arrows.png", it->first->getRadarObjectColour()));
231                        it->second.wasOutOfView_ = true;
[9421]232                        it->second.target_->hide();
[7163]233                    }
[9348]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                    }
[7163]263                    else
264                    {
[9348]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                        }
[7163]283                    }
284                }
[1580]285                else
286                {
[9348]287                    // Object is in view
[7163]288
[9348]289                    // Change material only if outOfView changed
290                    if (it->second.wasOutOfView_)
[7163]291                    {
[9348]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);
[9429]295                        it->second.target_->setDimensions(aimMarkerSize_ * this->getActualSize().x, this->aimMarkerSize_ * this->getActualSize().y);
[9348]296                        it->second.wasOutOfView_ = false;
[7163]297                    }
[9348]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
[9429]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
[9421]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);
[9429]321                        it->second.target_->show();
[9421]322                    }
323
324                    delete targetPos;
[1411]325                }
[9348]326
327                // Make sure the overlays are shown
328                it->second.panel_->show();
329                it->second.text_->show();
[1411]330            }
[9348]331            else // do not display on HUD
[9421]332
[1580]333            {
[9348]334                it->second.panel_->hide();
335                it->second.text_->hide();
[9429]336                it->second.target_->hide();
[9348]337            }
338        }
339    }
[7163]340
[9348]341    /** Overridden method of OrxonoxOverlay.
[9421]342     @details
343     Usually the entire overlay scales with scale().
344     Here we obviously have to adjust this.
345     */
[9348]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;
[7163]351
[9348]352        for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end(); ++it)
[1580]353        {
[9348]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);
[9421]358            if (it->second.target_ != NULL)
[9429]359                it->second.target_->setDimensions(this->aimMarkerSize_ * xScale, this->aimMarkerSize_ * yScale);
[7163]360        }
361    }
[1566]362
[9348]363    void HUDNavigation::addObject(RadarViewable* object)
[7163]364    {
[9348]365        if (showObject(object) == false)
[9421]366        return;
[7163]367
[9348]368        if (this->activeObjectList_.size() >= this->markerLimit_)
[9421]369        if (object == NULL)
370        return;
[7163]371
[9348]372        // Object hasn't been added yet (we know that)
373        assert(this->activeObjectList_.find(object) == this->activeObjectList_.end());
[7163]374
[9348]375        // Scales used for dimensions and text size
376        float xScale = this->getActualSize().x;
377        float yScale = this->getActualSize().y;
[7163]378
[9348]379        // Create everything needed to display the object on the radar and add it to the map
[7163]380
[9348]381        // Create arrow/marker
382        Ogre::PanelOverlayElement* panel = static_cast<Ogre::PanelOverlayElement*>( Ogre::OverlayManager::getSingleton()
[9421]383                .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberString()));
[9348]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());
[7163]388
[9421]389        // Create target marker
390        Ogre::PanelOverlayElement* target = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
391                .createOverlayElement("Panel", "HUDNavigation_targetMarker_" + getUniqueNumberString()));
[9429]392        target->setMaterialName(TextureGenerator::getMaterialName("target.png", object->getRadarObjectColour()));
393        target->setDimensions(this->aimMarkerSize_ * xScale, this->aimMarkerSize_ * yScale);
[9421]394
395        // Create text
[9348]396        Ogre::TextAreaOverlayElement* text = static_cast<Ogre::TextAreaOverlayElement*>( Ogre::OverlayManager::getSingleton()
[9421]397                .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberString()));
[9348]398        text->setFontName(this->fontName_);
399        text->setCharHeight(text->getCharHeight() * yScale);
400        text->setColour(object->getRadarObjectColour());
[7163]401
[9348]402        panel->hide();
[9421]403        target->hide();
[9348]404        text->hide();
[7163]405
[9421]406        ObjectInfo tempStruct =
407        {   panel, target, text, false /*, TODO: initialize wasOutOfView_ */};
[9348]408        this->activeObjectList_[object] = tempStruct;
[7163]409
[9348]410        this->background_->addChild(panel);
[9421]411        this->background_->addChild(target);
[9348]412        this->background_->addChild(text);
[7163]413
[9348]414        this->sortedObjectList_.push_front(std::make_pair(object, (unsigned int)0));
415    }
[7163]416
[9348]417    void HUDNavigation::removeObject(RadarViewable* viewable)
[1580]418    {
[9348]419        std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.find(viewable);
[7163]420
[9348]421        if (this->activeObjectList_.find(viewable) != this->activeObjectList_.end())
422        {
423            // Detach overlays
424            this->background_->removeChild(it->second.panel_->getName());
[9421]425            this->background_->removeChild(it->second.target_->getName());
[9348]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_);
[9421]429            Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.target_);
[9348]430            Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.text_);
431            // Remove from the list
432            this->activeObjectList_.erase(viewable);
433        }
[7163]434
[9348]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        }
[1410]443    }
[1590]444
[9348]445    void HUDNavigation::objectChanged(RadarViewable* viewable)
[1590]446    {
[9348]447        // TODO: niceification neccessary ;)
448        removeObject(viewable);
449        addObject(viewable);
450    }
[7163]451
[9348]452    bool HUDNavigation::showObject(RadarViewable* rv)
453    {
454        if (rv == orxonox_cast<RadarViewable*>(this->getOwner()))
[9421]455        return false;
[9348]456        assert(rv->getWorldEntity());
457        if (rv->getWorldEntity()->isVisible() == false || rv->getRadarVisibility() == false)
[9421]458        return false;
[9348]459        return true;
[1590]460    }
[7163]461
[9348]462    void HUDNavigation::changedOwner()
[7163]463    {
[9348]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_)
[9421]468            this->addObject(*it);
[9348]469        }
[7163]470    }
[9421]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    }
[7163]497}
Note: See TracBrowser for help on using the repository browser.