Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

splitted two classes in two files

File size: 5.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 "RadarOverlayElement.h"
29
30namespace orxonox
31{
32    using namespace Ogre;
33
34    RadarOverlayElement::RadarOverlayElement(const String& name):Ogre::PanelOverlayElement(name){
35    }
36
37    RadarOverlayElement::~RadarOverlayElement(){
38    }
39
40    void RadarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, Ogre::OverlayContainer* container){
41        // some initial data
42                om = &Ogre::OverlayManager::getSingleton();
43        dimRel_ = dimRel;
44        leftRel_ = leftRel;
45        topRel_ = topRel;
46        container_ = container;
47        firstRadarObject_ = NULL;
48        lastRadarObject_ = NULL;
49
50        // these have to fit the data in the level
51        shipPos_ = Vector3(0.0, 0.0, 0.0);
52        initialDir_ = Vector3(1.0, 0.0, 0.0);
53        currentDir_ = initialDir_;
54        initialOrth_ = Vector3(0.0, 0.0, 1.0);
55        currentOrth_ = initialOrth_;
56        plane = Plane(currentDir_, shipPos_);
57
58        setMetricsMode(Ogre::GMM_PIXELS);
59        setMaterialName("Orxonox/Radar");
60        resize();
61
62        container_->addChild(this);
63    }
64
65    void RadarOverlayElement::resize() {
66        // if window is resized, we must adapt these...
67        windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
68        windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
69        dim_ = (int) (dimRel_*windowH_);
70        left_ = (int) (leftRel_*windowW_-dim_/2);
71        top_ = (int) (topRel_*windowH_-dim_/2);
72        setPosition(left_, top_);
73        setDimensions(dim_,dim_);
74    }
75
76    void RadarOverlayElement::update() {
77        shipPos_ = SpaceShip::instance_s->getPosition();
78        currentDir_ = SpaceShip::instance_s->getOrientation()*initialDir_;              // according to beni....
79                currentOrth_ = SpaceShip::instance_s->getOrientation()*initialOrth_;
80        plane = Plane(currentDir_, shipPos_);
81
82        RadarObject* ro = firstRadarObject_;
83        // iterate through all RadarObjects
84                while(ro != NULL){
85                    // calc position on radar...
86            ro->radius_ = calcRadius(ro);
87            ro->phi_ = calcPhi(ro);
88            ro->right_ = calcRight(ro);
89
90            // set size to fit distance...
91            float d = (ro->pos_-shipPos_).length();
92            if(d<4000) ro->panel_->setDimensions(4,4);
93            else if(d<8000) ro->panel_->setDimensions(3,3);
94            else if(d<12000) ro->panel_->setDimensions(2,2);
95            else ro->panel_->setDimensions(1,1);
96
97            if (ro->right_){
98                ro->panel_->setPosition(sin(ro->phi_)*ro->radius_/
99                    3.5*dim_/2+dim_/2+left_-2,-cos(ro->phi_)*ro->radius_/3.5*dim_/2+dim_/2+top_-2);
100            }
101            else {
102                ro->panel_->setPosition(-sin(ro->phi_)*ro->radius_/
103                    3.5*dim_/2+dim_/2+left_-2,-cos(ro->phi_)*ro->radius_/3.5*dim_/2+dim_/2+top_-2);
104            }
105            ro = ro->next;
106                }
107    }
108
109    void RadarOverlayElement::addObject(Vector3 pos){
110        if(firstRadarObject_ == NULL){
111            firstRadarObject_ = new RadarObject(container_, pos);
112            lastRadarObject_ = firstRadarObject_;
113        }
114        else{
115            lastRadarObject_->next = new RadarObject(container_, pos);
116            lastRadarObject_ = lastRadarObject_->next;
117        }
118        }
119
120        void RadarOverlayElement::listObjects(){
121            int i = 0;
122            RadarObject* ro = firstRadarObject_;
123            COUT(3) << "List of RadarObjects:\n";
124            // iterate through all Radar Objects
125            while(ro != NULL) {
126                COUT(3) << i++ << ": " << ro->pos_ << std::endl;
127                ro = ro->next;
128            }
129        }
130
131        float RadarOverlayElement::calcRadius(RadarObject* obj){
132            return(acos((currentDir_.dotProduct(obj->pos_ - shipPos_))/
133                        ((obj->pos_ - shipPos_).length()*currentDir_.length())));
134        }
135
136        float RadarOverlayElement::calcPhi(RadarObject* obj){
137            // project difference vector on our plane...
138            Ogre::Vector3 proj = plane.projectVector(obj->pos_ - shipPos_);
139            // ...and find out the angle
140            return(acos((currentOrth_.dotProduct(proj))/
141            (currentOrth_.length()*proj.length())));
142        }
143
144        bool RadarOverlayElement::calcRight(RadarObject* obj){
145            if((currentDir_.crossProduct(currentOrth_)).dotProduct(obj->pos_ - shipPos_) > 0)
146                return true;
147        else return false;
148        }
149}
150
151/* my local clipboard...
152COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n";
153COUT(3) << firstRadarObject_->radius_ << "  " << firstRadarObject_->phi_ << std::endl;
154COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n";
155*/
Note: See TracBrowser for help on using the repository browser.