distanceMgr.cpp

Go to the documentation of this file.
00001 /*
00002  * distance.cpp
00003  *
00004  *  Created on: Jan 27, 2012
00005  *      Author: sushil
00006  */
00007 
00008 #include <distanceMgr.h>
00009 //#include <engine.h>
00010 #include <ent.h>
00011 #include <const.h>
00012 
00013 #include <cfloat>
00014 #include <OGRE/OgreVector3.h>
00015 #include "DEBUG.h"
00016 
00017 //FastEcslent::DistanceMgr::DistanceMgr(){
00018 //}
00019 
00020 FastEcslent::DistanceMgr::DistanceMgr(Engine* eng, Options opts): Mgr (eng){
00021         options = opts;
00022         //nEnts = 0;
00023 }
00024 
00025 void FastEcslent::DistanceMgr::init(){
00026         //nEnts = engine->entityMgr->nEnts;
00027         //ents  = engine->entityMgr->ents;
00028         for(int i = 0; i < MaxEnts; i++){
00029                 collisions[i] = 0;
00030                 closestEnemy[i] = -1;
00031                 closestEnemyDistance[i] = DBL_MAX;
00032                 furthestEnt[i] = -1;
00033                 furthestDistance[i] = 0;
00034 
00035         }
00036         collisionTotal = 0;
00037         //DEBUG(std::cout << "Distance Manager: " << nEnts << " number of ents in game" << std::endl;)
00038 
00039 }
00040 
00041 
00042 
00043 float FastEcslent::DistanceMgr::cpaTime(const Entity *ent1, const Entity *ent2){
00044         Ogre::Vector3 dv = ent1->vel - ent2->vel;
00045         float dvDotdv = dv.dotProduct(dv);
00046         if (dvDotdv < FastEcslent::EPSILON) {
00047                 return 0.0;
00048         }
00049         Ogre::Vector3 w0 = ent1->pos - ent2->pos;
00050         return w0.dotProduct(dv)/dvDotdv;
00051 }
00052 
00053 float FastEcslent::DistanceMgr::cpaSquaredDistance(const Entity *ent1, const Entity *ent2){
00054         float cpaT = cpaTime(ent1, ent2);
00055         Ogre::Vector3 p1 = ent1->pos + (ent1->vel * cpaT);
00056         Ogre::Vector3 p2 = ent2->pos + (ent2->vel * cpaT);
00057         return p1.squaredDistance(p2);
00058 }
00059 
00060 bool FastEcslent::DistanceMgr::isColliding(const Entity* ent, double collisionDistThreshold)
00061 {
00062     for(int i = 0; i < engine->entityMgr->nEnts; i++)
00063     {
00064         if (ent->entityId.id != i && distance[ent->entityId.id][i] < collisionDistThreshold)
00065         {
00066             return true;                        
00067         }
00068     }
00069     return false;
00070     //return isColliding(ent1->pos, ent1->length, ent1->width, ent2->pos, ent2->length, ent2->width);    
00071 }
00072 
00073 bool FastEcslent::DistanceMgr::wouldCollide(Ogre::Vector3 pos, double collisionDistThreshold)
00074 {
00075     Ogre::Vector3 dist;
00076     for(int i = 0; i < engine->entityMgr->nEnts; i++)
00077     {
00078         dist = pos - engine->entityMgr->ents[i]->pos;
00079         if (dist.length() < collisionDistThreshold)
00080         {
00081             return true;                        
00082         }
00083     }
00084     return false;
00085     //return isColliding(ent1->pos, ent1->length, ent1->width, ent2->pos, ent2->length, ent2->width);    
00086 }
00087 //bool FastEcslent::DistanceMgr::isColliding(Ogre::Vector3 pos1,float len1, float width1, Ogre::Vector3 pos2,float len2, float width2)
00088 //{
00089 //    Ogre::Vector3 dist = pos1 - pos2;
00090 //    if(Ogre::Math::Abs(dist.x) < (len1+len2) && Ogre::Math::Abs(dist.y) < (width1+width2))
00091 //    {
00092 //        return false;
00093 //    }
00094 //    return true;
00095 //}
00096 
00097 void FastEcslent::DistanceMgr::tick (double dt){
00098 
00099         for (int i = 0; i < engine->entityMgr->nEnts; i++){
00100                 ent = engine->entityMgr->ents[i];
00101                 distance[i][i] = 0.0;
00102                 distanceVec[i][i] = Ogre::Vector3(0.0, 0.0, 0.0);
00103 
00104                 //need calculate closestEnemyDistance each frame
00105                 closestEnemyDistance[i] = DBL_MAX;
00106 
00107                 for (int j = 0; j < engine->entityMgr->nEnts; j++){
00108                         other = engine->entityMgr->ents[j];
00109 
00110                         //ignore self
00111                         if(other->entityId.id == ent->entityId.id){
00112                                 continue;
00113                         }
00114                         //if the saved entity is dead, reset the distance information.
00115                         if(other->entityState != FastEcslent::ALIVE && closestEnemy[i] == j){
00116                                 closestEnemy[i] = -1;
00117                                 closestEnemyDistance[i] = DBL_MAX;
00118                                 furthestEnt[i] = -1;
00119                                 furthestDistance[i] = 0;
00120                                 return;
00121                         }
00122                         distanceVec[i][j] = other->pos - ent->pos;
00123                         normalizedDistanceVec[i][j] = distanceVec[i][j].normalisedCopy();
00124                         distanceVec[j][i] = ent->pos - other->pos;
00125                         normalizedDistanceVec[j][i] = distanceVec[j][i].normalisedCopy();
00126                         distance[i][j]    = distanceVec[i][j].length();
00127                         distance[j][i]    = distance[i][j];
00128 /*                      if(distance[i][j] < (ent->length + other->length)*2){
00129                                 collisions[i]++;
00130                                 collisions[j]++;
00131                                 collisionTotal++;
00132                         }*/
00133                         //if(distance[i][j] < closestEnemyDistance[i] && other->alive && other->entityId.side != ent->entityId.side){
00134                         if(distance[i][j] < closestEnemyDistance[i] && other->entityState == FastEcslent::ALIVE
00135                                         && other->entityId.side != ent->entityId.side && other->entityId.side != NEUTRAL){
00136                                 closestEnemyDistance[i] = distance[i][j];
00137                                 closestEnemy[i] = j;
00138 
00139                         }
00140                         //if(distance[j][i] < closestEnemyDistance[j] && ent->alive && other->entityId.side != ent->entityId.side){
00141                         if(distance[j][i] < closestEnemyDistance[j] && ent->entityState == FastEcslent::ALIVE
00142                                         && other->entityId.side != ent->entityId.side && ent->entityId.side != NEUTRAL){
00143                                 closestEnemyDistance[j] = distance[j][i];
00144                                 closestEnemy[j] = i;
00145                         }
00146                         if(distance[i][j] > furthestDistance[i]){
00147                                 furthestDistance[i] = distance[i][j];
00148                                 furthestEnt[i] = j;
00149                         }
00150                         if(distance[j][i] > furthestDistance[j]){
00151                                 furthestDistance[j] = distance[j][i];
00152                                 furthestEnt[j] = i;
00153                         }
00154                         cpa[i][j].cpaTime = cpaTime(ent, other);
00155                         cpa[j][i].cpaTime = cpa[i][j].cpaTime;
00156                         cpa[i][j].cpaSquaredDistance = cpaSquaredDistance(ent, other);
00157                         cpa[j][i].cpaTime = cpa[i][j].cpaSquaredDistance;
00158 
00159                 }
00160         }
00161 }
00162 
00163 void FastEcslent::DistanceMgr::dumpAll (){
00164         for (int i = 0; i < engine->entityMgr->nEnts; i++){
00165                 dumpOne(i);
00166         }
00167 }
00168 
00169 void FastEcslent::DistanceMgr::dumpOne (int index){
00170         //Entity *ent = engine->entityMgr->ents[index];
00171         for (int j = 0; j < engine->entityMgr->nEnts; j++){
00172                 //if (j == index) continue;
00173 //              DEBUG(std::cout << "Distance from ent: " << ent->entityId.id <<  " to " << ents[j]->entityId.id << " is " << distance[index][j] << std::endl;)
00174                 DEBUG(std::cout << distance[index][j] << ", ";)
00175         }
00176         DEBUG(std::cout << std::endl;)
00177 }
00178 
00179 

Generated on Fri Dec 13 14:54:17 2013 for FastECSLent by  doxygen 1.5.4