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