Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/hud/Navigation.cc @ 1399

Last change on this file since 1399 was 1399, checked in by FelixSchulthess, 16 years ago

enhanced navigation with a target designation cursor

File size: 7.4 KB
Line 
1/*
2*   ORXONOX - the hotnavText_ 3D action shooter ever to exist
3*
4*
5*   License notice:
6*
7*   This program is free software; you can redistribute it and/or
8*   modify it under the terms of the GNU General Public License
9*   as published by the Free Software Foundation; either version 2
10*   of the License, or (at your option) any later version.
11*
12*   This program is distributed in the hope that it will be useful,
13*   but WITHOUT ANY WARRANTY; without even the implied warranty of
14*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*   GNU General Public License for more details.
16*
17*   You should have received a copy of the GNU General Public License
18*   along with this program; if not, write to the Free Software
19*   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20*
21*   Author:
22*      Felix Schulthess
23*   Co-authors:
24*      ...
25*
26*/
27
28#include <OgreOverlayManager.h>
29#include <OgrePanelOverlayElement.h>
30#include <OgreTextAreaOverlayElement.h>
31#include <OgreStringConverter.h>
32#include <OgreMatrix4.h>
33#include <GraphicsEngine.h>
34#include "core/Iterator.h"
35#include "objects/SpaceShip.h"
36#include "objects/CameraHandler.h"
37#include "HUD.h"
38#include "Navigation.h"
39
40namespace orxonox
41{
42    using namespace Ogre;
43
44    Navigation::Navigation(OverlayContainer* container){
45        container_ = container;
46        focus_ = NULL;
47        init();
48    }
49
50    Navigation::Navigation(OverlayContainer* container, RadarObject* focus){
51        container_ = container;
52        focus_ = focus;
53        init();
54    }
55
56    void Navigation::init(){
57                om = &OverlayManager::getSingleton();
58//              for (Iterator<Model> it = ObjectList<Model>::begin(); it; ++it)
59//              {
60//            if(it->getPosition() == Vector(2000.0, 1000.0, 1000.0)){
61//                COUT(3) << "TARGET FOUND\n";
62//
63//
64//        }
65                cam_ = NULL;
66        // create nav text
67        navText_ = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "navText"));
68        navText_->show();
69        navText_->setMetricsMode(Ogre::GMM_PIXELS);
70        navText_->setDimensions(0.001, 0.001);
71        navText_->setPosition(0.02, 0.02);
72        navText_->setFontName("Console");
73        navText_->setCharHeight(20);
74        navText_->setCaption("");
75        container_->addChild(navText_);
76
77
78        // create nav marker ...
79        navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker"));
80        navMarker_->setMetricsMode(GMM_PIXELS);
81        navMarker_->hide();
82        navText_->hide();
83        container_->addChild(navMarker_);
84        }
85
86        void Navigation::update(){
87        if(focus_ == NULL) return;
88        shipPos_ = SpaceShip::getLocalShip()->getPosition();
89        windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
90        windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
91        updateMarker();
92    }
93
94    void Navigation::updateMarker(){
95        // set text
96        int dist = (float)(getDist2Focus()/100);
97        navText_->setCaption(Ogre::StringConverter::toString(dist));
98
99        if(cam_ == NULL) cam_ = SpaceShip::getLocalShip()->getCamera()->cam_;
100        Vector3 pos = focus_->pos_;
101        // transform to screen coordinates
102        pos = cam_->getProjectionMatrix()*cam_->getViewMatrix()*pos;
103        float xPosRel = 0.5*pos.x+0.5;
104        float yPosRel = 1-(0.5*pos.y+0.5);
105        int xPos = xPosRel*windowW_;
106        int yPos = yPosRel*windowH_;
107        // is object in view?
108        bool outOfView = (xPosRel<0 || xPosRel>1 || yPosRel<0 || yPosRel>1);
109        // if object is behind us, it is out of view anyway:
110        if(!outOfView && focus_->radius_>3.14/2) outOfView = true;
111
112        if(outOfView){
113            // NO!
114            navMarker_->setMaterialName("Orxonox/NavArrows");
115            navMarker_->setDimensions(16,16);
116            float phiUpRight = atan((float)(windowW_)/(float)(windowH_));
117            // from the angle we find out where to draw the marker
118            // and which of the four arrows to take
119            float phi = focus_->phi_;
120            if(focus_->right_){
121                if(phi<phiUpRight){
122                    // arrow up
123                    navMarker_->setPosition(tan(phi)*windowH_/2+windowW_/2, 0);
124                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
125                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
126                    navText_->setTop(navMarker_->getHeight());
127                }
128                else if(phi>3.14-phiUpRight){
129                    // arrow down
130                    navMarker_->setPosition(-tan(phi)*windowH_/2+windowW_/2, windowH_-16);
131                    navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
132                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
133                    navText_->setTop(navMarker_->getTop()-navMarker_->getHeight());
134                }
135                else {
136                    // arrow right
137                    navMarker_->setPosition(windowW_-16, -tan((3.14-2*phi)/2)*windowW_/2+windowH_/2);
138                    navMarker_->setUV(0.5, 0.5, 1.0, 1.0);
139                    navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth());
140                    navText_->setTop(navMarker_->getTop()+navMarker_->getHeight());
141                }
142            }
143            else{
144                if(phi<phiUpRight) {
145                    // arrow up
146                    navMarker_->setPosition(-tan(phi)*windowH_/2+windowW_/2, 0);
147                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
148                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
149                    navText_->setTop(navMarker_->getHeight());
150                }
151                else if(phi>3.14-phiUpRight) {
152                    // arrow down
153                    navMarker_->setPosition(tan(phi)*windowH_/2+windowW_/2, windowH_-16);
154                    navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
155                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
156                    navText_->setTop(navMarker_->getTop()-navMarker_->getHeight());
157                }
158                else {
159                    // arrow left
160                    navMarker_->setPosition(0, -tan((3.14-2*phi)/2)*windowW_/2+windowH_/2);
161                    navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
162                    navText_->setLeft(navMarker_->getWidth());
163                    navText_->setTop(navMarker_->getTop()+navMarker_->getHeight());
164                }
165            }
166        }
167        else{
168            // YES!
169            navMarker_->setMaterialName("Orxonox/NavTDC");
170            navMarker_->setDimensions(24,24);
171            navMarker_->setUV(0.0,0.0,1.0,1.0);
172            navMarker_->setPosition(xPos-navMarker_->getWidth()/2, yPos-navMarker_->getHeight()/2);
173            navText_->setPosition(xPos+navMarker_->getWidth()/2, yPos+navMarker_->getHeight()/2);
174        }
175        }
176
177    void Navigation::cycleFocus(){
178            if(focus_ == NULL){
179            focus_ = HUD::getSingleton().getFirstRadarObject();
180            }
181        else{
182            focus_->panel_->setMaterialName("Orxonox/RedDot");
183            if(focus_ != NULL) focus_ = focus_->next;
184        }
185
186        if(focus_ == NULL){
187            navMarker_->hide();
188            navText_->hide();
189        }
190        else{
191            navMarker_->show();
192            navText_->show();
193            focus_->panel_->setMaterialName("Orxonox/WhiteDot");
194        }
195        }
196
197        float Navigation::getDist2Focus(){
198            if(focus_ == NULL) return(0.0);
199            return((focus_->pos_-shipPos_).length());
200        }
201}
Note: See TracBrowser for help on using the repository browser.