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 | #include <string.h> |
---|
28 | |
---|
29 | |
---|
30 | #include <string.h> |
---|
31 | #include <OgreOverlayManager.h> |
---|
32 | #include <OgreStringConverter.h> |
---|
33 | #include <OgrePanelOverlayElement.h> |
---|
34 | |
---|
35 | #include "GraphicsEngine.h" |
---|
36 | #include "core/Tickable.h" |
---|
37 | #include "core/ConsoleCommand.h" |
---|
38 | #include "objects/SpaceShip.h" |
---|
39 | #include "HUD.h" |
---|
40 | #include "RadarOverlayElement.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | |
---|
45 | using namespace Ogre; |
---|
46 | |
---|
47 | RadarOverlayElement::RadarOverlayElement(const String& name):PanelOverlayElement(name){ |
---|
48 | } |
---|
49 | |
---|
50 | RadarOverlayElement::~RadarOverlayElement(){ |
---|
51 | } |
---|
52 | |
---|
53 | void RadarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, OverlayContainer* container){ |
---|
54 | // some initial data |
---|
55 | om = &OverlayManager::getSingleton(); |
---|
56 | dimRel_ = dimRel; |
---|
57 | leftRel_ = leftRel; |
---|
58 | topRel_ = topRel; |
---|
59 | container_ = container; |
---|
60 | |
---|
61 | // these have to fit the data in the level |
---|
62 | shipPos_ = Vector3(0.0, 0.0, 0.0); |
---|
63 | initialDir_ = Vector3(1.0, 0.0, 0.0); |
---|
64 | currentDir_ = initialDir_; |
---|
65 | initialOrth_ = Vector3(0.0, 0.0, 1.0); |
---|
66 | currentOrth_ = initialOrth_; |
---|
67 | plane = Plane(currentDir_, shipPos_); |
---|
68 | |
---|
69 | setMetricsMode(GMM_PIXELS); |
---|
70 | setMaterialName("Orxonox/Radar"); |
---|
71 | resize(); |
---|
72 | |
---|
73 | container_->addChild(this); |
---|
74 | } |
---|
75 | |
---|
76 | void RadarOverlayElement::resize() { |
---|
77 | // if window is resized, we must adapt these... |
---|
78 | windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); |
---|
79 | windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); |
---|
80 | dim_ = (int) (dimRel_*windowH_); |
---|
81 | left_ = (int) (leftRel_*windowW_-dim_/2); |
---|
82 | top_ = (int) (topRel_*windowH_-dim_/2); |
---|
83 | setPosition(left_, top_); |
---|
84 | setDimensions(dim_,dim_); |
---|
85 | } |
---|
86 | |
---|
87 | void RadarOverlayElement::update() { |
---|
88 | shipPos_ = SpaceShip::instance_s->getPosition(); |
---|
89 | currentDir_ = SpaceShip::instance_s->getOrientation()*initialDir_; |
---|
90 | currentOrth_ = SpaceShip::instance_s->getOrientation()*initialOrth_; |
---|
91 | plane = Plane(currentDir_, shipPos_); |
---|
92 | RadarObject* ro = HUD::getSingleton().getFirstRadarObject(); |
---|
93 | // iterate through all RadarObjects |
---|
94 | while(ro != NULL){ |
---|
95 | // calc position on radar... |
---|
96 | ro->radius_ = calcRadius(ro); |
---|
97 | ro->phi_ = calcPhi(ro); |
---|
98 | ro->right_ = calcRight(ro); |
---|
99 | |
---|
100 | // set size to fit distance... |
---|
101 | float d = (ro->pos_-shipPos_).length(); |
---|
102 | if(d<4000) ro->panel_->setDimensions(4,4); |
---|
103 | else if(d<8000) ro->panel_->setDimensions(3,3); |
---|
104 | else if(d<16000) ro->panel_->setDimensions(2,2); |
---|
105 | else ro->panel_->setDimensions(1,1); |
---|
106 | |
---|
107 | if (ro->right_){ |
---|
108 | ro->panel_->setPosition(sin(ro->phi_)*ro->radius_/ |
---|
109 | 3.5*dim_/2+dim_/2+left_-2,-cos(ro->phi_)*ro->radius_/3.5*dim_/2+dim_/2+top_-2); |
---|
110 | } |
---|
111 | else { |
---|
112 | ro->panel_->setPosition(-sin(ro->phi_)*ro->radius_/ |
---|
113 | 3.5*dim_/2+dim_/2+left_-2,-cos(ro->phi_)*ro->radius_/3.5*dim_/2+dim_/2+top_-2); |
---|
114 | } |
---|
115 | listObjects(); |
---|
116 | ro = ro->next; |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | void RadarOverlayElement::listObjects(){ |
---|
121 | int i = 0; |
---|
122 | RadarObject* ro = HUD::getSingleton().getFirstRadarObject(); |
---|
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){ |
---|
137 | // project difference vector on our plane... |
---|
138 | Vector3 proj = plane.projectVector(obj->pos_ - shipPos_); |
---|
139 | // ...and find out the angle |
---|
140 | return(acos((currentOrth_.dotProduct(proj))/ |
---|
141 | (currentOrth_.length()*proj.length()))); |
---|
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 | } |
---|
149 | } |
---|