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
RevLine 
[1393]1/*
[1394]2*   ORXONOX - the hotnavText_ 3D action shooter ever to exist
[1393]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
[1410]28#include "OrxonoxStableHeaders.h"
29#include "Navigation.h"
30
[1393]31#include <OgreOverlayManager.h>
[1395]32#include <OgreStringConverter.h>
[1410]33
34#include "GraphicsEngine.h"
35// TODO: remove the SpaceShip and CameraHandler dependencies
[1393]36#include "objects/SpaceShip.h"
[1399]37#include "objects/CameraHandler.h"
[1410]38#include "RadarObject.h"
39#include "RadarOverlayElement.h"
[1393]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(){
[1410]59        om = &OverlayManager::getSingleton();
60        navCam_ = NULL;
[1394]61        // create nav text
62        navText_ = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "navText"));
63        navText_->show();
[1399]64        navText_->setMetricsMode(Ogre::GMM_PIXELS);
65        navText_->setDimensions(0.001, 0.001);
[1394]66        navText_->setPosition(0.02, 0.02);
67        navText_->setFontName("Console");
[1399]68        navText_->setCharHeight(20);
[1396]69        navText_->setCaption("");
[1394]70        container_->addChild(navText_);
71
72
[1393]73        // create nav marker ...
74        navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker"));
75        navMarker_->setMetricsMode(GMM_PIXELS);
76        navMarker_->hide();
[1399]77        navText_->hide();
[1393]78        container_->addChild(navMarker_);
[1410]79    }
[1393]80
[1410]81    void Navigation::update(){
[1393]82        if(focus_ == NULL) return;
[1400]83        navCamPos_ = SpaceShip::getLocalShip()->getPosition();
84        currentDir_ = SpaceShip::getLocalShip()->getDir();
[1410]85        currentOrth_ = SpaceShip::getLocalShip()->getOrth();
[1400]86
[1393]87        windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
88        windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
[1399]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
[1400]97        if(navCam_ == NULL) navCam_ = SpaceShip::getLocalShip()->getCamera()->cam_;
[1399]98        Vector3 pos = focus_->pos_;
99        // transform to screen coordinates
[1400]100        pos = navCam_->getProjectionMatrix()*navCam_->getViewMatrix()*pos;
[1399]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_;
[1411]105        int xFromCenter = xPos-windowW_/2;
106        int yFromCenter = yPos-windowH_/2;
[1399]107        // is object in view?
[1411]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;
[1399]111        bool outOfView = (xPosRel<0 || xPosRel>1 || yPosRel<0 || yPosRel>1);
112        // if object is behind us, it is out of view anyway:
[1411]113        if(!outOfView && radius>3.14/2) outOfView = true;
[1399]114
115        if(outOfView){
[1411]116            // object is not in view
[1399]117            navMarker_->setMaterialName("Orxonox/NavArrows");
118            navMarker_->setDimensions(16,16);
[1411]119            float phiUpperCorner = atan((float)(windowW_)/(float)(windowH_));
120            // from the angle we find out on which edge to draw the marker
[1399]121            // and which of the four arrows to take
[1411]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);
[1399]129                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
130                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
131                    navText_->setTop(navMarker_->getHeight());
132                }
[1411]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);
[1399]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 {
[1411]151                    COUT(3) << "arrow right\n";
152                    navMarker_->setPosition(windowW_-16, tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
[1399]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                }
[1393]157            }
[1411]158            if(isAbove && !isRight){
159                // top left quadrant
160                if(phiNav<phiUpperCorner){
161                    COUT(3) << "arrow up\n";
[1400]162                    navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0);
[1399]163                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
164                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
165                    navText_->setTop(navMarker_->getHeight());
166                }
[1411]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";
[1400]179                    navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16);
[1399]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 {
[1411]185                    COUT(3) << "arrow left\n";
[1400]186                    navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
[1399]187                    navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
188                    navText_->setLeft(navMarker_->getWidth());
189                    navText_->setTop(navMarker_->getTop()+navMarker_->getHeight());
190                }
[1393]191            }
192        }
193        else{
[1411]194            // object is in view
[1399]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);
[1393]200        }
[1410]201    }
[1394]202
[1393]203    void Navigation::cycleFocus(){
[1410]204        if(focus_ == NULL){
[1393]205            focus_ = HUD::getSingleton().getFirstRadarObject();
[1410]206        }
[1393]207        else{
208            focus_->panel_->setMaterialName("Orxonox/RedDot");
[1399]209            if(focus_ != NULL) focus_ = focus_->next;
[1393]210        }
211
212        if(focus_ == NULL){
213            navMarker_->hide();
[1399]214            navText_->hide();
[1393]215        }
216        else{
217            navMarker_->show();
[1399]218            navText_->show();
[1393]219            focus_->panel_->setMaterialName("Orxonox/WhiteDot");
220        }
[1410]221    }
[1393]222
[1410]223    float Navigation::getDist2Focus(){
224        if(focus_ == NULL) return(0.0);
225        return((focus_->pos_-SpaceShip::getLocalShip()->getPosition()).length());
226    }
[1393]227}
Note: See TracBrowser for help on using the repository browser.