| [1495] | 1 | /* | 
|---|
 | 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
 | 3 |  *                    > www.orxonox.net < | 
|---|
 | 4 |  * | 
|---|
 | 5 |  * | 
|---|
 | 6 |  *   License notice: | 
|---|
 | 7 |  * | 
|---|
 | 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
 | 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
 | 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
 | 11 |  *   of the License, or (at your option) any later version. | 
|---|
 | 12 |  * | 
|---|
 | 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
 | 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
 | 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
 | 16 |  *   GNU General Public License for more details. | 
|---|
 | 17 |  * | 
|---|
 | 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
 | 19 |  *   along with this program; if not, write to the Free Software | 
|---|
 | 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
 | 21 |  * | 
|---|
| [1454] | 22 |  *   Author: | 
|---|
 | 23 |  *      Yuning Chai | 
|---|
 | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      Felix Schulthess | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
| [1283] | 28 |  | 
|---|
| [1373] | 29 | #include "OrxonoxStableHeaders.h" | 
|---|
| [1283] | 30 | #include "RadarOverlayElement.h" | 
|---|
 | 31 |  | 
|---|
| [1410] | 32 | #include <string> | 
|---|
| [1406] | 33 | #include <OgreOverlayManager.h> | 
|---|
 | 34 | #include <OgreStringConverter.h> | 
|---|
 | 35 |  | 
|---|
 | 36 | #include "GraphicsEngine.h" | 
|---|
 | 37 | #include "core/Tickable.h" | 
|---|
 | 38 | #include "core/ConsoleCommand.h" | 
|---|
 | 39 | #include "objects/SpaceShip.h" | 
|---|
| [1410] | 40 | #include "RadarObject.h" | 
|---|
| [1406] | 41 | #include "HUD.h" | 
|---|
 | 42 |  | 
|---|
| [1283] | 43 | namespace orxonox | 
|---|
 | 44 | { | 
|---|
| [1302] | 45 |     using namespace Ogre; | 
|---|
| [1283] | 46 |  | 
|---|
| [1406] | 47 |     RadarOverlayElement::RadarOverlayElement(const String& name):PanelOverlayElement(name){ | 
|---|
| [1302] | 48 |     } | 
|---|
| [1283] | 49 |  | 
|---|
| [1302] | 50 |     RadarOverlayElement::~RadarOverlayElement(){ | 
|---|
 | 51 |     } | 
|---|
| [1283] | 52 |  | 
|---|
| [1406] | 53 |     void RadarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, OverlayContainer* container){ | 
|---|
| [1339] | 54 |         // some initial data | 
|---|
| [1410] | 55 |         om = &OverlayManager::getSingleton(); | 
|---|
| [1314] | 56 |         dimRel_ = dimRel; | 
|---|
 | 57 |         leftRel_ = leftRel; | 
|---|
 | 58 |         topRel_ = topRel; | 
|---|
| [1302] | 59 |         container_ = container; | 
|---|
| [1283] | 60 |  | 
|---|
| [1406] | 61 |         setMetricsMode(GMM_PIXELS); | 
|---|
| [1310] | 62 |         setMaterialName("Orxonox/Radar"); | 
|---|
| [1314] | 63 |         resize(); | 
|---|
| [1339] | 64 |  | 
|---|
| [1335] | 65 |         container_->addChild(this); | 
|---|
| [1283] | 66 |     } | 
|---|
| [1302] | 67 |  | 
|---|
| [1314] | 68 |     void RadarOverlayElement::resize() { | 
|---|
 | 69 |         // if window is resized, we must adapt these... | 
|---|
 | 70 |         windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); | 
|---|
 | 71 |         windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); | 
|---|
| [1342] | 72 |         dim_ = (int) (dimRel_*windowH_); | 
|---|
 | 73 |         left_ = (int) (leftRel_*windowW_-dim_/2); | 
|---|
 | 74 |         top_ = (int) (topRel_*windowH_-dim_/2); | 
|---|
| [1314] | 75 |         setPosition(left_, top_); | 
|---|
 | 76 |         setDimensions(dim_,dim_); | 
|---|
 | 77 |     } | 
|---|
 | 78 |  | 
|---|
| [1302] | 79 |     void RadarOverlayElement::update() { | 
|---|
| [1384] | 80 |         shipPos_ = SpaceShip::getLocalShip()->getPosition(); | 
|---|
| [1406] | 81 |         currentDir_ = SpaceShip::getLocalShip()->getDir(); | 
|---|
| [1410] | 82 |         currentOrth_ = SpaceShip::getLocalShip()->getOrth(); | 
|---|
| [1339] | 83 |         // iterate through all RadarObjects | 
|---|
| [1456] | 84 |         for(std::set<RadarObject*>::iterator it=HUD::getSingleton().roSet.begin(); it!=HUD::getSingleton().roSet.end(); it++){ | 
|---|
| [1410] | 85 |         // calc position on radar... | 
|---|
| [1456] | 86 |             float radius = calcRadius(shipPos_, currentDir_, currentOrth_, (*it)); | 
|---|
 | 87 |             float phi = calcPhi(shipPos_, currentDir_, currentOrth_, (*it)); | 
|---|
 | 88 |             bool right = calcRight(shipPos_, currentDir_, currentOrth_, (*it)); | 
|---|
| [1346] | 89 |  | 
|---|
 | 90 |             // set size to fit distance... | 
|---|
| [1456] | 91 |             float d = ((*it)->getPosition()-shipPos_).length(); | 
|---|
| [1463] | 92 |             if(d<10000) (*it)->panel_->setDimensions(4,4); | 
|---|
 | 93 |             else if(d<20000) (*it)->panel_->setDimensions(3,3); | 
|---|
 | 94 |             else (*it)->panel_->setDimensions(2,2); | 
|---|
| [1346] | 95 |  | 
|---|
| [1406] | 96 |             if (right){ | 
|---|
| [1456] | 97 |                 (*it)->panel_->setPosition(sin(phi)*radius/ | 
|---|
| [1406] | 98 |                     3.5*dim_/2+dim_/2+left_-2,-cos(phi)*radius/3.5*dim_/2+dim_/2+top_-2); | 
|---|
| [1339] | 99 |             } | 
|---|
 | 100 |             else { | 
|---|
| [1456] | 101 |                 (*it)->panel_->setPosition(-sin(phi)*radius/ | 
|---|
| [1406] | 102 |                     3.5*dim_/2+dim_/2+left_-2,-cos(phi)*radius/3.5*dim_/2+dim_/2+top_-2); | 
|---|
| [1339] | 103 |             } | 
|---|
| [1410] | 104 |         } | 
|---|
| [1339] | 105 |     } | 
|---|
| [1310] | 106 |  | 
|---|
| [1410] | 107 |     void RadarOverlayElement::listObjects(){ | 
|---|
 | 108 |         int i = 0; | 
|---|
 | 109 |         COUT(3) << "List of RadarObjects:\n"; | 
|---|
 | 110 |         // iterate through all Radar Objects | 
|---|
| [1456] | 111 |         for(std::set<RadarObject*>::iterator it=HUD::getSingleton().roSet.begin(); it!=HUD::getSingleton().roSet.end(); it++){ | 
|---|
 | 112 |             COUT(3) << i++ << ": " << (*it)->getPosition() << std::endl; | 
|---|
| [1410] | 113 |         } | 
|---|
 | 114 |     } | 
|---|
| [1339] | 115 |  | 
|---|
| [1410] | 116 |     float RadarOverlayElement::calcRadius(Vector3 pos, Vector3 dir, Vector3 orth, RadarObject* obj){ | 
|---|
| [1450] | 117 |         return(acos((dir.dotProduct(obj->getPosition() - pos))/ | 
|---|
 | 118 |         ((obj->getPosition() - pos).length()*dir.length()))); | 
|---|
| [1410] | 119 |     } | 
|---|
| [1356] | 120 |  | 
|---|
| [1410] | 121 |     float RadarOverlayElement::calcPhi(Vector3 pos, Vector3 dir, Vector3 orth, RadarObject* obj){ | 
|---|
 | 122 |         // project difference vector on our plane... | 
|---|
| [1450] | 123 |         Vector3 proj = Plane(dir, pos).projectVector(obj->getPosition() - pos); | 
|---|
| [1410] | 124 |         // ...and find out the angle | 
|---|
 | 125 |         return(acos((orth.dotProduct(proj))/ | 
|---|
 | 126 |               (orth.length()*proj.length()))); | 
|---|
 | 127 |     } | 
|---|
| [1339] | 128 |  | 
|---|
| [1410] | 129 |     bool RadarOverlayElement::calcRight(Vector3 pos, Vector3 dir, Vector3 orth, RadarObject* obj){ | 
|---|
| [1450] | 130 |         if((dir.crossProduct(orth)).dotProduct(obj->getPosition() - pos) > 0) | 
|---|
| [1410] | 131 |             return true; | 
|---|
| [1339] | 132 |         else return false; | 
|---|
| [1410] | 133 |     } | 
|---|
| [1283] | 134 | } | 
|---|