Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 8, 2008, 5:46:52 AM (16 years ago)
Author:
landauf
Message:
  • several small changes in most of the HUD classes (code cleanup): removed obsolete variables, privatized all member variables, removed resizing functioncalls from tick, destroying overlayelements, added some const qualifiers.
  • moved calculation functions for RadarObject-position to Math.h and changed the phi/right/radius format to Vector2. the functions are used too by SpaceShipAI.
  • cycleNavigationFocus takes the nearest object if focus was NULL
  • BarOverlayElement works in both directions (left to right and right to left)
  • fixed bug causing SpaceShipAI to not stop shooting when losing target - this also speeds up orxonox a lot, because there are less projectiles

####################################

!! UPDATE YOUR MEDIA REPOSITORY !!

####################################
…or the BarOverlayElement will look strange

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/orxonox/hud/Navigation.cc

    r1562 r1564  
    4141#include "HUD.h"
    4242#include "core/Debug.h"
     43#include "util/Math.h"
    4344
    4445namespace orxonox
     
    5859    }
    5960
     61    Navigation::~Navigation()
     62    {
     63        OverlayManager::getSingleton().destroyOverlayElement(this->navText_);
     64        OverlayManager::getSingleton().destroyOverlayElement(this->navMarker_);
     65        OverlayManager::getSingleton().destroyOverlayElement(this->aimMarker_);
     66    }
     67
    6068    void Navigation::init(){
    61         om = &OverlayManager::getSingleton();
    62         navCam_ = NULL;
    6369        // create nav text
    64         navText_ = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "navText"));
     70        navText_ = static_cast<TextAreaOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("TextArea", "navText"));
    6571        navText_->show();
    6672        navText_->setMetricsMode(Ogre::GMM_PIXELS);
     
    7076        navText_->setCharHeight(20);
    7177        navText_->setCaption("");
     78        navText_->hide();
    7279        container_->addChild(navText_);
    7380
    7481
    7582        // create nav marker ...
    76         navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker"));
     83        navMarker_ = static_cast<PanelOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("Panel", "NavMarker"));
     84        aimMarker_ = static_cast<PanelOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("Panel", "aimMarker"));
    7785        navMarker_->setMetricsMode(GMM_PIXELS);
     86        aimMarker_->setMetricsMode(GMM_PIXELS);
    7887        navMarker_->hide();
    79         navText_->hide();
     88        aimMarker_->hide();
    8089        container_->addChild(navMarker_);
     90        container_->addChild(aimMarker_);
    8191    }
    8292
    8393    void Navigation::update(){
    84         if(focus_ == NULL) return;
    85         navCamPos_ = SpaceShip::getLocalShip()->getPosition();
    86         currentDir_ = SpaceShip::getLocalShip()->getDir();
    87         currentOrth_ = SpaceShip::getLocalShip()->getOrth();
    88 
    89         windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
    90         windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
     94        if (!focus_)
     95            return;
     96
    9197        updateMarker();
    9298    }
    9399
    94100    void Navigation::updateMarker(){
     101        int windowW = GraphicsEngine::getSingleton().getWindowWidth();
     102        int windowH = GraphicsEngine::getSingleton().getWindowHeight();
     103
    95104        // set text
    96105        int dist = (int) getDist2Focus()/100;
    97106        navText_->setCaption(Ogre::StringConverter::toString(dist));
    98107
    99         if(navCam_ == NULL) navCam_ = SpaceShip::getLocalShip()->getCamera()->cam_;
    100108        Vector3 pos = focus_->getPosition();
     109        Ogre::Camera* navCam = SpaceShip::getLocalShip()->getCamera()->cam_;
    101110        // transform to screen coordinates
    102         pos = navCam_->getProjectionMatrix()*navCam_->getViewMatrix()*pos;
     111        pos = navCam->getProjectionMatrix() * navCam->getViewMatrix() * pos;
     112
    103113        float xPosRel = 0.5*pos.x+0.5;
    104114        float yPosRel = 1-(0.5*pos.y+0.5);
    105         int xPos = (int) (xPosRel*windowW_);
    106         int yPos = (int) (yPosRel*windowH_);
    107         int xFromCenter = xPos-windowW_/2;
    108         int yFromCenter = yPos-windowH_/2;
     115        int xPos = (int) (xPosRel*windowW);
     116        int yPos = (int) (yPosRel*windowH);
     117        int xFromCenter = xPos-windowW/2;
     118        int yFromCenter = yPos-windowH/2;
     119
    109120        // is object in view?
    110         float radius = RadarOverlayElement::calcRadius(navCamPos_, currentDir_, currentOrth_, focus_);
    111         bool isRight = (currentDir_.crossProduct(currentOrth_)).dotProduct(focus_->getPosition() - navCamPos_)>0;
    112         bool isAbove = currentOrth_.dotProduct(focus_->getPosition() - navCamPos_)>0;
     121        Vector3 navCamPos = SpaceShip::getLocalShip()->getPosition();
     122        Vector3 currentDir = SpaceShip::getLocalShip()->getDir();
     123        Vector3 currentOrth = SpaceShip::getLocalShip()->getOrth();
     124        float radius = getAngle(navCamPos, currentDir, focus_->getPosition());
     125        bool isRight = (currentDir.crossProduct(currentOrth)).dotProduct(focus_->getPosition() - navCamPos)>0;
     126        bool isAbove = currentOrth.dotProduct(focus_->getPosition() - navCamPos)>0;
    113127        bool outOfView = (xPosRel<0 || xPosRel>1 || yPosRel<0 || yPosRel>1);
    114128        // if object is behind us, it is out of view anyway:
    115         if(!outOfView && radius>3.14/2) outOfView = true;
     129        if(!outOfView && radius > Ogre::Math::PI / 2) outOfView = true;
    116130
    117131        if(outOfView){
     
    119133            navMarker_->setMaterialName("Orxonox/NavArrows");
    120134            navMarker_->setDimensions(16,16);
    121             float phiUpperCorner = atan((float)(windowW_)/(float)(windowH_));
     135            float phiUpperCorner = atan((float)(windowW)/(float)(windowH));
    122136            // from the angle we find out on which edge to draw the marker
    123137            // and which of the four arrows to take
     
    128142                if(-phiNav<phiUpperCorner){
    129143                    //COUT(3) << "arrow up\n";
    130                     navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0);
     144                    navMarker_->setPosition(-tan(phiNav)*windowH/2+windowW/2, 0);
    131145                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
    132146                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
     
    135149                else {
    136150                    //COUT(3) << "arrow right\n";
    137                     navMarker_->setPosition(windowW_-16, tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
     151                    navMarker_->setPosition(windowW-16, tan((3.14-2*phiNav)/2)*windowW/2+windowH/2);
    138152                    navMarker_->setUV(0.5, 0.5, 1.0, 1.0);
    139153                    navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth());
     
    145159                if(phiNav<phiUpperCorner) {
    146160                    //COUT(3) << "arrow down\n";
    147                     navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16);
     161                    navMarker_->setPosition(tan(phiNav)*windowH/2+windowW/2, windowH-16);
    148162                    navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
    149163                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
     
    152166                else {
    153167                    //COUT(3) << "arrow right\n";
    154                     navMarker_->setPosition(windowW_-16, tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
     168                    navMarker_->setPosition(windowW-16, tan((3.14-2*phiNav)/2)*windowW/2+windowH/2);
    155169                    navMarker_->setUV(0.5, 0.5, 1.0, 1.0);
    156170                    navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth());
     
    162176                if(phiNav<phiUpperCorner){
    163177                    //COUT(3) << "arrow up\n";
    164                     navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0);
     178                    navMarker_->setPosition(-tan(phiNav)*windowH/2+windowW/2, 0);
    165179                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
    166180                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
     
    169183                else {
    170184                    //COUT(3) << "arrow left\n";
    171                     navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
     185                    navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW/2+windowH/2);
    172186                    navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
    173187                    navText_->setLeft(navMarker_->getWidth());
     
    179193                if(phiNav>-phiUpperCorner) {
    180194                    //COUT(3) << "arrow down\n";
    181                     navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16);
     195                    navMarker_->setPosition(tan(phiNav)*windowH/2+windowW/2, windowH-16);
    182196                    navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
    183197                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
     
    186200                else {
    187201                    //COUT(3) << "arrow left\n";
    188                     navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
     202                    navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW/2+windowH/2);
    189203                    navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
    190204                    navText_->setLeft(navMarker_->getWidth());
     
    205219    void Navigation::cycleFocus(){
    206220        if(focus_ == NULL){
    207             it_ = HUD::getSingleton().roSet.begin();
    208             focus_ = *it_;
    209             ++it_;
     221            // Get closest object
     222            float distance = (unsigned int) -1;
     223            Vector3 shipPos = SpaceShip::getLocalShip()->getPosition();
     224            it_ = HUD::getSingleton().getRadarObjects().begin();
     225
     226            for (std::list<RadarObject*>::iterator it = HUD::getSingleton().getRadarObjects().begin(); it != HUD::getSingleton().getRadarObjects().end(); ++it)
     227            {
     228                float newdist = (*it)->getPosition().squaredDistance(shipPos);
     229                if (newdist < distance)
     230                {
     231                    distance = newdist;
     232                    it_ = it;
     233                }
     234            }
     235
     236            if (it_ != HUD::getSingleton().getRadarObjects().end())
     237            {
     238                focus_ = *it_;
     239
     240                // move the focused object to the begin of the list, so we will iterate through all other objects when cycling
     241                HUD::getSingleton().getRadarObjects().erase(it_);
     242                HUD::getSingleton().getRadarObjects().insert(HUD::getSingleton().getRadarObjects().begin(), focus_);
     243                it_ = HUD::getSingleton().getRadarObjects().begin();
     244                ++it_;
     245            }
    210246        }
    211247        else{
    212248            focus_->resetMaterial();
    213             if(it_ != HUD::getSingleton().roSet.end()){
     249            if(it_ != HUD::getSingleton().getRadarObjects().end()){
    214250                focus_ = *it_;
    215251                ++it_;
     
    217253            else focus_ = NULL;
    218254        }
     255        updateFocus();
     256    }
     257
     258    void Navigation::updateFocus(){
    219259        if(focus_ == NULL){
    220260            navMarker_->hide();
     
    228268    }
    229269
    230     float Navigation::getDist2Focus(){
     270    float Navigation::getDist2Focus() const {
    231271        if(focus_ == NULL) return(0.0);
    232272        return((focus_->getPosition()-SpaceShip::getLocalShip()->getPosition()).length());
Note: See TracChangeset for help on using the changeset viewer.