Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/orxonox/hud/RadarOverlayElement.cc @ 1384

Last change on this file since 1384 was 1384, checked in by scheusso, 16 years ago

changed radar handling (SpaceShip::instance is now SpaceShip::getLocalShip)

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