Collsn.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. //******************************************************************************
  2. // collsn.cpp - This file contains the Collision Detection System
  3. //
  4. // MechCommander 2
  5. //
  6. //---------------------------------------------------------------------------//
  7. // Copyright (C) Microsoft Corporation. All rights reserved. //
  8. //===========================================================================//
  9. // Include Files
  10. #ifndef MCLIB_H
  11. #include "mclib.h"
  12. #endif
  13. #ifndef COLLSN_H
  14. #include "collsn.h"
  15. #endif
  16. #ifndef GAMEOBJ_H
  17. #include "gameobj.h"
  18. #endif
  19. #ifndef OBJTYPE_H
  20. #include "objtype.h"
  21. #endif
  22. #ifndef OBJMGR_H
  23. #include "objmgr.h"
  24. #endif
  25. #ifndef MOVE_H
  26. #include "move.h"
  27. #endif
  28. //------------------------------------------------------------------------------
  29. // Static globals
  30. //CollisionSystem *collisionSystem = NULL;
  31. GlobalCollisionAlert *globalCollisionAlert = NULL;
  32. UserHeapPtr CollisionSystem::collisionHeap = NULL;
  33. unsigned long CollisionSystem::maxObjects = 0;
  34. unsigned long CollisionSystem::gridRadius = 0;
  35. unsigned long CollisionSystem::xGridSize = 0;
  36. unsigned long CollisionSystem::yGridSize = 0;
  37. unsigned long CollisionSystem::maxCollisions = 0;
  38. //------------------------------------------------------------------------------
  39. // class CollisionGrid
  40. //------------------------------------------------------------------------------
  41. long GlobalCollisionAlert::init (unsigned long maxCollisionAlerts)
  42. {
  43. maxAlerts = maxCollisionAlerts;
  44. collisionAlerts = (CollisionAlertRecordPtr)systemHeap->Malloc(sizeof(CollisionAlertRecord) * maxAlerts);
  45. gosASSERT(collisionAlerts != NULL);
  46. purgeRecords();
  47. return(NO_ERR);
  48. }
  49. //------------------------------------------------------------------------------
  50. void GlobalCollisionAlert::destroy (void)
  51. {
  52. systemHeap->Free(collisionAlerts);
  53. collisionAlerts = NULL;
  54. maxAlerts = nextRecord = 0;
  55. }
  56. //------------------------------------------------------------------------------
  57. long GlobalCollisionAlert::addRecord (GameObjectPtr obj1, GameObjectPtr obj2, float distance, float time)
  58. {
  59. if (nextRecord < maxAlerts)
  60. {
  61. collisionAlerts[nextRecord].object1 = obj1;
  62. collisionAlerts[nextRecord].object2 = obj2;
  63. collisionAlerts[nextRecord].currentDistance = distance;
  64. collisionAlerts[nextRecord].timeToImpact = time;
  65. nextRecord++;
  66. return(NO_ERR);
  67. }
  68. return(-1);
  69. }
  70. //------------------------------------------------------------------------------
  71. CollisionAlertRecordPtr GlobalCollisionAlert::findAlert (GameObjectPtr object, CollisionAlertRecordPtr startRecord)
  72. {
  73. unsigned long startIndex = 0;
  74. if (startRecord)
  75. {
  76. for (long i=startIndex;i<(long)nextRecord;i++)
  77. {
  78. if (&(collisionAlerts[i]) == startRecord)
  79. {
  80. break;
  81. }
  82. }
  83. startIndex = i+1;
  84. }
  85. if (startIndex < nextRecord)
  86. {
  87. for (long i=startIndex;i<(long)nextRecord;i++)
  88. {
  89. if (( (collisionAlerts[i].object1) == object) ||
  90. ( (collisionAlerts[i].object2) == object))
  91. {
  92. return(&(collisionAlerts[i]));
  93. }
  94. }
  95. }
  96. return(NULL);
  97. }
  98. //------------------------------------------------------------------------------
  99. void GlobalCollisionAlert::purgeRecords (void)
  100. {
  101. nextRecord = 0;
  102. for (long i=0;i<(long)maxAlerts;i++)
  103. {
  104. collisionAlerts[i].object1 = NULL;
  105. collisionAlerts[i].object2 = NULL;
  106. collisionAlerts[i].currentDistance = 0.0;
  107. collisionAlerts[i].timeToImpact = 0.0;
  108. }
  109. }
  110. //------------------------------------------------------------------------------
  111. // class CollisionGrid
  112. void * CollisionGrid::operator new (size_t mySize)
  113. {
  114. void *result = NULL;
  115. if (CollisionSystem::collisionHeap && CollisionSystem::collisionHeap->heapReady())
  116. result = CollisionSystem::collisionHeap->Malloc(mySize);
  117. return(result);
  118. }
  119. //------------------------------------------------------------------------------
  120. void CollisionGrid::operator delete (void *us)
  121. {
  122. if (CollisionSystem::collisionHeap && CollisionSystem::collisionHeap->heapReady())
  123. CollisionSystem::collisionHeap->Free(us);
  124. }
  125. //------------------------------------------------------------------------------
  126. long CollisionGrid::init (Stuff::Vector3D &newOrigin)
  127. {
  128. //--------------------------------------------------
  129. // Check if stuff is allocated. Once this happens
  130. // it is silly to call destroy each time. We can
  131. // simply reinit and check if allocs already go.
  132. if (!gridIsGo)
  133. {
  134. xGridWidth = CollisionSystem::xGridSize;
  135. yGridWidth = CollisionSystem::yGridSize;
  136. maxGridRadius = CollisionSystem::gridRadius;
  137. maxObjects = CollisionSystem::maxObjects;
  138. gridSize = sizeof(CollisionGridNodePtr) * xGridWidth * yGridWidth;
  139. nodeSize = sizeof(CollisionGridNode) * maxObjects;
  140. if (CollisionSystem::collisionHeap && CollisionSystem::collisionHeap->heapReady())
  141. grid = (CollisionGridNodePtr *)CollisionSystem::collisionHeap->Malloc(gridSize);
  142. gosASSERT(grid != NULL);
  143. if (CollisionSystem::collisionHeap && CollisionSystem::collisionHeap->heapReady())
  144. nodes = (CollisionGridNodePtr)CollisionSystem::collisionHeap->Malloc(nodeSize);
  145. gosASSERT(nodes != NULL);
  146. gridXOffset = ((xGridWidth + 1) * maxGridRadius) / 2;
  147. gridYOffset = ((yGridWidth + 1) * maxGridRadius) / 2;
  148. gridXCheck = xGridWidth * maxGridRadius;
  149. gridYCheck = yGridWidth * maxGridRadius;
  150. gridIsGo = TRUE;
  151. }
  152. //-----------------------------
  153. // Reset all of the grid data.
  154. memset(grid,0,gridSize);
  155. memset(nodes,0,nodeSize);
  156. nextAvailableNode = 0;
  157. gridOrigin = newOrigin;
  158. giantObjects = NULL;
  159. return(NO_ERR);
  160. }
  161. //------------------------------------------------------------------------------
  162. void CollisionGrid::destroy (void)
  163. {
  164. if (gridIsGo)
  165. {
  166. if (CollisionSystem::collisionHeap && CollisionSystem::collisionHeap->heapReady())
  167. {
  168. CollisionSystem::collisionHeap->Free(nodes);
  169. nodes = NULL;
  170. CollisionSystem::collisionHeap->Free(grid);
  171. grid = NULL;
  172. }
  173. gridIsGo = FALSE;
  174. init();
  175. }
  176. }
  177. //------------------------------------------------------------------------------
  178. long CollisionGrid::add (unsigned long gridIndex, GameObjectPtr object)
  179. {
  180. gosASSERT(nextAvailableNode < maxObjects);
  181. gosASSERT((gridIndex >= 0) && (gridIndex < (xGridWidth * yGridWidth)));
  182. CollisionGridNodePtr prev = grid[gridIndex];
  183. CollisionGridNodePtr node = &nodes[nextAvailableNode++];
  184. grid[gridIndex] = node;
  185. node->object = object;
  186. node->next = prev;
  187. return(NO_ERR);
  188. }
  189. //------------------------------------------------------------------------------
  190. long CollisionGrid::add (GameObjectPtr object)
  191. {
  192. if (object->getTangible()) //Can anything even hit me?
  193. {
  194. gosASSERT(nextAvailableNode < maxObjects);
  195. float objectRadius = object->getExtentRadius();
  196. //---------------------------------------------
  197. // Check if we are a giant Object
  198. if (objectRadius > maxGridRadius)
  199. {
  200. CollisionGridNodePtr node = &nodes[nextAvailableNode++];
  201. node->object = object;
  202. node->next = giantObjects;
  203. giantObjects = node;
  204. return NO_ERR;
  205. }
  206. float gx,gy;
  207. gx = object->getPosition().x; // - gridOrigin.x;
  208. gx += gridXOffset;
  209. if (gx < 0)
  210. gx = 0;
  211. if (gx >= gridXCheck)
  212. gx = gridXCheck - 1;
  213. gx /= maxGridRadius;
  214. gy = object->getPosition().y; // - gridOrigin.y;
  215. gy += gridYOffset;
  216. if (gy < 0)
  217. gy = 0;
  218. if (gy >= gridYCheck)
  219. gy = gridYCheck - 1;
  220. gy /= maxGridRadius;
  221. unsigned long gridIndex = float2long(gx-0.5f) + float2long(gy-0.5f) * xGridWidth;
  222. long result = add(gridIndex,object);
  223. return result;
  224. }
  225. return NO_ERR;
  226. }
  227. //------------------------------------------------------------------------------
  228. void CollisionGrid::createGrid (void)
  229. {
  230. //------------------------------------------------
  231. // This block of code is only necessary if
  232. // we collide a giantObject against a giantObject.
  233. CollisionGridNodePtr g = giantObjects;
  234. unsigned long totalGiantObjects = 0;
  235. while (g)
  236. {
  237. if (g->next)
  238. {
  239. checkGrid(g->object,g->next);
  240. totalGiantObjects++;
  241. }
  242. g = g->next;
  243. }
  244. for (long y=0;y<(long)yGridWidth;y++)
  245. {
  246. for (long x=0;x<(long)xGridWidth;x++)
  247. {
  248. unsigned long gridIndex = x + y*xGridWidth;
  249. CollisionGridNodePtr g = grid[gridIndex];
  250. while (g)
  251. {
  252. GameObjectPtr obj = g->object;
  253. //--------------------------------------
  254. // Check against the big things.
  255. if (giantObjects)
  256. checkGrid(obj,giantObjects);
  257. CollisionGridNodePtr area = g->next;
  258. //---------------------------
  259. // Check same grid as object
  260. if (area)
  261. checkGrid(obj,area);
  262. if (x < long(xGridWidth-1))
  263. {
  264. //---------------------
  265. // Check grid at x+1,y
  266. area = grid[gridIndex+1];
  267. if (area)
  268. checkGrid(obj,area);
  269. if (y < long(yGridWidth-1))
  270. {
  271. //-----------------------
  272. // Check grid at x+1,y+1
  273. area = grid[gridIndex+1+xGridWidth];
  274. if (area)
  275. checkGrid(obj,area);
  276. }
  277. }
  278. if (y < long(yGridWidth-1))
  279. {
  280. //---------------------
  281. // Check grid at x,y+1
  282. area = grid[gridIndex+xGridWidth];
  283. if (area)
  284. checkGrid(obj,area);
  285. }
  286. g = g->next;
  287. }
  288. }
  289. }
  290. }
  291. //------------------------------------------------------------------------------
  292. void CollisionGrid::checkGrid (GameObjectPtr obj1, CollisionGridNodePtr area)
  293. {
  294. while (area)
  295. {
  296. GameObjectPtr obj2 = area->object;
  297. area = area->next;
  298. if (obj1 && obj2)
  299. {
  300. //-------------------------------------------------------------
  301. // CULL collisions between things which can never collide here
  302. //------------------------------------------------------------
  303. if ((obj1->getObjectClass() == TURRET) && (obj2->getObjectClass() == TURRET) ||
  304. (obj1->getObjectClass() == GATE) && (obj2->getObjectClass() == GATE) ||
  305. (obj1->getObjectClass() == GATE) && (obj2->getObjectClass() == TURRET) ||
  306. (obj1->getObjectClass() == TURRET) && (obj2->getObjectClass() == GATE) ||
  307. (obj1->getObjectClass() == TURRET) && (obj2->getObjectClass() == TREE) ||
  308. (obj1->getObjectClass() == TREE) && (obj2->getObjectClass() == TURRET) ||
  309. (obj1->getObjectClass() == EXPLOSION) && (obj2->getObjectClass() == EXPLOSION))
  310. {
  311. }
  312. else
  313. {
  314. //--------------------------------------------------------
  315. // At this point, we have two objects in the same area
  316. // and they can collide. We now run the bigBoy detection
  317. CollisionSystem::detectCollision(obj1,obj2);
  318. }
  319. }
  320. }
  321. }
  322. //------------------------------------------------------------------------------
  323. // class CollisionSystem
  324. void * CollisionSystem::operator new (size_t mySize)
  325. {
  326. void *result = NULL;
  327. result = systemHeap->Malloc(mySize);
  328. return(result);
  329. }
  330. //------------------------------------------------------------------------------
  331. void CollisionSystem::operator delete (void *us)
  332. {
  333. systemHeap->Free(us);
  334. }
  335. //------------------------------------------------------------------------------
  336. long CollisionSystem::init (FitIniFile *scenarioFile)
  337. {
  338. xGridSize =24;
  339. yGridSize =24;
  340. gridRadius = 200;
  341. maxObjects = 300;
  342. maxCollisions = 100;
  343. warningDist = 250.0; //This is in world Units!!!
  344. alertTime = 2.5; //This is in seconds
  345. collisionHeap = new UserHeap;
  346. gosASSERT(collisionHeap != NULL);
  347. long result = collisionHeap->init(65535);
  348. gosASSERT(result == NO_ERR);
  349. collisionGrid = new CollisionGrid;
  350. gosASSERT(collisionGrid != NULL);
  351. globalCollisionAlert = new GlobalCollisionAlert;
  352. gosASSERT(globalCollisionAlert);
  353. result = globalCollisionAlert->init(20);
  354. gosASSERT(result == NO_ERR);
  355. return(NO_ERR);
  356. }
  357. #define MAX_LISTS_TO_CHECK 3
  358. //------------------------------------------------------------------------------
  359. void CollisionSystem::checkObjects (void)
  360. {
  361. Stuff::Vector3D gridCenter(0L,0L,0L);
  362. collisionGrid->init(gridCenter);
  363. //-----------------------------------------------------------
  364. // Reset the Collision Alerts
  365. globalCollisionAlert->purgeRecords();
  366. #if 1
  367. GameObjectPtr* objList = NULL;
  368. long numCollidables = ObjectManager->getCollidableList(objList);
  369. for (long i = 0; i < numCollidables; i++)
  370. {
  371. if (objList[i] && objList[i]->getExists() && objList[i]->getTangible())
  372. {
  373. #ifdef _DEBUG
  374. long result =
  375. #endif
  376. collisionGrid->add(objList[i]);
  377. gosASSERT(result == NO_ERR);
  378. objList[i]->handleStaticCollision();
  379. }
  380. }
  381. #else
  382. //---------------------------------------------------------
  383. // Convert to Glenn's Magical New Object System!
  384. ObjectQueueNodePtr objList = objectList->getHeadList(); //Start with the default list.
  385. ObjectNodePtr objNode = NULL;
  386. unsigned long objectsPerList[MAX_LISTS_TO_CHECK] =
  387. {
  388. 0,0,0
  389. };
  390. while (objList && (currentList < MAX_LISTS_TO_CHECK))
  391. {
  392. //----------------------------------------------------
  393. // if getObjectType returns NULL we are a baseObject.
  394. // Something like the sun or terrain. DO NOT CHECK!!
  395. // We cast as a GameObject and that will seriously
  396. // mess up if we are just a baseObject!
  397. if (objNode && objNode->getObjectType() != NULL)
  398. {
  399. result = collisionGrid->add((GameObjectPtr)objNode);
  400. objectsPerList[currentList]++;
  401. //----------------------------------------------------------
  402. // This can only happen if we are out of nodes.
  403. // Just break out of loop and check what we have, cause
  404. // no more are getting added! We should probably Inform
  405. // the debug crowd of this!!
  406. gosASSERT(result == NO_ERR);
  407. //-----------------------------------------------------
  408. //-- For right now, just explosions and artillery
  409. //-- Movers need a revised check for the code to work.
  410. //-- Code is in. Away we go.
  411. objNode->handleStaticCollision();
  412. objNode = objNode->next;
  413. }
  414. else
  415. {
  416. objNode = objList->head;
  417. }
  418. if (objNode == NULL)
  419. {
  420. objList = objList->next;
  421. currentList++;
  422. }
  423. }
  424. #endif
  425. collisionGrid->createGrid();
  426. }
  427. //------------------------------------------------------------------------------
  428. void CollisionSystem::detectCollision (GameObjectPtr obj1, GameObjectPtr obj2)
  429. {
  430. float timeOfClosest = 0.0;
  431. //---------------------------------------------------------
  432. // Convert to Glenn's Magical New Object System!
  433. // Need some way to know this is a MOVER/Collider!
  434. if ((obj1->getObjectClass() < EXPLOSION) && (obj2->getObjectClass() < EXPLOSION))
  435. {
  436. //---------------------------------------------------------------
  437. // Objects have to be in the same move plane in order to collide.
  438. // Thus, a copter can't collide with a tree...
  439. long obj1Pos[2] = {0, 0};
  440. long obj2Pos[2] = {0, 0};
  441. obj1->getCellPosition(obj1Pos[0], obj1Pos[1]);
  442. obj2->getCellPosition(obj2Pos[0], obj2Pos[1]);
  443. //-------------------------------------------
  444. // Do a cell based collision. Ignore Radii.
  445. long obj1Block, obj2Block;
  446. long obj1Vertex, obj2Vertex;
  447. obj1->getBlockAndVertexNumber(obj1Block,obj1Vertex);
  448. obj2->getBlockAndVertexNumber(obj2Block,obj2Vertex);
  449. if ((obj1Block == obj2Block) && (obj1Vertex == obj2Vertex))
  450. if ((obj1Pos[0] == obj2Pos[0]) && (obj1Pos[1] == obj2Pos[1])) {
  451. //------------------------------------------------
  452. // OK, we now need to check extents to determine
  453. // If collision happened. May not need to go any
  454. // further for Honor Bound.
  455. checkExtents(obj1,obj2,timeOfClosest);
  456. }
  457. }
  458. else
  459. {
  460. //--------------------------------------------------------
  461. // First we check and see if we are already colliding.
  462. // If so, dump us to checkExtents.
  463. Stuff::Vector3D obj1Pos = obj1->getPosition();
  464. Stuff::Vector3D obj2Pos = obj2->getPosition();
  465. Stuff::Vector3D pos;
  466. pos.Subtract(obj2Pos, obj1Pos);
  467. pos.z = 0.0; //Ignore Elevation. May cause explosion/artillery f-ups!
  468. float distMag = pos.x * pos.x + pos.y * pos.y;//pos.magnitude();
  469. float dist0 = obj1->getExtentRadius();
  470. float dist1 = obj2->getExtentRadius();
  471. float maxDist = dist1 + dist0;
  472. maxDist *= maxDist;
  473. if (distMag < maxDist)
  474. {
  475. //------------------------------------------------
  476. // OK, we now need to check extents to determine
  477. // If collision happened. May not need to go any
  478. // further for Honor Bound.
  479. checkExtents(obj1,obj2,timeOfClosest);
  480. }
  481. }
  482. }
  483. //------------------------------------------------------------------------------
  484. void CollisionSystem::detectStaticCollision (GameObjectPtr obj1, GameObjectPtr obj2)
  485. {
  486. float timeOfClosest = 0.0;
  487. //--------------------------------------------------------
  488. // First we check and see if we are already colliding.
  489. // If so, dump us to checkExtents.
  490. Stuff::Vector3D obj1Pos = obj1->getPosition();
  491. Stuff::Vector3D obj2Pos = obj2->getPosition();
  492. Stuff::Vector3D pos;
  493. pos.Subtract(obj2Pos,obj1Pos);
  494. float distMag = pos.x * pos.x + pos.y * pos.y;
  495. float dist0 = obj1->getExtentRadius();
  496. float dist1 = obj2->getExtentRadius();
  497. float maxDist = dist1 + dist0;
  498. maxDist *= maxDist;
  499. if (distMag < maxDist)
  500. {
  501. //-------------------------------------------------------------------------------------------------------
  502. // If we are inside the extent radius AND standing on impassable terrain, we hit the static object.
  503. // ONLY if we are a mover. We hit them if we are not a mover and we are an artillery or turret or gate.
  504. if (obj1->isMover() && (obj2->getObjectClass() != TREE) && (obj2->getObjectClass() != TERRAINOBJECT))
  505. {
  506. if (!(obj2->isSpecialBuilding()) && (obj2->getObjectClass() != BRIDGE))
  507. {
  508. long obj1Pos[2] = {0, 0};
  509. obj1->getCellPosition(obj1Pos[0], obj1Pos[1]);
  510. if (GameMap->getPassable(obj1Pos[0], obj1Pos[1]))
  511. return;
  512. }
  513. }
  514. checkExtents(obj1,obj2,timeOfClosest);
  515. }
  516. }
  517. //------------------------------------------------------------------------------
  518. void CollisionSystem::checkExtents (GameObjectPtr obj1, GameObjectPtr obj2, float time)
  519. {
  520. //---------------------------------------------------------
  521. // We may not need any more information for MechCommander2
  522. // At this point, simply build a CollisionRecord.
  523. // If we do need more info, add extent checks here.
  524. //------------------------------------------------------------
  525. // If we are an explosion, handle our collisions here and
  526. // DO NOT add our record. Fuel Tank Farms will completely
  527. // swamp this here otherwise.
  528. //
  529. // In fact, I can think of NO reason why we wouldn't just
  530. // handle all of the collisions here anyway. Saves us the
  531. // trouble of ever running out of collision records and since
  532. // we run process collisions afterwards anyway and nothing
  533. // in MechCommander can ever go away, why not?
  534. //
  535. // We handle all here. NO more collision records.
  536. //
  537. //Wow, does this not work.
  538. // Helicopters still need to collide with turrets and explosions and artillery.
  539. // Make it so.
  540. // -fs
  541. if (!obj1->getExists() || !obj2->getExists())
  542. return;
  543. if ((obj1->getMoveLevel() == 2) || (obj2->getMoveLevel() == 2))
  544. {
  545. if ((obj1->getMoveLevel() != obj2->getMoveLevel()) &&
  546. (obj1->getObjectClass() != TURRET) &&
  547. (obj2->getObjectClass() != TURRET) &&
  548. (obj1->getObjectClass() != ARTILLERY) &&
  549. (obj2->getObjectClass() != ARTILLERY) &&
  550. (obj1->getObjectClass() != EXPLOSION) &&
  551. (obj2->getObjectClass() != EXPLOSION))
  552. return;
  553. }
  554. ObjectTypePtr obj1Type = obj1->getObjectType();
  555. ObjectTypePtr obj2Type = obj2->getObjectType();
  556. bool obj1Result = obj1Type->handleCollision(obj1,obj2);
  557. bool obj2Result = obj2Type->handleCollision(obj2,obj1);
  558. //-----------------------------------------
  559. // If we came back TRUE, whack the object.
  560. if (obj1Result)
  561. {
  562. bool removeObject1 = obj1Type->handleDestruction(obj1,obj2);
  563. //------------------------------------------------------------
  564. // Pull the object from the objList here if this flag is set.
  565. // Otherwise, the object will still be around for a little
  566. // while as it destroys itself over the next several frames.
  567. if (removeObject1)
  568. {
  569. obj1->setExists(false);
  570. }
  571. }
  572. //-----------------------------------------
  573. // If we came back TRUE, whack the object.
  574. if (obj2Result)
  575. {
  576. bool removeObject2 = obj2Type->handleDestruction(obj2,obj1);
  577. //------------------------------------------------------------
  578. // Pull the object from the objList here if this flag is set.
  579. // Otherwise, the object will still be around for a little
  580. // while as it destroys itself over the next several frames.
  581. if (removeObject2)
  582. {
  583. obj2->setExists(false);
  584. }
  585. }
  586. }
  587. //------------------------------------------------------------------------------
  588. float CollisionSystem::timeToImpact (GameObjectPtr obj1, GameObjectPtr obj2)
  589. {
  590. float timeOfClosest = -1.0;
  591. //--------------------------------------------------------
  592. // First we check and see if we are already colliding.
  593. // If so, dump us to checkExtents.
  594. Stuff::Vector3D obj1Pos = obj1->getPosition();
  595. Stuff::Vector3D obj2Pos = obj2->getPosition();
  596. Stuff::Vector3D pos;
  597. pos.Subtract(obj2Pos,obj1Pos);
  598. float distMag = pos.x * pos.x + pos.y * pos.y; //pos.magnitude();
  599. float dist0 = obj1->getExtentRadius();
  600. float dist1 = obj2->getExtentRadius();
  601. float maxDist = dist1 + dist0;
  602. maxDist *= maxDist;
  603. if (distMag < maxDist)
  604. {
  605. //------------------------------------------------
  606. // OK, we now need to check extents to determine
  607. // If collision happened. May not need to go any
  608. // further for Honor Bound.
  609. timeOfClosest = 0.0;
  610. return(timeOfClosest);
  611. }
  612. //--------------------------------------------------------
  613. // Then we check the current positions against the final
  614. // positions for the this frame and determine if the
  615. // objects are moving toward or away from each other.
  616. // If away, we're done. If not, next step.
  617. Stuff::Vector3D obj1Vel = obj1->getVelocity();
  618. Stuff::Vector3D obj2Vel = obj2->getVelocity();
  619. Stuff::Vector3D obj1FPos;
  620. obj1Vel *= frameLength;
  621. obj1FPos.Add(obj1Pos,obj1Vel);
  622. Stuff::Vector3D obj2FPos;
  623. obj2Vel *= frameLength;
  624. obj2FPos.Add(obj2Pos,obj2Vel);
  625. Stuff::Vector3D obj1RPos;
  626. obj1RPos.Subtract(obj1FPos,obj1Pos);
  627. Stuff::Vector3D obj2RPos;
  628. obj2RPos.Subtract(obj2FPos,obj2Pos);
  629. float headingResult = obj1RPos * obj2RPos;
  630. //--------------------------------------------------------------
  631. // If this is negative, objects are heading towards each other.
  632. if (headingResult < 0)
  633. {
  634. //--------------------------------------------------------------
  635. // Now find the time of closest approach. This is given by the
  636. // formula (pos DOT vel) / (vel DOT vel) = -timeOfClosest.
  637. Stuff::Vector3D vel;
  638. vel.Subtract(obj2Vel,obj1Vel);
  639. float timeOfClosest = -((pos * vel) / (vel * vel));
  640. if (timeOfClosest <= frameLength)
  641. {
  642. //------------------------------------------------
  643. // They are closest during this frame.
  644. // Find out what distance this is.
  645. Stuff::Vector3D dist;
  646. vel *= timeOfClosest;
  647. dist.Add(pos, vel);
  648. float distMag = dist.x*dist.x + dist.y*dist.y; //dist.magnitude();
  649. if (distMag < maxDist)
  650. {
  651. //------------------------------------------------
  652. // OK, we now need to check extents to determine
  653. // If collision happened. May not need to go any
  654. // further for Honor Bound.
  655. return(timeOfClosest);
  656. }
  657. }
  658. }
  659. return(timeOfClosest);
  660. }
  661. //------------------------------------------------------------------------------
  662. void CollisionSystem::destroy (void)
  663. {
  664. delete collisionGrid;
  665. collisionGrid = NULL;
  666. delete collisionHeap;
  667. collisionHeap = NULL;
  668. delete globalCollisionAlert;
  669. globalCollisionAlert = NULL;
  670. }
  671. //------------------------------------------------------------------------------