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 "DEBUG.h" 00015 00016 //FastEcslent::DistanceMgr::DistanceMgr(){ 00017 //} 00018 00019 FastEcslent::DistanceMgr::DistanceMgr(Engine* eng, Options opts): Mgr (eng){ 00020 options = opts; 00021 //nEnts = 0; 00022 } 00023 00024 void FastEcslent::DistanceMgr::init(){ 00025 //nEnts = engine->entityMgr->nEnts; 00026 //ents = engine->entityMgr->ents; 00027 for(int i = 0; i < MaxEnts; i++){ 00028 collisions[i] = 0; 00029 closestEnemy[i] = -1; 00030 closestEnemyDistance[i] = DBL_MAX; 00031 furthestEnt[i] = -1; 00032 furthestDistance[i] = 0; 00033 00034 } 00035 collisionTotal = 0; 00036 //DEBUG(std::cout << "Distance Manager: " << nEnts << " number of ents in game" << std::endl;) 00037 00038 } 00039 00040 00041 00042 float FastEcslent::DistanceMgr::cpaTime(const Entity *ent1, const Entity *ent2){ 00043 Ogre::Vector3 dv = ent1->vel - ent2->vel; 00044 float dvDotdv = dv.dotProduct(dv); 00045 if (dvDotdv < FastEcslent::EPSILON) { 00046 return 0.0; 00047 } 00048 Ogre::Vector3 w0 = ent1->pos - ent2->pos; 00049 return w0.dotProduct(dv)/dvDotdv; 00050 } 00051 00052 float FastEcslent::DistanceMgr::cpaSquaredDistance(const Entity *ent1, const Entity *ent2){ 00053 float cpaT = cpaTime(ent1, ent2); 00054 Ogre::Vector3 p1 = ent1->pos + (ent1->vel * cpaT); 00055 Ogre::Vector3 p2 = ent2->pos + (ent2->vel * cpaT); 00056 return p1.squaredDistance(p2); 00057 } 00058 00059 void FastEcslent::DistanceMgr::tick (double dt){ 00060 00061 for (int i = 0; i < engine->entityMgr->nEnts; i++){ 00062 ent = engine->entityMgr->ents[i]; 00063 distance[i][i] = 0.0; 00064 distanceVec[i][i] = Ogre::Vector3(0.0, 0.0, 0.0); 00065 for (int j = i+1; j < engine->entityMgr->nEnts; j++){ 00066 other = engine->entityMgr->ents[j]; 00067 distanceVec[i][j] = other->pos - ent->pos; 00068 normalizedDistanceVec[i][j] = distanceVec[i][j].normalisedCopy(); 00069 distanceVec[j][i] = ent->pos - other->pos; 00070 normalizedDistanceVec[j][i] = distanceVec[j][i].normalisedCopy(); 00071 distance[i][j] = distanceVec[i][j].length(); 00072 distance[j][i] = distance[i][j]; 00073 /* if(distance[i][j] < (ent->length + other->length)*2){ 00074 collisions[i]++; 00075 collisions[j]++; 00076 collisionTotal++; 00077 }*/ 00078 //if(distance[i][j] < closestEnemyDistance[i] && other->alive && other->entityId.side != ent->entityId.side){ 00079 if(distance[i][j] < closestEnemyDistance[i] && other->entityState == FastEcslent::ALIVE && other->entityId.side != ent->entityId.side){ 00080 closestEnemyDistance[i] = distance[i][j]; 00081 closestEnemy[i] = j; 00082 00083 } 00084 //if(distance[j][i] < closestEnemyDistance[j] && ent->alive && other->entityId.side != ent->entityId.side){ 00085 if(distance[j][i] < closestEnemyDistance[j] && ent->entityState == FastEcslent::ALIVE && other->entityId.side != ent->entityId.side){ 00086 closestEnemyDistance[j] = distance[j][i]; 00087 closestEnemy[j] = i; 00088 } 00089 if(distance[i][j] > furthestDistance[i]){ 00090 furthestDistance[i] = distance[i][j]; 00091 furthestEnt[i] = j; 00092 } 00093 if(distance[j][i] > furthestDistance[j]){ 00094 furthestDistance[j] = distance[j][i]; 00095 furthestEnt[j] = i; 00096 } 00097 cpa[i][j].cpaTime = cpaTime(ent, other); 00098 cpa[j][i].cpaTime = cpa[i][j].cpaTime; 00099 cpa[i][j].cpaSquaredDistance = cpaSquaredDistance(ent, other); 00100 cpa[j][i].cpaTime = cpa[i][j].cpaSquaredDistance; 00101 00102 } 00103 } 00104 } 00105 00106 void FastEcslent::DistanceMgr::dumpAll (){ 00107 for (int i = 0; i < engine->entityMgr->nEnts; i++){ 00108 dumpOne(i); 00109 } 00110 } 00111 00112 void FastEcslent::DistanceMgr::dumpOne (int index){ 00113 //Entity *ent = engine->entityMgr->ents[index]; 00114 for (int j = 0; j < engine->entityMgr->nEnts; j++){ 00115 //if (j == index) continue; 00116 // DEBUG(std::cout << "Distance from ent: " << ent->entityId.id << " to " << ents[j]->entityId.id << " is " << distance[index][j] << std::endl;) 00117 DEBUG(std::cout << distance[index][j] << ", ";) 00118 } 00119 DEBUG(std::cout << std::endl;) 00120 } 00121 00122