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 | |
---|
72 | setMetricsMode(Ogre::GMM_PIXELS); |
---|
73 | setMaterialName("Orxonox/Radar"); |
---|
74 | resize(); |
---|
75 | |
---|
76 | container_->addChild(this); |
---|
77 | } |
---|
78 | |
---|
79 | void RadarOverlayElement::resize() { |
---|
80 | // if window is resized, we must adapt these... |
---|
81 | windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); |
---|
82 | windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); |
---|
83 | dim_ = (int) (dimRel_*windowH_); |
---|
84 | left_ = (int) (leftRel_*windowW_-dim_/2); |
---|
85 | top_ = (int) (topRel_*windowH_-dim_/2); |
---|
86 | setPosition(left_, top_); |
---|
87 | setDimensions(dim_,dim_); |
---|
88 | } |
---|
89 | |
---|
90 | void RadarOverlayElement::update() { |
---|
91 | shipPos_ = SpaceShip::instance_s->getPosition(); |
---|
92 | currentDir_ = SpaceShip::instance_s->getOrientation()*initialDir_; // according to beni.... |
---|
93 | currentOrth_ = SpaceShip::instance_s->getOrientation()*initialOrth_; |
---|
94 | |
---|
95 | RadarObject* ro = firstRadarObject_; |
---|
96 | // iterate through all RadarObjects |
---|
97 | while(ro != NULL){ |
---|
98 | ro->radius_ = calcRadius(ro); |
---|
99 | ro->phi_ = calcPhi(ro); |
---|
100 | ro->right_ = calcRight(ro); |
---|
101 | if (ro->right_){ |
---|
102 | ro->panel_->setPosition(sin(ro->phi_)*ro->radius_/ |
---|
103 | 3.5*dim_/2+dim_/2+left_-2,-cos(ro->phi_)*ro->radius_/3.5*dim_/2+dim_/2+top_-2); |
---|
104 | } |
---|
105 | else { |
---|
106 | ro->panel_->setPosition(-sin(ro->phi_)*ro->radius_/ |
---|
107 | 3.5*dim_/2+dim_/2+left_-2,-cos(ro->phi_)*ro->radius_/3.5*dim_/2+dim_/2+top_-2); |
---|
108 | } |
---|
109 | ro = ro->next; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | void RadarOverlayElement::addObject(Vector3 pos){ |
---|
114 | if(firstRadarObject_ == NULL){ |
---|
115 | firstRadarObject_ = new RadarObject(container_); |
---|
116 | firstRadarObject_->pos_ = pos; |
---|
117 | lastRadarObject_ = firstRadarObject_; |
---|
118 | } |
---|
119 | else{ |
---|
120 | lastRadarObject_->next = new RadarObject(container_); |
---|
121 | lastRadarObject_->next->pos_ = pos; |
---|
122 | lastRadarObject_ = lastRadarObject_->next; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | void RadarOverlayElement::listObjects(){ |
---|
127 | int i = 0; |
---|
128 | RadarObject* ro = firstRadarObject_; |
---|
129 | COUT(3) << "List of RadarObjects:\n"; |
---|
130 | // iterate through all Radar Objects |
---|
131 | while(ro != NULL) { |
---|
132 | COUT(3) << i++ << ": " << ro->pos_ << std::endl; |
---|
133 | ro = ro->next; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | float RadarOverlayElement::calcRadius(RadarObject* obj){ |
---|
138 | return(acos((currentDir_.dotProduct(obj->pos_ - shipPos_))/ |
---|
139 | ((obj->pos_ - shipPos_).length()*currentDir_.length()))); |
---|
140 | } |
---|
141 | |
---|
142 | float RadarOverlayElement::calcPhi(RadarObject* obj){ |
---|
143 | return(acos((currentOrth_.dotProduct(firstRadarObject_->pos_ - shipPos_))/ |
---|
144 | ((firstRadarObject_->pos_ - shipPos_).length()*currentOrth_.length()))); |
---|
145 | } |
---|
146 | |
---|
147 | bool RadarOverlayElement::calcRight(RadarObject* obj){ |
---|
148 | if((currentDir_.crossProduct(currentOrth_)).dotProduct(obj->pos_ - shipPos_) > 0) |
---|
149 | return true; |
---|
150 | else return false; |
---|
151 | } |
---|
152 | |
---|
153 | //////// RadarObject //////// |
---|
154 | |
---|
155 | int RadarObject::count = 0; // initialize static variable |
---|
156 | |
---|
157 | RadarObject::RadarObject(Ogre::OverlayContainer* container){ |
---|
158 | container_ = container; |
---|
159 | pos_ = Vector3(0.0, 0.0, 0.0); |
---|
160 | init(); |
---|
161 | } |
---|
162 | |
---|
163 | RadarObject::RadarObject(Ogre::OverlayContainer* container, Vector3 pos){ |
---|
164 | container_ = container; |
---|
165 | pos_ = pos; |
---|
166 | init(); |
---|
167 | } |
---|
168 | |
---|
169 | RadarObject::~RadarObject(){} |
---|
170 | |
---|
171 | void RadarObject::init(){ |
---|
172 | next = NULL; |
---|
173 | om = &Ogre::OverlayManager::getSingleton(); |
---|
174 | panel_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", |
---|
175 | "Object"+Ogre::StringConverter::toString(count++))); |
---|
176 | panel_->setMaterialName("Orxonox/RedDot"); |
---|
177 | panel_->setDimensions(5,5); |
---|
178 | panel_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
179 | panel_->show(); |
---|
180 | container_->addChild(panel_); |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | /* my local clipboard... |
---|
185 | COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n"; |
---|
186 | COUT(3) << firstRadarObject_->radius_ << " " << firstRadarObject_->phi_ << std::endl; |
---|
187 | COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n"; |
---|
188 | */ |
---|