Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud3/src/orxonox/hud/RadarOverlayElement.cc @ 1350

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

added a navigation marker

File size: 7.8 KB
RevLine 
[1283]1/*
2*   ORXONOX - the hottest 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*      Yuning Chai
23*   Co-authors:
24*      ...
25*
26*/
27
28#include "RadarOverlayElement.h"
29
30namespace orxonox
31{
[1302]32    using namespace Ogre;
[1283]33
[1350]34    RadarOverlayElement* RadarOverlayElement::instance_s = NULL;
35
[1302]36    RadarOverlayElement::RadarOverlayElement(const String& name):Ogre::PanelOverlayElement(name){
[1350]37        RadarOverlayElement::instance_s = this;
[1302]38    }
[1283]39
[1302]40    RadarOverlayElement::~RadarOverlayElement(){
41    }
[1283]42
[1328]43    void RadarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, Ogre::OverlayContainer* container){
[1339]44        // some initial data
45                om = &Ogre::OverlayManager::getSingleton();
[1314]46        dimRel_ = dimRel;
47        leftRel_ = leftRel;
48        topRel_ = topRel;
[1302]49        container_ = container;
[1339]50        firstRadarObject_ = NULL;
51        lastRadarObject_ = NULL;
[1350]52        focus_ = NULL;
[1283]53
[1350]54        // create nav marker ...
55        navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker"));
56        navMarker_->setMetricsMode(Ogre::GMM_PIXELS);
57        navMarker_->setMaterialName("Orxonox/NavMarker");
58        navMarker_->setDimensions(16,16);
59        navMarker_->setPosition(0,386);
60        navMarker_->hide();
61        container_->addChild(navMarker_);
62
[1339]63        // these have to fit the data in the level
[1302]64        shipPos_ = Vector3(0.0, 0.0, 0.0);
[1308]65        initialDir_ = Vector3(1.0, 0.0, 0.0);
[1302]66        currentDir_ = initialDir_;
[1308]67        initialOrth_ = Vector3(0.0, 0.0, 1.0);
[1302]68        currentOrth_ = initialOrth_;
[1343]69        plane = Plane(currentDir_, shipPos_);
[1310]70
[1302]71        setMetricsMode(Ogre::GMM_PIXELS);
[1310]72        setMaterialName("Orxonox/Radar");
[1314]73        resize();
[1339]74
[1335]75        container_->addChild(this);
[1283]76    }
[1302]77
[1314]78    void RadarOverlayElement::resize() {
79        // if window is resized, we must adapt these...
80        windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
81        windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
[1342]82        dim_ = (int) (dimRel_*windowH_);
83        left_ = (int) (leftRel_*windowW_-dim_/2);
84        top_ = (int) (topRel_*windowH_-dim_/2);
[1314]85        setPosition(left_, top_);
86        setDimensions(dim_,dim_);
87    }
88
[1302]89    void RadarOverlayElement::update() {
90        shipPos_ = SpaceShip::instance_s->getPosition();
[1308]91        currentDir_ = SpaceShip::instance_s->getOrientation()*initialDir_;              // according to beni....
92                currentOrth_ = SpaceShip::instance_s->getOrientation()*initialOrth_;
[1343]93        plane = Plane(currentDir_, shipPos_);
[1310]94
[1339]95        RadarObject* ro = firstRadarObject_;
96        // iterate through all RadarObjects
97                while(ro != NULL){
[1346]98                    // calc position on radar...
[1339]99            ro->radius_ = calcRadius(ro);
100            ro->phi_ = calcPhi(ro);
101            ro->right_ = calcRight(ro);
[1346]102
103            // set size to fit distance...
104            float d = (ro->pos_-shipPos_).length();
105            if(d<4000) ro->panel_->setDimensions(4,4);
106            else if(d<8000) ro->panel_->setDimensions(3,3);
107            else if(d<12000) ro->panel_->setDimensions(2,2);
108            else ro->panel_->setDimensions(1,1);
109
[1339]110            if (ro->right_){
111                ro->panel_->setPosition(sin(ro->phi_)*ro->radius_/
112                    3.5*dim_/2+dim_/2+left_-2,-cos(ro->phi_)*ro->radius_/3.5*dim_/2+dim_/2+top_-2);
113            }
114            else {
115                ro->panel_->setPosition(-sin(ro->phi_)*ro->radius_/
116                    3.5*dim_/2+dim_/2+left_-2,-cos(ro->phi_)*ro->radius_/3.5*dim_/2+dim_/2+top_-2);
117            }
118            ro = ro->next;
119                }
[1350]120                updateNavMarker();
[1339]121    }
[1310]122
[1350]123    void RadarOverlayElement::updateNavMarker(){
124        if(focus_ == NULL) return;
125        // from the angle we find out where to draw the marker
126        // and which of the four arrows to take
127        float r1 = 0.97;//atan(windowW_/windowH_); // doesn't work correctly yet
128        float phi = focus_->phi_;
129        if(focus_->right_){
130            if(phi<r1){
131                navMarker_->setPosition(tan(phi)*windowH_/2+windowW_/2, 0);
132                navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
133            }
134            else if(phi>3.14-r1){
135                navMarker_->setPosition(-tan(phi)*windowH_/2+windowW_/2, windowH_-16);
136                navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
137            }
138            else {
139                navMarker_->setPosition(windowW_-16, -tan((3.14-2*phi)/2)*windowW_/2+windowH_/2);
140                navMarker_->setUV(0.5, 0.5, 1.0, 1.0);
141            }
142        }
143        else{
144            if(phi<r1) {
145                navMarker_->setPosition(-tan(phi)*windowH_/2+windowW_/2, 0);
146                navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
147            }
148            else if(phi>3.14-r1) {
149                navMarker_->setPosition(tan(phi)*windowH_/2+windowW_/2, windowH_-16);
150                navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
151            }
152            else {
153                navMarker_->setPosition(0, -tan((3.14-2*phi)/2)*windowW_/2+windowH_/2);
154                navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
155            }
156        }
157    }
158
[1339]159    void RadarOverlayElement::addObject(Vector3 pos){
160        if(firstRadarObject_ == NULL){
[1346]161            firstRadarObject_ = new RadarObject(container_, pos);
[1339]162            lastRadarObject_ = firstRadarObject_;
[1308]163        }
[1339]164        else{
[1346]165            lastRadarObject_->next = new RadarObject(container_, pos);
[1339]166            lastRadarObject_ = lastRadarObject_->next;
[1308]167        }
[1335]168        }
[1339]169
170        void RadarOverlayElement::listObjects(){
171            int i = 0;
172            RadarObject* ro = firstRadarObject_;
173            COUT(3) << "List of RadarObjects:\n";
174            // iterate through all Radar Objects
175            while(ro != NULL) {
176                COUT(3) << i++ << ": " << ro->pos_ << std::endl;
177                ro = ro->next;
178            }
179        }
180
181        float RadarOverlayElement::calcRadius(RadarObject* obj){
182            return(acos((currentDir_.dotProduct(obj->pos_ - shipPos_))/
183                        ((obj->pos_ - shipPos_).length()*currentDir_.length())));
184        }
185
186        float RadarOverlayElement::calcPhi(RadarObject* obj){
[1343]187            // project difference vector on our plane...
188            Ogre::Vector3 proj = plane.projectVector(obj->pos_ - shipPos_);
189            // ...and find out the angle
190            return(acos((currentOrth_.dotProduct(proj))/
191            (currentOrth_.length()*proj.length())));
[1339]192        }
193
194        bool RadarOverlayElement::calcRight(RadarObject* obj){
195            if((currentDir_.crossProduct(currentOrth_)).dotProduct(obj->pos_ - shipPos_) > 0)
196                return true;
197        else return false;
198        }
[1350]199
200        /*static*/void RadarOverlayElement::cycleFocus(){
201            if(RadarOverlayElement::instance_s == NULL) return;
202            if(RadarOverlayElement::instance_s->focus_ == NULL)
203            RadarOverlayElement::instance_s->focus_ = RadarOverlayElement::instance_s->firstRadarObject_;
204        else
205            RadarOverlayElement::instance_s->focus_ = RadarOverlayElement::instance_s->focus_->next;
206        if(RadarOverlayElement::instance_s->focus_ == NULL)
207            RadarOverlayElement::instance_s->navMarker_->hide();
208        else
209            RadarOverlayElement::instance_s->navMarker_->show();
210        }
[1283]211}
212
[1335]213/* my local clipboard...
214COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n";
[1339]215COUT(3) << firstRadarObject_->radius_ << "  " << firstRadarObject_->phi_ << std::endl;
[1335]216COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n";
217*/
Note: See TracBrowser for help on using the repository browser.