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 | |
---|
28 | /* local coordinate system of space ship at the beginning: |
---|
29 | |
---|
30 | y |
---|
31 | + z |
---|
32 | | + |
---|
33 | | / |
---|
34 | |/ |
---|
35 | x +------O |
---|
36 | */ |
---|
37 | |
---|
38 | #include <OgreOverlayManager.h> |
---|
39 | #include <OgreOverlayElement.h> |
---|
40 | #include <OgrePanelOverlayElement.h> |
---|
41 | #include <OgreStringConverter.h> |
---|
42 | #include "RadarOverlayElement.h" |
---|
43 | #include "GraphicsEngine.h" |
---|
44 | |
---|
45 | namespace orxonox |
---|
46 | { |
---|
47 | using namespace Ogre; |
---|
48 | |
---|
49 | RadarOverlayElement::RadarOverlayElement(const String& name):Ogre::PanelOverlayElement(name){ |
---|
50 | } |
---|
51 | |
---|
52 | RadarOverlayElement::~RadarOverlayElement(){ |
---|
53 | } |
---|
54 | |
---|
55 | void RadarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, Ogre::OverlayContainer* container){ |
---|
56 | // some initial data |
---|
57 | om = &Ogre::OverlayManager::getSingleton(); |
---|
58 | dimRel_ = dimRel; |
---|
59 | leftRel_ = leftRel; |
---|
60 | topRel_ = topRel; |
---|
61 | container_ = container; |
---|
62 | firstRadarObject_ = NULL; |
---|
63 | lastRadarObject_ = NULL; |
---|
64 | |
---|
65 | // these have to fit the data in the level |
---|
66 | shipPos_ = Vector3(0.0, 0.0, 0.0); |
---|
67 | initialDir_ = Vector3(1.0, 0.0, 0.0); |
---|
68 | currentDir_ = initialDir_; |
---|
69 | initialOrth_ = Vector3(0.0, 0.0, 1.0); |
---|
70 | currentOrth_ = initialOrth_; |
---|
71 | plane = Plane(currentDir_, shipPos_); |
---|
72 | |
---|
73 | setMetricsMode(Ogre::GMM_PIXELS); |
---|
74 | setMaterialName("Orxonox/Radar"); |
---|
75 | resize(); |
---|
76 | |
---|
77 | container_->addChild(this); |
---|
78 | } |
---|
79 | |
---|
80 | void RadarOverlayElement::resize() { |
---|
81 | // if window is resized, we must adapt these... |
---|
82 | windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); |
---|
83 | windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); |
---|
84 | dim_ = (int) (dimRel_*windowH_); |
---|
85 | left_ = (int) (leftRel_*windowW_-dim_/2); |
---|
86 | top_ = (int) (topRel_*windowH_-dim_/2); |
---|
87 | setPosition(left_, top_); |
---|
88 | setDimensions(dim_,dim_); |
---|
89 | } |
---|
90 | |
---|
91 | void RadarOverlayElement::update() { |
---|
92 | shipPos_ = SpaceShip::instance_s->getPosition(); |
---|
93 | currentDir_ = SpaceShip::instance_s->getOrientation()*initialDir_; // according to beni.... |
---|
94 | currentOrth_ = SpaceShip::instance_s->getOrientation()*initialOrth_; |
---|
95 | plane = Plane(currentDir_, shipPos_); |
---|
96 | |
---|
97 | RadarObject* ro = firstRadarObject_; |
---|
98 | // iterate through all RadarObjects |
---|
99 | while(ro != NULL){ |
---|
100 | ro->radius_ = calcRadius(ro); |
---|
101 | ro->phi_ = calcPhi(ro); |
---|
102 | ro->right_ = calcRight(ro); |
---|
103 | if (ro->right_){ |
---|
104 | ro->panel_->setPosition(sin(ro->phi_)*ro->radius_/ |
---|
105 | 3.5*dim_/2+dim_/2+left_-2,-cos(ro->phi_)*ro->radius_/3.5*dim_/2+dim_/2+top_-2); |
---|
106 | } |
---|
107 | else { |
---|
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 | ro = ro->next; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | void RadarOverlayElement::addObject(Vector3 pos){ |
---|
116 | if(firstRadarObject_ == NULL){ |
---|
117 | firstRadarObject_ = new RadarObject(container_); |
---|
118 | firstRadarObject_->pos_ = pos; |
---|
119 | lastRadarObject_ = firstRadarObject_; |
---|
120 | } |
---|
121 | else{ |
---|
122 | lastRadarObject_->next = new RadarObject(container_); |
---|
123 | lastRadarObject_->next->pos_ = pos; |
---|
124 | lastRadarObject_ = lastRadarObject_->next; |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | void RadarOverlayElement::listObjects(){ |
---|
129 | int i = 0; |
---|
130 | RadarObject* ro = firstRadarObject_; |
---|
131 | COUT(3) << "List of RadarObjects:\n"; |
---|
132 | // iterate through all Radar Objects |
---|
133 | while(ro != NULL) { |
---|
134 | COUT(3) << i++ << ": " << ro->pos_ << std::endl; |
---|
135 | ro = ro->next; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | float RadarOverlayElement::calcRadius(RadarObject* obj){ |
---|
140 | return(acos((currentDir_.dotProduct(obj->pos_ - shipPos_))/ |
---|
141 | ((obj->pos_ - shipPos_).length()*currentDir_.length()))); |
---|
142 | } |
---|
143 | |
---|
144 | float RadarOverlayElement::calcPhi(RadarObject* obj){ |
---|
145 | // project difference vector on our plane... |
---|
146 | Ogre::Vector3 proj = plane.projectVector(obj->pos_ - shipPos_); |
---|
147 | // ...and find out the angle |
---|
148 | return(acos((currentOrth_.dotProduct(proj))/ |
---|
149 | (currentOrth_.length()*proj.length()))); |
---|
150 | } |
---|
151 | |
---|
152 | bool RadarOverlayElement::calcRight(RadarObject* obj){ |
---|
153 | if((currentDir_.crossProduct(currentOrth_)).dotProduct(obj->pos_ - shipPos_) > 0) |
---|
154 | return true; |
---|
155 | else return false; |
---|
156 | } |
---|
157 | |
---|
158 | //////// RadarObject //////// |
---|
159 | |
---|
160 | int RadarObject::count = 0; // initialize static variable |
---|
161 | |
---|
162 | RadarObject::RadarObject(Ogre::OverlayContainer* container){ |
---|
163 | container_ = container; |
---|
164 | pos_ = Vector3(0.0, 0.0, 0.0); |
---|
165 | init(); |
---|
166 | } |
---|
167 | |
---|
168 | RadarObject::RadarObject(Ogre::OverlayContainer* container, Vector3 pos){ |
---|
169 | container_ = container; |
---|
170 | pos_ = pos; |
---|
171 | init(); |
---|
172 | } |
---|
173 | |
---|
174 | RadarObject::~RadarObject(){} |
---|
175 | |
---|
176 | void RadarObject::init(){ |
---|
177 | next = NULL; |
---|
178 | om = &Ogre::OverlayManager::getSingleton(); |
---|
179 | panel_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", |
---|
180 | "Object"+Ogre::StringConverter::toString(count++))); |
---|
181 | panel_->setMaterialName("Orxonox/RedDot"); |
---|
182 | panel_->setDimensions(5,5); |
---|
183 | panel_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
184 | panel_->show(); |
---|
185 | container_->addChild(panel_); |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | /* my local clipboard... |
---|
190 | COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n"; |
---|
191 | COUT(3) << firstRadarObject_->radius_ << " " << firstRadarObject_->phi_ << std::endl; |
---|
192 | COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n"; |
---|
193 | */ |
---|