Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1378 was 1373, checked in by rgrieder, 17 years ago
  • HUD additions for msvc
File size: 8.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
[1373]28#include "OrxonoxStableHeaders.h"
[1283]29#include "RadarOverlayElement.h"
30
31namespace orxonox
32{
[1358]33    ConsoleCommandShortcut(RadarOverlayElement, cycleFocus, AccessLevel::User);
[1353]34
[1302]35    using namespace Ogre;
[1283]36
[1350]37    RadarOverlayElement* RadarOverlayElement::instance_s = NULL;
38
[1302]39    RadarOverlayElement::RadarOverlayElement(const String& name):Ogre::PanelOverlayElement(name){
[1350]40        RadarOverlayElement::instance_s = this;
[1302]41    }
[1283]42
[1302]43    RadarOverlayElement::~RadarOverlayElement(){
44    }
[1283]45
[1328]46    void RadarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, Ogre::OverlayContainer* container){
[1339]47        // some initial data
48                om = &Ogre::OverlayManager::getSingleton();
[1314]49        dimRel_ = dimRel;
50        leftRel_ = leftRel;
51        topRel_ = topRel;
[1302]52        container_ = container;
[1339]53        firstRadarObject_ = NULL;
54        lastRadarObject_ = NULL;
[1350]55        focus_ = NULL;
[1283]56
[1350]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
[1339]66        // these have to fit the data in the level
[1302]67        shipPos_ = Vector3(0.0, 0.0, 0.0);
[1308]68        initialDir_ = Vector3(1.0, 0.0, 0.0);
[1302]69        currentDir_ = initialDir_;
[1308]70        initialOrth_ = Vector3(0.0, 0.0, 1.0);
[1302]71        currentOrth_ = initialOrth_;
[1343]72        plane = Plane(currentDir_, shipPos_);
[1310]73
[1302]74        setMetricsMode(Ogre::GMM_PIXELS);
[1310]75        setMaterialName("Orxonox/Radar");
[1314]76        resize();
[1339]77
[1335]78        container_->addChild(this);
[1283]79    }
[1302]80
[1314]81    void RadarOverlayElement::resize() {
82        // if window is resized, we must adapt these...
83        windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
84        windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
[1342]85        dim_ = (int) (dimRel_*windowH_);
86        left_ = (int) (leftRel_*windowW_-dim_/2);
87        top_ = (int) (topRel_*windowH_-dim_/2);
[1314]88        setPosition(left_, top_);
89        setDimensions(dim_,dim_);
90    }
91
[1302]92    void RadarOverlayElement::update() {
93        shipPos_ = SpaceShip::instance_s->getPosition();
[1308]94        currentDir_ = SpaceShip::instance_s->getOrientation()*initialDir_;              // according to beni....
95                currentOrth_ = SpaceShip::instance_s->getOrientation()*initialOrth_;
[1343]96        plane = Plane(currentDir_, shipPos_);
[1310]97
[1339]98        RadarObject* ro = firstRadarObject_;
99        // iterate through all RadarObjects
100                while(ro != NULL){
[1346]101                    // calc position on radar...
[1339]102            ro->radius_ = calcRadius(ro);
103            ro->phi_ = calcPhi(ro);
104            ro->right_ = calcRight(ro);
[1346]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);
[1354]110            else if(d<16000) ro->panel_->setDimensions(2,2);
[1346]111            else ro->panel_->setDimensions(1,1);
112
[1339]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                }
[1350]123                updateNavMarker();
[1339]124    }
[1310]125
[1350]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
[1357]130        float r1 = atan((float)(windowW_)/(float)(windowH_));
[1350]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
[1339]162    void RadarOverlayElement::addObject(Vector3 pos){
163        if(firstRadarObject_ == NULL){
[1346]164            firstRadarObject_ = new RadarObject(container_, pos);
[1339]165            lastRadarObject_ = firstRadarObject_;
[1308]166        }
[1339]167        else{
[1346]168            lastRadarObject_->next = new RadarObject(container_, pos);
[1339]169            lastRadarObject_ = lastRadarObject_->next;
[1308]170        }
[1335]171        }
[1339]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
[1356]184        float RadarOverlayElement::getDist2Focus(){
[1358]185            if(focus_ == NULL) return(0.0);
[1356]186            return((focus_->pos_-shipPos_).length());
187        }
188
[1339]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){
[1343]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())));
[1339]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        }
[1350]207
208        /*static*/void RadarOverlayElement::cycleFocus(){
209            if(RadarOverlayElement::instance_s == NULL) return;
[1353]210
211            if(RadarOverlayElement::instance_s->focus_ == NULL){
[1350]212            RadarOverlayElement::instance_s->focus_ = RadarOverlayElement::instance_s->firstRadarObject_;
[1353]213            }
214        else{
215            RadarOverlayElement::instance_s->focus_->panel_->setMaterialName("Orxonox/RedDot");
[1350]216            RadarOverlayElement::instance_s->focus_ = RadarOverlayElement::instance_s->focus_->next;
[1353]217        }
218
219        if(RadarOverlayElement::instance_s->focus_ == NULL){
[1350]220            RadarOverlayElement::instance_s->navMarker_->hide();
[1353]221        }
222        else{
[1350]223            RadarOverlayElement::instance_s->navMarker_->show();
[1353]224            RadarOverlayElement::instance_s->focus_->panel_->setMaterialName("Orxonox/WhiteDot");
225        }
[1350]226        }
[1283]227}
228
[1335]229/* my local clipboard...
230COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n";
[1339]231COUT(3) << firstRadarObject_->radius_ << "  " << firstRadarObject_->phi_ << std::endl;
[1335]232COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n";
233*/
Note: See TracBrowser for help on using the repository browser.