| 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 | * Felix Schulthess |
|---|
| 25 | * |
|---|
| 26 | */ |
|---|
| 27 | #include <string.h> |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | #include <string.h> |
|---|
| 31 | #include <OgreOverlayManager.h> |
|---|
| 32 | #include <OgreStringConverter.h> |
|---|
| 33 | #include <OgrePanelOverlayElement.h> |
|---|
| 34 | |
|---|
| 35 | #include "GraphicsEngine.h" |
|---|
| 36 | #include "core/Tickable.h" |
|---|
| 37 | #include "core/ConsoleCommand.h" |
|---|
| 38 | #include "objects/SpaceShip.h" |
|---|
| 39 | #include "HUD.h" |
|---|
| 40 | #include "RadarOverlayElement.h" |
|---|
| 41 | |
|---|
| 42 | namespace orxonox |
|---|
| 43 | { |
|---|
| 44 | |
|---|
| 45 | using namespace Ogre; |
|---|
| 46 | |
|---|
| 47 | RadarOverlayElement::RadarOverlayElement(const String& name):PanelOverlayElement(name){ |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | RadarOverlayElement::~RadarOverlayElement(){ |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | void RadarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, OverlayContainer* container){ |
|---|
| 54 | // some initial data |
|---|
| 55 | om = &OverlayManager::getSingleton(); |
|---|
| 56 | dimRel_ = dimRel; |
|---|
| 57 | leftRel_ = leftRel; |
|---|
| 58 | topRel_ = topRel; |
|---|
| 59 | container_ = container; |
|---|
| 60 | |
|---|
| 61 | setMetricsMode(GMM_PIXELS); |
|---|
| 62 | setMaterialName("Orxonox/Radar"); |
|---|
| 63 | resize(); |
|---|
| 64 | |
|---|
| 65 | container_->addChild(this); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | void RadarOverlayElement::resize() { |
|---|
| 69 | // if window is resized, we must adapt these... |
|---|
| 70 | windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); |
|---|
| 71 | windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); |
|---|
| 72 | dim_ = (int) (dimRel_*windowH_); |
|---|
| 73 | left_ = (int) (leftRel_*windowW_-dim_/2); |
|---|
| 74 | top_ = (int) (topRel_*windowH_-dim_/2); |
|---|
| 75 | setPosition(left_, top_); |
|---|
| 76 | setDimensions(dim_,dim_); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | void RadarOverlayElement::update() { |
|---|
| 80 | shipPos_ = SpaceShip::getLocalShip()->getPosition(); |
|---|
| 81 | currentDir_ = SpaceShip::getLocalShip()->getDir(); |
|---|
| 82 | currentOrth_ = SpaceShip::getLocalShip()->getOrth(); |
|---|
| 83 | RadarObject* ro = HUD::getSingleton().getFirstRadarObject(); |
|---|
| 84 | // iterate through all RadarObjects |
|---|
| 85 | while(ro != NULL){ |
|---|
| 86 | // calc position on radar... |
|---|
| 87 | float radius = calcRadius(shipPos_, currentDir_, currentOrth_, ro); |
|---|
| 88 | float phi = calcPhi(shipPos_, currentDir_, currentOrth_, ro); |
|---|
| 89 | bool right = calcRight(shipPos_, currentDir_, currentOrth_, ro); |
|---|
| 90 | |
|---|
| 91 | // set size to fit distance... |
|---|
| 92 | float d = (ro->pos_-shipPos_).length(); |
|---|
| 93 | if(d<4000) ro->panel_->setDimensions(4,4); |
|---|
| 94 | else if(d<8000) ro->panel_->setDimensions(3,3); |
|---|
| 95 | else if(d<16000) ro->panel_->setDimensions(2,2); |
|---|
| 96 | else ro->panel_->setDimensions(1,1); |
|---|
| 97 | |
|---|
| 98 | if (right){ |
|---|
| 99 | ro->panel_->setPosition(sin(phi)*radius/ |
|---|
| 100 | 3.5*dim_/2+dim_/2+left_-2,-cos(phi)*radius/3.5*dim_/2+dim_/2+top_-2); |
|---|
| 101 | } |
|---|
| 102 | else { |
|---|
| 103 | ro->panel_->setPosition(-sin(phi)*radius/ |
|---|
| 104 | 3.5*dim_/2+dim_/2+left_-2,-cos(phi)*radius/3.5*dim_/2+dim_/2+top_-2); |
|---|
| 105 | } |
|---|
| 106 | ro = ro->next; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | void RadarOverlayElement::listObjects(){ |
|---|
| 111 | int i = 0; |
|---|
| 112 | RadarObject* ro = HUD::getSingleton().getFirstRadarObject(); |
|---|
| 113 | COUT(3) << "List of RadarObjects:\n"; |
|---|
| 114 | // iterate through all Radar Objects |
|---|
| 115 | while(ro != NULL) { |
|---|
| 116 | COUT(3) << i++ << ": " << ro->pos_ << std::endl; |
|---|
| 117 | ro = ro->next; |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | float RadarOverlayElement::calcRadius(Vector3 pos, Vector3 dir, Vector3 orth, RadarObject* obj){ |
|---|
| 122 | return(acos((dir.dotProduct(obj->pos_ - pos))/ |
|---|
| 123 | ((obj->pos_ - pos).length()*dir.length()))); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | float RadarOverlayElement::calcPhi(Vector3 pos, Vector3 dir, Vector3 orth, RadarObject* obj){ |
|---|
| 127 | // project difference vector on our plane... |
|---|
| 128 | Vector3 proj = Plane(dir, pos).projectVector(obj->pos_ - pos); |
|---|
| 129 | // ...and find out the angle |
|---|
| 130 | return(acos((orth.dotProduct(proj))/ |
|---|
| 131 | (orth.length()*proj.length()))); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | bool RadarOverlayElement::calcRight(Vector3 pos, Vector3 dir, Vector3 orth, RadarObject* obj){ |
|---|
| 135 | if((dir.crossProduct(orth)).dotProduct(obj->pos_ - pos) > 0) |
|---|
| 136 | return true; |
|---|
| 137 | else return false; |
|---|
| 138 | } |
|---|
| 139 | } |
|---|