Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1425 was 1410, checked in by rgrieder, 16 years ago
  • converted tabs to spaces in HUD
  • adjusted msvc project
File size: 4.7 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:
[1406]24*      Felix Schulthess
[1283]25*
26*/
27
[1373]28#include "OrxonoxStableHeaders.h"
[1283]29#include "RadarOverlayElement.h"
30
[1410]31#include <string>
[1406]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"
[1410]39#include "RadarObject.h"
[1406]40#include "HUD.h"
41
[1283]42namespace orxonox
43{
[1302]44    using namespace Ogre;
[1283]45
[1406]46    RadarOverlayElement::RadarOverlayElement(const String& name):PanelOverlayElement(name){
[1302]47    }
[1283]48
[1302]49    RadarOverlayElement::~RadarOverlayElement(){
50    }
[1283]51
[1406]52    void RadarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, OverlayContainer* container){
[1339]53        // some initial data
[1410]54        om = &OverlayManager::getSingleton();
[1314]55        dimRel_ = dimRel;
56        leftRel_ = leftRel;
57        topRel_ = topRel;
[1302]58        container_ = container;
[1283]59
[1406]60        setMetricsMode(GMM_PIXELS);
[1310]61        setMaterialName("Orxonox/Radar");
[1314]62        resize();
[1339]63
[1335]64        container_->addChild(this);
[1283]65    }
[1302]66
[1314]67    void RadarOverlayElement::resize() {
68        // if window is resized, we must adapt these...
69        windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
70        windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
[1342]71        dim_ = (int) (dimRel_*windowH_);
72        left_ = (int) (leftRel_*windowW_-dim_/2);
73        top_ = (int) (topRel_*windowH_-dim_/2);
[1314]74        setPosition(left_, top_);
75        setDimensions(dim_,dim_);
76    }
77
[1302]78    void RadarOverlayElement::update() {
[1384]79        shipPos_ = SpaceShip::getLocalShip()->getPosition();
[1406]80        currentDir_ = SpaceShip::getLocalShip()->getDir();
[1410]81        currentOrth_ = SpaceShip::getLocalShip()->getOrth();
[1406]82        RadarObject* ro = HUD::getSingleton().getFirstRadarObject();
[1339]83        // iterate through all RadarObjects
[1410]84        while(ro != NULL){
85        // calc position on radar...
[1406]86            float radius = calcRadius(shipPos_, currentDir_, currentOrth_, ro);
87            float phi = calcPhi(shipPos_, currentDir_, currentOrth_, ro);
88            bool right = calcRight(shipPos_, currentDir_, currentOrth_, 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);
[1354]94            else if(d<16000) ro->panel_->setDimensions(2,2);
[1346]95            else ro->panel_->setDimensions(1,1);
96
[1406]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);
[1339]100            }
101            else {
[1406]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);
[1339]104            }
105            ro = ro->next;
[1410]106        }
[1339]107    }
[1310]108
[1410]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    }
[1339]119
[1410]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    }
[1356]124
[1410]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    }
[1339]132
[1410]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;
[1339]136        else return false;
[1410]137    }
[1283]138}
Note: See TracBrowser for help on using the repository browser.