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
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    using namespace Ogre;
33
34    RadarOverlayElement* RadarOverlayElement::instance_s = NULL;
35
36    RadarOverlayElement::RadarOverlayElement(const String& name):Ogre::PanelOverlayElement(name){
37        RadarOverlayElement::instance_s = this;
38    }
39
40    RadarOverlayElement::~RadarOverlayElement(){
41    }
42
43    void RadarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, Ogre::OverlayContainer* container){
44        // some initial data
45                om = &Ogre::OverlayManager::getSingleton();
46        dimRel_ = dimRel;
47        leftRel_ = leftRel;
48        topRel_ = topRel;
49        container_ = container;
50        firstRadarObject_ = NULL;
51        lastRadarObject_ = NULL;
52        focus_ = NULL;
53
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
63        // these have to fit the data in the level
64        shipPos_ = Vector3(0.0, 0.0, 0.0);
65        initialDir_ = Vector3(1.0, 0.0, 0.0);
66        currentDir_ = initialDir_;
67        initialOrth_ = Vector3(0.0, 0.0, 1.0);
68        currentOrth_ = initialOrth_;
69        plane = Plane(currentDir_, shipPos_);
70
71        setMetricsMode(Ogre::GMM_PIXELS);
72        setMaterialName("Orxonox/Radar");
73        resize();
74
75        container_->addChild(this);
76    }
77
78    void RadarOverlayElement::resize() {
79        // if window is resized, we must adapt these...
80        windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
81        windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
82        dim_ = (int) (dimRel_*windowH_);
83        left_ = (int) (leftRel_*windowW_-dim_/2);
84        top_ = (int) (topRel_*windowH_-dim_/2);
85        setPosition(left_, top_);
86        setDimensions(dim_,dim_);
87    }
88
89    void RadarOverlayElement::update() {
90        shipPos_ = SpaceShip::instance_s->getPosition();
91        currentDir_ = SpaceShip::instance_s->getOrientation()*initialDir_;              // according to beni....
92                currentOrth_ = SpaceShip::instance_s->getOrientation()*initialOrth_;
93        plane = Plane(currentDir_, shipPos_);
94
95        RadarObject* ro = firstRadarObject_;
96        // iterate through all RadarObjects
97                while(ro != NULL){
98                    // calc position on radar...
99            ro->radius_ = calcRadius(ro);
100            ro->phi_ = calcPhi(ro);
101            ro->right_ = calcRight(ro);
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
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                }
120                updateNavMarker();
121    }
122
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
159    void RadarOverlayElement::addObject(Vector3 pos){
160        if(firstRadarObject_ == NULL){
161            firstRadarObject_ = new RadarObject(container_, pos);
162            lastRadarObject_ = firstRadarObject_;
163        }
164        else{
165            lastRadarObject_->next = new RadarObject(container_, pos);
166            lastRadarObject_ = lastRadarObject_->next;
167        }
168        }
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){
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())));
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        }
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        }
211}
212
213/* my local clipboard...
214COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n";
215COUT(3) << firstRadarObject_->radius_ << "  " << firstRadarObject_->phi_ << std::endl;
216COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n";
217*/
Note: See TracBrowser for help on using the repository browser.