Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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