[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: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | #include "RadarOverlayElement.h" |
---|
| 29 | |
---|
| 30 | namespace orxonox |
---|
| 31 | { |
---|
[1353] | 32 | |
---|
[1302] | 33 | using namespace Ogre; |
---|
[1283] | 34 | |
---|
[1302] | 35 | RadarOverlayElement::RadarOverlayElement(const String& name):Ogre::PanelOverlayElement(name){ |
---|
| 36 | } |
---|
[1283] | 37 | |
---|
[1302] | 38 | RadarOverlayElement::~RadarOverlayElement(){ |
---|
| 39 | } |
---|
[1283] | 40 | |
---|
[1328] | 41 | void RadarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, Ogre::OverlayContainer* container){ |
---|
[1339] | 42 | // some initial data |
---|
| 43 | om = &Ogre::OverlayManager::getSingleton(); |
---|
[1314] | 44 | dimRel_ = dimRel; |
---|
| 45 | leftRel_ = leftRel; |
---|
| 46 | topRel_ = topRel; |
---|
[1302] | 47 | container_ = container; |
---|
[1339] | 48 | firstRadarObject_ = NULL; |
---|
| 49 | lastRadarObject_ = NULL; |
---|
[1350] | 50 | focus_ = NULL; |
---|
[1283] | 51 | |
---|
[1350] | 52 | // create nav marker ... |
---|
| 53 | navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker")); |
---|
| 54 | navMarker_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
| 55 | navMarker_->setMaterialName("Orxonox/NavMarker"); |
---|
| 56 | navMarker_->setDimensions(16,16); |
---|
| 57 | navMarker_->setPosition(0,386); |
---|
| 58 | navMarker_->hide(); |
---|
| 59 | container_->addChild(navMarker_); |
---|
| 60 | |
---|
[1339] | 61 | // these have to fit the data in the level |
---|
[1302] | 62 | shipPos_ = Vector3(0.0, 0.0, 0.0); |
---|
[1308] | 63 | initialDir_ = Vector3(1.0, 0.0, 0.0); |
---|
[1302] | 64 | currentDir_ = initialDir_; |
---|
[1308] | 65 | initialOrth_ = Vector3(0.0, 0.0, 1.0); |
---|
[1302] | 66 | currentOrth_ = initialOrth_; |
---|
[1343] | 67 | plane = Plane(currentDir_, shipPos_); |
---|
[1310] | 68 | |
---|
[1302] | 69 | setMetricsMode(Ogre::GMM_PIXELS); |
---|
[1310] | 70 | setMaterialName("Orxonox/Radar"); |
---|
[1314] | 71 | resize(); |
---|
[1339] | 72 | |
---|
[1335] | 73 | container_->addChild(this); |
---|
[1283] | 74 | } |
---|
[1302] | 75 | |
---|
[1314] | 76 | void RadarOverlayElement::resize() { |
---|
| 77 | // if window is resized, we must adapt these... |
---|
| 78 | windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); |
---|
| 79 | windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); |
---|
[1342] | 80 | dim_ = (int) (dimRel_*windowH_); |
---|
| 81 | left_ = (int) (leftRel_*windowW_-dim_/2); |
---|
| 82 | top_ = (int) (topRel_*windowH_-dim_/2); |
---|
[1314] | 83 | setPosition(left_, top_); |
---|
| 84 | setDimensions(dim_,dim_); |
---|
| 85 | } |
---|
| 86 | |
---|
[1302] | 87 | void RadarOverlayElement::update() { |
---|
| 88 | shipPos_ = SpaceShip::instance_s->getPosition(); |
---|
[1308] | 89 | currentDir_ = SpaceShip::instance_s->getOrientation()*initialDir_; // according to beni.... |
---|
| 90 | currentOrth_ = SpaceShip::instance_s->getOrientation()*initialOrth_; |
---|
[1343] | 91 | plane = Plane(currentDir_, shipPos_); |
---|
[1310] | 92 | |
---|
[1339] | 93 | RadarObject* ro = firstRadarObject_; |
---|
| 94 | // iterate through all RadarObjects |
---|
| 95 | while(ro != NULL){ |
---|
[1346] | 96 | // calc position on radar... |
---|
[1339] | 97 | ro->radius_ = calcRadius(ro); |
---|
| 98 | ro->phi_ = calcPhi(ro); |
---|
| 99 | ro->right_ = calcRight(ro); |
---|
[1346] | 100 | |
---|
| 101 | // set size to fit distance... |
---|
| 102 | float d = (ro->pos_-shipPos_).length(); |
---|
| 103 | if(d<4000) ro->panel_->setDimensions(4,4); |
---|
| 104 | else if(d<8000) ro->panel_->setDimensions(3,3); |
---|
[1354] | 105 | else if(d<16000) ro->panel_->setDimensions(2,2); |
---|
[1346] | 106 | else ro->panel_->setDimensions(1,1); |
---|
| 107 | |
---|
[1339] | 108 | if (ro->right_){ |
---|
| 109 | ro->panel_->setPosition(sin(ro->phi_)*ro->radius_/ |
---|
| 110 | 3.5*dim_/2+dim_/2+left_-2,-cos(ro->phi_)*ro->radius_/3.5*dim_/2+dim_/2+top_-2); |
---|
| 111 | } |
---|
| 112 | else { |
---|
| 113 | ro->panel_->setPosition(-sin(ro->phi_)*ro->radius_/ |
---|
| 114 | 3.5*dim_/2+dim_/2+left_-2,-cos(ro->phi_)*ro->radius_/3.5*dim_/2+dim_/2+top_-2); |
---|
| 115 | } |
---|
| 116 | ro = ro->next; |
---|
| 117 | } |
---|
[1350] | 118 | updateNavMarker(); |
---|
[1339] | 119 | } |
---|
[1310] | 120 | |
---|
[1350] | 121 | void RadarOverlayElement::updateNavMarker(){ |
---|
| 122 | if(focus_ == NULL) return; |
---|
| 123 | // from the angle we find out where to draw the marker |
---|
| 124 | // and which of the four arrows to take |
---|
[1357] | 125 | float r1 = atan((float)(windowW_)/(float)(windowH_)); |
---|
[1350] | 126 | float phi = focus_->phi_; |
---|
| 127 | if(focus_->right_){ |
---|
| 128 | if(phi<r1){ |
---|
| 129 | navMarker_->setPosition(tan(phi)*windowH_/2+windowW_/2, 0); |
---|
| 130 | navMarker_->setUV(0.5, 0.0, 1.0, 0.5); |
---|
| 131 | } |
---|
| 132 | else if(phi>3.14-r1){ |
---|
| 133 | navMarker_->setPosition(-tan(phi)*windowH_/2+windowW_/2, windowH_-16); |
---|
| 134 | navMarker_->setUV(0.0, 0.5, 0.5, 1.0); |
---|
| 135 | } |
---|
| 136 | else { |
---|
| 137 | navMarker_->setPosition(windowW_-16, -tan((3.14-2*phi)/2)*windowW_/2+windowH_/2); |
---|
| 138 | navMarker_->setUV(0.5, 0.5, 1.0, 1.0); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | else{ |
---|
| 142 | if(phi<r1) { |
---|
| 143 | navMarker_->setPosition(-tan(phi)*windowH_/2+windowW_/2, 0); |
---|
| 144 | navMarker_->setUV(0.5, 0.0, 1.0, 0.5); |
---|
| 145 | } |
---|
| 146 | else if(phi>3.14-r1) { |
---|
| 147 | navMarker_->setPosition(tan(phi)*windowH_/2+windowW_/2, windowH_-16); |
---|
| 148 | navMarker_->setUV(0.0, 0.5, 0.5, 1.0); |
---|
| 149 | } |
---|
| 150 | else { |
---|
| 151 | navMarker_->setPosition(0, -tan((3.14-2*phi)/2)*windowW_/2+windowH_/2); |
---|
| 152 | navMarker_->setUV(0.0, 0.0, 0.5, 0.5); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
[1339] | 157 | void RadarOverlayElement::addObject(Vector3 pos){ |
---|
| 158 | if(firstRadarObject_ == NULL){ |
---|
[1346] | 159 | firstRadarObject_ = new RadarObject(container_, pos); |
---|
[1339] | 160 | lastRadarObject_ = firstRadarObject_; |
---|
[1308] | 161 | } |
---|
[1339] | 162 | else{ |
---|
[1346] | 163 | lastRadarObject_->next = new RadarObject(container_, pos); |
---|
[1339] | 164 | lastRadarObject_ = lastRadarObject_->next; |
---|
[1308] | 165 | } |
---|
[1335] | 166 | } |
---|
[1339] | 167 | |
---|
| 168 | void RadarOverlayElement::listObjects(){ |
---|
| 169 | int i = 0; |
---|
| 170 | RadarObject* ro = firstRadarObject_; |
---|
| 171 | COUT(3) << "List of RadarObjects:\n"; |
---|
| 172 | // iterate through all Radar Objects |
---|
| 173 | while(ro != NULL) { |
---|
| 174 | COUT(3) << i++ << ": " << ro->pos_ << std::endl; |
---|
| 175 | ro = ro->next; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
[1356] | 179 | float RadarOverlayElement::getDist2Focus(){ |
---|
[1358] | 180 | if(focus_ == NULL) return(0.0); |
---|
[1356] | 181 | return((focus_->pos_-shipPos_).length()); |
---|
| 182 | } |
---|
| 183 | |
---|
[1339] | 184 | float RadarOverlayElement::calcRadius(RadarObject* obj){ |
---|
| 185 | return(acos((currentDir_.dotProduct(obj->pos_ - shipPos_))/ |
---|
| 186 | ((obj->pos_ - shipPos_).length()*currentDir_.length()))); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | float RadarOverlayElement::calcPhi(RadarObject* obj){ |
---|
[1343] | 190 | // project difference vector on our plane... |
---|
| 191 | Ogre::Vector3 proj = plane.projectVector(obj->pos_ - shipPos_); |
---|
| 192 | // ...and find out the angle |
---|
| 193 | return(acos((currentOrth_.dotProduct(proj))/ |
---|
| 194 | (currentOrth_.length()*proj.length()))); |
---|
[1339] | 195 | } |
---|
| 196 | |
---|
| 197 | bool RadarOverlayElement::calcRight(RadarObject* obj){ |
---|
| 198 | if((currentDir_.crossProduct(currentOrth_)).dotProduct(obj->pos_ - shipPos_) > 0) |
---|
| 199 | return true; |
---|
| 200 | else return false; |
---|
| 201 | } |
---|
[1350] | 202 | |
---|
[1387] | 203 | void RadarOverlayElement::cycleFocus(){ |
---|
| 204 | if(focus_ == NULL){ |
---|
| 205 | focus_ = firstRadarObject_; |
---|
[1353] | 206 | } |
---|
| 207 | else{ |
---|
[1387] | 208 | focus_->panel_->setMaterialName("Orxonox/RedDot"); |
---|
| 209 | focus_ = focus_->next; |
---|
[1353] | 210 | } |
---|
| 211 | |
---|
[1387] | 212 | if(focus_ == NULL){ |
---|
| 213 | navMarker_->hide(); |
---|
[1353] | 214 | } |
---|
| 215 | else{ |
---|
[1387] | 216 | navMarker_->show(); |
---|
| 217 | focus_->panel_->setMaterialName("Orxonox/WhiteDot"); |
---|
[1353] | 218 | } |
---|
[1350] | 219 | } |
---|
[1283] | 220 | } |
---|
| 221 | |
---|
[1335] | 222 | /* my local clipboard... |
---|
| 223 | COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n"; |
---|
[1339] | 224 | COUT(3) << firstRadarObject_->radius_ << " " << firstRadarObject_->phi_ << std::endl; |
---|
[1335] | 225 | COUT(3) << "WWWWWWWWWWWWWWWWWWWWWWWWWWWW\n"; |
---|
| 226 | */ |
---|