Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/hud/RadarOverlayElement.cc @ 1387

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

made HUD singleton

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