Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/orxonox/hud/Navigation.cc @ 1425

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

rewritten navigation algebra… very ugly, and not optimized

File size: 9.0 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 "OrxonoxStableHeaders.h"
29#include "Navigation.h"
30
31#include <OgreOverlayManager.h>
32#include <OgreStringConverter.h>
33
34#include "GraphicsEngine.h"
35// TODO: remove the SpaceShip and CameraHandler dependencies
36#include "objects/SpaceShip.h"
37#include "objects/CameraHandler.h"
38#include "RadarObject.h"
39#include "RadarOverlayElement.h"
40#include "HUD.h"
41
42namespace orxonox
43{
44    using namespace Ogre;
45
46    Navigation::Navigation(OverlayContainer* container){
47        container_ = container;
48        focus_ = NULL;
49        init();
50    }
51
52    Navigation::Navigation(OverlayContainer* container, RadarObject* focus){
53        container_ = container;
54        focus_ = focus;
55        init();
56    }
57
58    void Navigation::init(){
59        om = &OverlayManager::getSingleton();
60        navCam_ = NULL;
61        // create nav text
62        navText_ = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "navText"));
63        navText_->show();
64        navText_->setMetricsMode(Ogre::GMM_PIXELS);
65        navText_->setDimensions(0.001, 0.001);
66        navText_->setPosition(0.02, 0.02);
67        navText_->setFontName("Console");
68        navText_->setCharHeight(20);
69        navText_->setCaption("");
70        container_->addChild(navText_);
71
72
73        // create nav marker ...
74        navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker"));
75        navMarker_->setMetricsMode(GMM_PIXELS);
76        navMarker_->hide();
77        navText_->hide();
78        container_->addChild(navMarker_);
79    }
80
81    void Navigation::update(){
82        if(focus_ == NULL) return;
83        navCamPos_ = SpaceShip::getLocalShip()->getPosition();
84        currentDir_ = SpaceShip::getLocalShip()->getDir();
85        currentOrth_ = SpaceShip::getLocalShip()->getOrth();
86
87        windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
88        windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
89        updateMarker();
90    }
91
92    void Navigation::updateMarker(){
93        // set text
94        int dist = (float)(getDist2Focus()/100);
95        navText_->setCaption(Ogre::StringConverter::toString(dist));
96
97        if(navCam_ == NULL) navCam_ = SpaceShip::getLocalShip()->getCamera()->cam_;
98        Vector3 pos = focus_->pos_;
99        // transform to screen coordinates
100        pos = navCam_->getProjectionMatrix()*navCam_->getViewMatrix()*pos;
101        float xPosRel = 0.5*pos.x+0.5;
102        float yPosRel = 1-(0.5*pos.y+0.5);
103        int xPos = xPosRel*windowW_;
104        int yPos = yPosRel*windowH_;
105        int xFromCenter = xPos-windowW_/2;
106        int yFromCenter = yPos-windowH_/2;
107        // is object in view?
108        float radius = RadarOverlayElement::calcRadius(navCamPos_, currentDir_, currentOrth_, focus_);
109        bool isRight = (currentDir_.crossProduct(currentOrth_)).dotProduct(focus_->pos_ - navCamPos_)>0;
110        bool isAbove = currentOrth_.dotProduct(focus_->pos_ - navCamPos_)>0;
111        bool outOfView = (xPosRel<0 || xPosRel>1 || yPosRel<0 || yPosRel>1);
112        // if object is behind us, it is out of view anyway:
113        if(!outOfView && radius>3.14/2) outOfView = true;
114
115        if(outOfView){
116            // object is not in view
117            navMarker_->setMaterialName("Orxonox/NavArrows");
118            navMarker_->setDimensions(16,16);
119            float phiUpperCorner = atan((float)(windowW_)/(float)(windowH_));
120            // from the angle we find out on which edge to draw the marker
121            // and which of the four arrows to take
122            float phiNav = atan((float) xFromCenter / (float) yFromCenter);
123
124            if(isAbove && isRight){
125                // top right quadrant
126                if(-phiNav<phiUpperCorner){
127                    COUT(3) << "arrow up\n";
128                    navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0);
129                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
130                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
131                    navText_->setTop(navMarker_->getHeight());
132                }
133                else {
134                    COUT(3) << "arrow right\n";
135                    navMarker_->setPosition(windowW_-16, tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
136                    navMarker_->setUV(0.5, 0.5, 1.0, 1.0);
137                    navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth());
138                    navText_->setTop(navMarker_->getTop()+navMarker_->getHeight());
139                }
140            }
141            if(!isAbove && isRight){
142                // bottom right quadrant
143                if(phiNav<phiUpperCorner) {
144                    COUT(3) << "arrow down\n";
145                    navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16);
146                    navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
147                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
148                    navText_->setTop(navMarker_->getTop()-navMarker_->getHeight());
149                }
150                else {
151                    COUT(3) << "arrow right\n";
152                    navMarker_->setPosition(windowW_-16, tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
153                    navMarker_->setUV(0.5, 0.5, 1.0, 1.0);
154                    navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth());
155                    navText_->setTop(navMarker_->getTop()+navMarker_->getHeight());
156                }
157            }
158            if(isAbove && !isRight){
159                // top left quadrant
160                if(phiNav<phiUpperCorner){
161                    COUT(3) << "arrow up\n";
162                    navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0);
163                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
164                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
165                    navText_->setTop(navMarker_->getHeight());
166                }
167                else {
168                    COUT(3) << "arrow left\n";
169                    navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
170                    navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
171                    navText_->setLeft(navMarker_->getWidth());
172                    navText_->setTop(navMarker_->getTop()+navMarker_->getHeight());
173                }
174            }
175            if(!isAbove && !isRight){
176                // bottom left quadrant
177                if(phiNav>-phiUpperCorner) {
178                    COUT(3) << "arrow down\n";
179                    navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16);
180                    navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
181                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
182                    navText_->setTop(navMarker_->getTop()-navMarker_->getHeight());
183                }
184                else {
185                    COUT(3) << "arrow left\n";
186                    navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
187                    navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
188                    navText_->setLeft(navMarker_->getWidth());
189                    navText_->setTop(navMarker_->getTop()+navMarker_->getHeight());
190                }
191            }
192        }
193        else{
194            // object is in view
195            navMarker_->setMaterialName("Orxonox/NavTDC");
196            navMarker_->setDimensions(24,24);
197            navMarker_->setUV(0.0,0.0,1.0,1.0);
198            navMarker_->setPosition(xPos-navMarker_->getWidth()/2, yPos-navMarker_->getHeight()/2);
199            navText_->setPosition(xPos+navMarker_->getWidth()/2, yPos+navMarker_->getHeight()/2);
200        }
201    }
202
203    void Navigation::cycleFocus(){
204        if(focus_ == NULL){
205            focus_ = HUD::getSingleton().getFirstRadarObject();
206        }
207        else{
208            focus_->panel_->setMaterialName("Orxonox/RedDot");
209            if(focus_ != NULL) focus_ = focus_->next;
210        }
211
212        if(focus_ == NULL){
213            navMarker_->hide();
214            navText_->hide();
215        }
216        else{
217            navMarker_->show();
218            navText_->show();
219            focus_->panel_->setMaterialName("Orxonox/WhiteDot");
220        }
221    }
222
223    float Navigation::getDist2Focus(){
224        if(focus_ == NULL) return(0.0);
225        return((focus_->pos_-SpaceShip::getLocalShip()->getPosition()).length());
226    }
227}
Note: See TracBrowser for help on using the repository browser.