00001
00002
00003
00004
00005
00006
00007
00008 #include <distanceMgr.h>
00009
00010 #include <ent.h>
00011 #include <const.h>
00012
00013 #include <cfloat>
00014 #include <OGRE/OgreVector3.h>
00015 #include "DEBUG.h"
00016
00017
00018
00019
00020 FastEcslent::DistanceMgr::DistanceMgr(Engine* eng, Options opts): Mgr (eng){
00021 options = opts;
00022
00023 }
00024
00025 void FastEcslent::DistanceMgr::init(){
00026
00027
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
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
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
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
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
00105 closestEnemyDistance[i] = DBL_MAX;
00106
00107 for (int j = 0; j < engine->entityMgr->nEnts; j++){
00108 other = engine->entityMgr->ents[j];
00109
00110
00111 if(other->entityId.id == ent->entityId.id){
00112 continue;
00113 }
00114
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
00129
00130
00131
00132
00133
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
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
00171 for (int j = 0; j < engine->entityMgr->nEnts; j++){
00172
00173
00174 DEBUG(std::cout << distance[index][j] << ", ";)
00175 }
00176 DEBUG(std::cout << std::endl;)
00177 }
00178
00179