Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1418 was 1410, checked in by rgrieder, 17 years ago
  • converted tabs to spaces in HUD
  • adjusted msvc project
File size: 4.7 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*      Felix Schulthess
25*
26*/
27
28#include "OrxonoxStableHeaders.h"
29#include "RadarOverlayElement.h"
30
31#include <string>
32#include <OgreOverlayManager.h>
33#include <OgreStringConverter.h>
34
35#include "GraphicsEngine.h"
36#include "core/Tickable.h"
37#include "core/ConsoleCommand.h"
38#include "objects/SpaceShip.h"
39#include "RadarObject.h"
40#include "HUD.h"
41
42namespace orxonox
43{
44    using namespace Ogre;
45
46    RadarOverlayElement::RadarOverlayElement(const String& name):PanelOverlayElement(name){
47    }
48
49    RadarOverlayElement::~RadarOverlayElement(){
50    }
51
52    void RadarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, OverlayContainer* container){
53        // some initial data
54        om = &OverlayManager::getSingleton();
55        dimRel_ = dimRel;
56        leftRel_ = leftRel;
57        topRel_ = topRel;
58        container_ = container;
59
60        setMetricsMode(GMM_PIXELS);
61        setMaterialName("Orxonox/Radar");
62        resize();
63
64        container_->addChild(this);
65    }
66
67    void RadarOverlayElement::resize() {
68        // if window is resized, we must adapt these...
69        windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
70        windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
71        dim_ = (int) (dimRel_*windowH_);
72        left_ = (int) (leftRel_*windowW_-dim_/2);
73        top_ = (int) (topRel_*windowH_-dim_/2);
74        setPosition(left_, top_);
75        setDimensions(dim_,dim_);
76    }
77
78    void RadarOverlayElement::update() {
79        shipPos_ = SpaceShip::getLocalShip()->getPosition();
80        currentDir_ = SpaceShip::getLocalShip()->getDir();
81        currentOrth_ = SpaceShip::getLocalShip()->getOrth();
82        RadarObject* ro = HUD::getSingleton().getFirstRadarObject();
83        // iterate through all RadarObjects
84        while(ro != NULL){
85        // calc position on radar...
86            float radius = calcRadius(shipPos_, currentDir_, currentOrth_, ro);
87            float phi = calcPhi(shipPos_, currentDir_, currentOrth_, ro);
88            bool right = calcRight(shipPos_, currentDir_, currentOrth_, 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<16000) ro->panel_->setDimensions(2,2);
95            else ro->panel_->setDimensions(1,1);
96
97            if (right){
98                ro->panel_->setPosition(sin(phi)*radius/
99                    3.5*dim_/2+dim_/2+left_-2,-cos(phi)*radius/3.5*dim_/2+dim_/2+top_-2);
100            }
101            else {
102                ro->panel_->setPosition(-sin(phi)*radius/
103                    3.5*dim_/2+dim_/2+left_-2,-cos(phi)*radius/3.5*dim_/2+dim_/2+top_-2);
104            }
105            ro = ro->next;
106        }
107    }
108
109    void RadarOverlayElement::listObjects(){
110        int i = 0;
111        RadarObject* ro = HUD::getSingleton().getFirstRadarObject();
112        COUT(3) << "List of RadarObjects:\n";
113        // iterate through all Radar Objects
114        while(ro != NULL) {
115            COUT(3) << i++ << ": " << ro->pos_ << std::endl;
116            ro = ro->next;
117        }
118    }
119
120    float RadarOverlayElement::calcRadius(Vector3 pos, Vector3 dir, Vector3 orth, RadarObject* obj){
121        return(acos((dir.dotProduct(obj->pos_ - pos))/
122        ((obj->pos_ - pos).length()*dir.length())));
123    }
124
125    float RadarOverlayElement::calcPhi(Vector3 pos, Vector3 dir, Vector3 orth, RadarObject* obj){
126        // project difference vector on our plane...
127        Vector3 proj = Plane(dir, pos).projectVector(obj->pos_ - pos);
128        // ...and find out the angle
129        return(acos((orth.dotProduct(proj))/
130              (orth.length()*proj.length())));
131    }
132
133    bool RadarOverlayElement::calcRight(Vector3 pos, Vector3 dir, Vector3 orth, RadarObject* obj){
134        if((dir.crossProduct(orth)).dotProduct(obj->pos_ - pos) > 0)
135            return true;
136        else return false;
137    }
138}
Note: See TracBrowser for help on using the repository browser.