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
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
[1302]34    RadarOverlayElement::RadarOverlayElement(const String& name):Ogre::PanelOverlayElement(name){
35    }
[1283]36
[1302]37    RadarOverlayElement::~RadarOverlayElement(){
38    }
[1283]39
[1328]40    void RadarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, Ogre::OverlayContainer* container){
[1339]41        // some initial data
42                om = &Ogre::OverlayManager::getSingleton();
[1314]43        dimRel_ = dimRel;
44        leftRel_ = leftRel;
45        topRel_ = topRel;
[1302]46        container_ = container;
[1339]47        firstRadarObject_ = NULL;
48        lastRadarObject_ = NULL;
[1283]49
[1339]50        // these have to fit the data in the level
[1302]51        shipPos_ = Vector3(0.0, 0.0, 0.0);
[1308]52        initialDir_ = Vector3(1.0, 0.0, 0.0);
[1302]53        currentDir_ = initialDir_;
[1308]54        initialOrth_ = Vector3(0.0, 0.0, 1.0);
[1302]55        currentOrth_ = initialOrth_;
[1343]56        plane = Plane(currentDir_, shipPos_);
[1310]57
[1302]58        setMetricsMode(Ogre::GMM_PIXELS);
[1310]59        setMaterialName("Orxonox/Radar");
[1314]60        resize();
[1339]61
[1335]62        container_->addChild(this);
[1283]63    }
[1302]64
[1314]65    void RadarOverlayElement::resize() {
66        // if window is resized, we must adapt these...
67        windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
68        windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
[1342]69        dim_ = (int) (dimRel_*windowH_);
70        left_ = (int) (leftRel_*windowW_-dim_/2);
71        top_ = (int) (topRel_*windowH_-dim_/2);
[1314]72        setPosition(left_, top_);
73        setDimensions(dim_,dim_);
74    }
75
[1302]76    void RadarOverlayElement::update() {
77        shipPos_ = SpaceShip::instance_s->getPosition();
[1308]78        currentDir_ = SpaceShip::instance_s->getOrientation()*initialDir_;              // according to beni....
79                currentOrth_ = SpaceShip::instance_s->getOrientation()*initialOrth_;
[1343]80        plane = Plane(currentDir_, shipPos_);
[1310]81
[1339]82        RadarObject* ro = firstRadarObject_;
83        // iterate through all RadarObjects
84                while(ro != NULL){
[1346]85                    // calc position on radar...
[1339]86            ro->radius_ = calcRadius(ro);
87            ro->phi_ = calcPhi(ro);
88            ro->right_ = calcRight(ro);
[1346]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
[1339]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    }
[1310]108
[1339]109    void RadarOverlayElement::addObject(Vector3 pos){
110        if(firstRadarObject_ == NULL){
[1346]111            firstRadarObject_ = new RadarObject(container_, pos);
[1339]112            lastRadarObject_ = firstRadarObject_;
[1308]113        }
[1339]114        else{
[1346]115            lastRadarObject_->next = new RadarObject(container_, pos);
[1339]116            lastRadarObject_ = lastRadarObject_->next;
[1308]117        }
[1335]118        }
[1339]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){
[1343]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())));
[1339]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        }
[1283]149}
150
[1335]151/* my local clipboard...
152COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n";
[1339]153COUT(3) << firstRadarObject_->radius_ << "  " << firstRadarObject_->phi_ << std::endl;
[1335]154COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n";
155*/
Note: See TracBrowser for help on using the repository browser.