1 | /* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
---|
2 | * Author: |
---|
3 | * Yuning Chai |
---|
4 | * Co-authors: |
---|
5 | * Felix Schulthess |
---|
6 | * |
---|
7 | */ |
---|
8 | |
---|
9 | #include "OrxonoxStableHeaders.h" |
---|
10 | #include "RadarOverlayElement.h" |
---|
11 | |
---|
12 | #include <string> |
---|
13 | #include <OgreOverlayManager.h> |
---|
14 | #include <OgreStringConverter.h> |
---|
15 | |
---|
16 | #include "GraphicsEngine.h" |
---|
17 | #include "core/Tickable.h" |
---|
18 | #include "core/ConsoleCommand.h" |
---|
19 | #include "objects/SpaceShip.h" |
---|
20 | #include "RadarObject.h" |
---|
21 | #include "HUD.h" |
---|
22 | |
---|
23 | namespace orxonox |
---|
24 | { |
---|
25 | using namespace Ogre; |
---|
26 | |
---|
27 | RadarOverlayElement::RadarOverlayElement(const String& name):PanelOverlayElement(name){ |
---|
28 | } |
---|
29 | |
---|
30 | RadarOverlayElement::~RadarOverlayElement(){ |
---|
31 | } |
---|
32 | |
---|
33 | void RadarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, OverlayContainer* container){ |
---|
34 | // some initial data |
---|
35 | om = &OverlayManager::getSingleton(); |
---|
36 | dimRel_ = dimRel; |
---|
37 | leftRel_ = leftRel; |
---|
38 | topRel_ = topRel; |
---|
39 | container_ = container; |
---|
40 | |
---|
41 | setMetricsMode(GMM_PIXELS); |
---|
42 | setMaterialName("Orxonox/Radar"); |
---|
43 | resize(); |
---|
44 | |
---|
45 | container_->addChild(this); |
---|
46 | } |
---|
47 | |
---|
48 | void RadarOverlayElement::resize() { |
---|
49 | // if window is resized, we must adapt these... |
---|
50 | windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); |
---|
51 | windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); |
---|
52 | dim_ = (int) (dimRel_*windowH_); |
---|
53 | left_ = (int) (leftRel_*windowW_-dim_/2); |
---|
54 | top_ = (int) (topRel_*windowH_-dim_/2); |
---|
55 | setPosition(left_, top_); |
---|
56 | setDimensions(dim_,dim_); |
---|
57 | } |
---|
58 | |
---|
59 | void RadarOverlayElement::update() { |
---|
60 | shipPos_ = SpaceShip::getLocalShip()->getPosition(); |
---|
61 | currentDir_ = SpaceShip::getLocalShip()->getDir(); |
---|
62 | currentOrth_ = SpaceShip::getLocalShip()->getOrth(); |
---|
63 | // iterate through all RadarObjects |
---|
64 | for(std::set<RadarObject*>::iterator it=HUD::getSingleton().roSet.begin(); it!=HUD::getSingleton().roSet.end(); it++){ |
---|
65 | // calc position on radar... |
---|
66 | float radius = calcRadius(shipPos_, currentDir_, currentOrth_, (*it)); |
---|
67 | float phi = calcPhi(shipPos_, currentDir_, currentOrth_, (*it)); |
---|
68 | bool right = calcRight(shipPos_, currentDir_, currentOrth_, (*it)); |
---|
69 | |
---|
70 | // set size to fit distance... |
---|
71 | float d = ((*it)->getPosition()-shipPos_).length(); |
---|
72 | if(d<10000) (*it)->panel_->setDimensions(4,4); |
---|
73 | else if(d<20000) (*it)->panel_->setDimensions(3,3); |
---|
74 | else (*it)->panel_->setDimensions(2,2); |
---|
75 | |
---|
76 | if (right){ |
---|
77 | (*it)->panel_->setPosition(sin(phi)*radius/ |
---|
78 | 3.5*dim_/2+dim_/2+left_-2,-cos(phi)*radius/3.5*dim_/2+dim_/2+top_-2); |
---|
79 | } |
---|
80 | else { |
---|
81 | (*it)->panel_->setPosition(-sin(phi)*radius/ |
---|
82 | 3.5*dim_/2+dim_/2+left_-2,-cos(phi)*radius/3.5*dim_/2+dim_/2+top_-2); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | void RadarOverlayElement::listObjects(){ |
---|
88 | int i = 0; |
---|
89 | COUT(3) << "List of RadarObjects:\n"; |
---|
90 | // iterate through all Radar Objects |
---|
91 | for(std::set<RadarObject*>::iterator it=HUD::getSingleton().roSet.begin(); it!=HUD::getSingleton().roSet.end(); it++){ |
---|
92 | COUT(3) << i++ << ": " << (*it)->getPosition() << std::endl; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | float RadarOverlayElement::calcRadius(Vector3 pos, Vector3 dir, Vector3 orth, RadarObject* obj){ |
---|
97 | return(acos((dir.dotProduct(obj->getPosition() - pos))/ |
---|
98 | ((obj->getPosition() - pos).length()*dir.length()))); |
---|
99 | } |
---|
100 | |
---|
101 | float RadarOverlayElement::calcPhi(Vector3 pos, Vector3 dir, Vector3 orth, RadarObject* obj){ |
---|
102 | // project difference vector on our plane... |
---|
103 | Vector3 proj = Plane(dir, pos).projectVector(obj->getPosition() - pos); |
---|
104 | // ...and find out the angle |
---|
105 | return(acos((orth.dotProduct(proj))/ |
---|
106 | (orth.length()*proj.length()))); |
---|
107 | } |
---|
108 | |
---|
109 | bool RadarOverlayElement::calcRight(Vector3 pos, Vector3 dir, Vector3 orth, RadarObject* obj){ |
---|
110 | if((dir.crossProduct(orth)).dotProduct(obj->getPosition() - pos) > 0) |
---|
111 | return true; |
---|
112 | else return false; |
---|
113 | } |
---|
114 | } |
---|