CollisionModel_debug.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. /*
  21. ===============================================================================
  22. Trace model vs. polygonal model collision detection.
  23. ===============================================================================
  24. */
  25. #pragma hdrstop
  26. #include "../idlib/precompiled.h"
  27. #include "CollisionModel_local.h"
  28. /*
  29. ===============================================================================
  30. Visualisation code
  31. ===============================================================================
  32. */
  33. const char *cm_contentsNameByIndex[] = {
  34. "none", // 0
  35. "solid", // 1
  36. "opaque", // 2
  37. "water", // 3
  38. "playerclip", // 4
  39. "monsterclip", // 5
  40. "moveableclip", // 6
  41. "ikclip", // 7
  42. "blood", // 8
  43. "body", // 9
  44. "corpse", // 10
  45. "trigger", // 11
  46. "aas_solid", // 12
  47. "aas_obstacle", // 13
  48. "flashlight_trigger", // 14
  49. NULL
  50. };
  51. int cm_contentsFlagByIndex[] = {
  52. -1, // 0
  53. CONTENTS_SOLID, // 1
  54. CONTENTS_OPAQUE, // 2
  55. CONTENTS_WATER, // 3
  56. CONTENTS_PLAYERCLIP, // 4
  57. CONTENTS_MONSTERCLIP, // 5
  58. CONTENTS_MOVEABLECLIP, // 6
  59. CONTENTS_IKCLIP, // 7
  60. CONTENTS_BLOOD, // 8
  61. CONTENTS_BODY, // 9
  62. CONTENTS_CORPSE, // 10
  63. CONTENTS_TRIGGER, // 11
  64. CONTENTS_AAS_SOLID, // 12
  65. CONTENTS_AAS_OBSTACLE, // 13
  66. CONTENTS_FLASHLIGHT_TRIGGER, // 14
  67. 0
  68. };
  69. idCVar cm_drawMask( "cm_drawMask", "none", CVAR_GAME, "collision mask", cm_contentsNameByIndex, idCmdSystem::ArgCompletion_String<cm_contentsNameByIndex> );
  70. idCVar cm_drawColor( "cm_drawColor", "1 0 0 .5", CVAR_GAME, "color used to draw the collision models" );
  71. idCVar cm_drawFilled( "cm_drawFilled", "0", CVAR_GAME | CVAR_BOOL, "draw filled polygons" );
  72. idCVar cm_drawInternal( "cm_drawInternal", "1", CVAR_GAME | CVAR_BOOL, "draw internal edges green" );
  73. idCVar cm_drawNormals( "cm_drawNormals", "0", CVAR_GAME | CVAR_BOOL, "draw polygon and edge normals" );
  74. idCVar cm_backFaceCull( "cm_backFaceCull", "0", CVAR_GAME | CVAR_BOOL, "cull back facing polygons" );
  75. idCVar cm_debugCollision( "cm_debugCollision", "0", CVAR_GAME | CVAR_BOOL, "debug the collision detection" );
  76. static idVec4 cm_color;
  77. /*
  78. ================
  79. idCollisionModelManagerLocal::ContentsFromString
  80. ================
  81. */
  82. int idCollisionModelManagerLocal::ContentsFromString( const char *string ) const {
  83. int i, contents = 0;
  84. idLexer src( string, idStr::Length( string ), "ContentsFromString" );
  85. idToken token;
  86. while( src.ReadToken( &token ) ) {
  87. if ( token == "," ) {
  88. continue;
  89. }
  90. for ( i = 1; cm_contentsNameByIndex[i] != NULL; i++ ) {
  91. if ( token.Icmp( cm_contentsNameByIndex[i] ) == 0 ) {
  92. contents |= cm_contentsFlagByIndex[i];
  93. break;
  94. }
  95. }
  96. }
  97. return contents;
  98. }
  99. /*
  100. ================
  101. idCollisionModelManagerLocal::StringFromContents
  102. ================
  103. */
  104. const char *idCollisionModelManagerLocal::StringFromContents( const int contents ) const {
  105. int i, length = 0;
  106. static char contentsString[MAX_STRING_CHARS];
  107. contentsString[0] = '\0';
  108. for ( i = 1; cm_contentsFlagByIndex[i] != 0; i++ ) {
  109. if ( contents & cm_contentsFlagByIndex[i] ) {
  110. if ( length != 0 ) {
  111. length += idStr::snPrintf( contentsString + length, sizeof( contentsString ) - length, "," );
  112. }
  113. length += idStr::snPrintf( contentsString + length, sizeof( contentsString ) - length, cm_contentsNameByIndex[i] );
  114. }
  115. }
  116. return contentsString;
  117. }
  118. /*
  119. ================
  120. idCollisionModelManagerLocal::DrawEdge
  121. ================
  122. */
  123. void idCollisionModelManagerLocal::DrawEdge( cm_model_t *model, int edgeNum, const idVec3 &origin, const idMat3 &axis ) {
  124. int side;
  125. cm_edge_t *edge;
  126. idVec3 start, end, mid;
  127. bool isRotated;
  128. isRotated = axis.IsRotated();
  129. edge = model->edges + abs(edgeNum);
  130. side = edgeNum < 0;
  131. start = model->vertices[edge->vertexNum[side]].p;
  132. end = model->vertices[edge->vertexNum[!side]].p;
  133. if ( isRotated ) {
  134. start *= axis;
  135. end *= axis;
  136. }
  137. start += origin;
  138. end += origin;
  139. if ( edge->internal ) {
  140. if ( cm_drawInternal.GetBool() ) {
  141. common->RW()->DebugArrow( colorGreen, start, end, 1 );
  142. }
  143. } else {
  144. if ( edge->numUsers > 2 ) {
  145. common->RW()->DebugArrow( colorBlue, start, end, 1 );
  146. } else {
  147. common->RW()->DebugArrow( cm_color, start, end, 1 );
  148. }
  149. }
  150. if ( cm_drawNormals.GetBool() ) {
  151. mid = (start + end) * 0.5f;
  152. if ( isRotated ) {
  153. end = mid + 5 * (axis * edge->normal);
  154. } else {
  155. end = mid + 5 * edge->normal;
  156. }
  157. common->RW()->DebugArrow( colorCyan, mid, end, 1 );
  158. }
  159. }
  160. /*
  161. ================
  162. idCollisionModelManagerLocal::DrawPolygon
  163. ================
  164. */
  165. void idCollisionModelManagerLocal::DrawPolygon( cm_model_t *model, cm_polygon_t *p, const idVec3 &origin, const idMat3 &axis, const idVec3 &viewOrigin ) {
  166. int i, edgeNum;
  167. cm_edge_t *edge;
  168. idVec3 center, end, dir;
  169. if ( cm_backFaceCull.GetBool() ) {
  170. edgeNum = p->edges[0];
  171. edge = model->edges + abs(edgeNum);
  172. dir = model->vertices[edge->vertexNum[0]].p - viewOrigin;
  173. if ( dir * p->plane.Normal() > 0.0f ) {
  174. return;
  175. }
  176. }
  177. if ( cm_drawNormals.GetBool() ) {
  178. center = vec3_origin;
  179. for ( i = 0; i < p->numEdges; i++ ) {
  180. edgeNum = p->edges[i];
  181. edge = model->edges + abs(edgeNum);
  182. center += model->vertices[edge->vertexNum[edgeNum < 0]].p;
  183. }
  184. center *= (1.0f / p->numEdges);
  185. if ( axis.IsRotated() ) {
  186. center = center * axis + origin;
  187. end = center + 5 * (axis * p->plane.Normal());
  188. } else {
  189. center += origin;
  190. end = center + 5 * p->plane.Normal();
  191. }
  192. common->RW()->DebugArrow( colorMagenta, center, end, 1 );
  193. }
  194. if ( cm_drawFilled.GetBool() ) {
  195. idFixedWinding winding;
  196. for ( i = p->numEdges - 1; i >= 0; i-- ) {
  197. edgeNum = p->edges[i];
  198. edge = model->edges + abs(edgeNum);
  199. winding += origin + model->vertices[edge->vertexNum[INT32_SIGNBITSET(edgeNum)]].p * axis;
  200. }
  201. common->RW()->DebugPolygon( cm_color, winding );
  202. } else {
  203. for ( i = 0; i < p->numEdges; i++ ) {
  204. edgeNum = p->edges[i];
  205. edge = model->edges + abs(edgeNum);
  206. if ( edge->checkcount == checkCount ) {
  207. continue;
  208. }
  209. edge->checkcount = checkCount;
  210. DrawEdge( model, edgeNum, origin, axis );
  211. }
  212. }
  213. }
  214. /*
  215. ================
  216. idCollisionModelManagerLocal::DrawNodePolygons
  217. ================
  218. */
  219. void idCollisionModelManagerLocal::DrawNodePolygons( cm_model_t *model, cm_node_t *node,
  220. const idVec3 &origin, const idMat3 &axis,
  221. const idVec3 &viewOrigin, const float radius ) {
  222. int i;
  223. cm_polygon_t *p;
  224. cm_polygonRef_t *pref;
  225. while (1) {
  226. for ( pref = node->polygons; pref; pref = pref->next ) {
  227. p = pref->p;
  228. if ( radius ) {
  229. // polygon bounds should overlap with trace bounds
  230. for ( i = 0; i < 3; i++ ) {
  231. if ( p->bounds[0][i] > viewOrigin[i] + radius ) {
  232. break;
  233. }
  234. if ( p->bounds[1][i] < viewOrigin[i] - radius ) {
  235. break;
  236. }
  237. }
  238. if ( i < 3 ) {
  239. continue;
  240. }
  241. }
  242. if ( p->checkcount == checkCount ) {
  243. continue;
  244. }
  245. if ( !( p->contents & cm_contentsFlagByIndex[cm_drawMask.GetInteger()] ) ) {
  246. continue;
  247. }
  248. DrawPolygon( model, p, origin, axis, viewOrigin );
  249. p->checkcount = checkCount;
  250. }
  251. if ( node->planeType == -1 ) {
  252. break;
  253. }
  254. if ( radius && viewOrigin[node->planeType] > node->planeDist + radius ) {
  255. node = node->children[0];
  256. } else if ( radius && viewOrigin[node->planeType] < node->planeDist - radius ) {
  257. node = node->children[1];
  258. } else {
  259. DrawNodePolygons( model, node->children[1], origin, axis, viewOrigin, radius );
  260. node = node->children[0];
  261. }
  262. }
  263. }
  264. /*
  265. ================
  266. idCollisionModelManagerLocal::DrawModel
  267. ================
  268. */
  269. void idCollisionModelManagerLocal::DrawModel( cmHandle_t handle, const idVec3 &modelOrigin, const idMat3 &modelAxis,
  270. const idVec3 &viewOrigin, const float radius ) {
  271. cm_model_t *model;
  272. idVec3 viewPos;
  273. if ( handle < 0 && handle >= numModels ) {
  274. return;
  275. }
  276. if ( cm_drawColor.IsModified() ) {
  277. sscanf( cm_drawColor.GetString(), "%f %f %f %f", &cm_color.x, &cm_color.y, &cm_color.z, &cm_color.w );
  278. cm_drawColor.ClearModified();
  279. }
  280. model = models[ handle ];
  281. viewPos = (viewOrigin - modelOrigin) * modelAxis.Transpose();
  282. checkCount++;
  283. DrawNodePolygons( model, model->node, modelOrigin, modelAxis, viewPos, radius );
  284. }
  285. /*
  286. ===============================================================================
  287. Speed test code
  288. ===============================================================================
  289. */
  290. static idCVar cm_testCollision( "cm_testCollision", "0", CVAR_GAME | CVAR_BOOL, "" );
  291. static idCVar cm_testRotation( "cm_testRotation", "1", CVAR_GAME | CVAR_BOOL, "" );
  292. static idCVar cm_testModel( "cm_testModel", "0", CVAR_GAME | CVAR_INTEGER, "" );
  293. static idCVar cm_testTimes( "cm_testTimes", "1000", CVAR_GAME | CVAR_INTEGER, "" );
  294. static idCVar cm_testRandomMany( "cm_testRandomMany", "0", CVAR_GAME | CVAR_BOOL, "" );
  295. static idCVar cm_testOrigin( "cm_testOrigin", "0 0 0", CVAR_GAME, "" );
  296. static idCVar cm_testReset( "cm_testReset", "0", CVAR_GAME | CVAR_BOOL, "" );
  297. static idCVar cm_testBox( "cm_testBox", "-16 -16 0 16 16 64", CVAR_GAME, "" );
  298. static idCVar cm_testBoxRotation( "cm_testBoxRotation", "0 0 0", CVAR_GAME, "" );
  299. static idCVar cm_testWalk( "cm_testWalk", "1", CVAR_GAME | CVAR_BOOL, "" );
  300. static idCVar cm_testLength( "cm_testLength", "1024", CVAR_GAME | CVAR_FLOAT, "" );
  301. static idCVar cm_testRadius( "cm_testRadius", "64", CVAR_GAME | CVAR_FLOAT, "" );
  302. static idCVar cm_testAngle( "cm_testAngle", "60", CVAR_GAME | CVAR_FLOAT, "" );
  303. static int total_translation;
  304. static int min_translation = 999999;
  305. static int max_translation = -999999;
  306. static int num_translation = 0;
  307. static int total_rotation;
  308. static int min_rotation = 999999;
  309. static int max_rotation = -999999;
  310. static int num_rotation = 0;
  311. static idVec3 start;
  312. static idVec3 *testend;
  313. #include "../sys/sys_public.h"
  314. void idCollisionModelManagerLocal::DebugOutput( const idVec3 &origin ) {
  315. int i, k, t;
  316. char buf[128];
  317. idVec3 end;
  318. idAngles boxAngles;
  319. idMat3 modelAxis, boxAxis;
  320. idBounds bounds;
  321. trace_t trace;
  322. if ( !cm_testCollision.GetBool() ) {
  323. return;
  324. }
  325. testend = (idVec3 *) Mem_Alloc( cm_testTimes.GetInteger() * sizeof(idVec3), TAG_COLLISION );
  326. if ( cm_testReset.GetBool() || ( cm_testWalk.GetBool() && !start.Compare( start ) ) ) {
  327. total_translation = total_rotation = 0;
  328. min_translation = min_rotation = 999999;
  329. max_translation = max_rotation = -999999;
  330. num_translation = num_rotation = 0;
  331. cm_testReset.SetBool( false );
  332. }
  333. if ( cm_testWalk.GetBool() ) {
  334. start = origin;
  335. cm_testOrigin.SetString( va( "%1.2f %1.2f %1.2f", start[0], start[1], start[2] ) );
  336. } else {
  337. sscanf( cm_testOrigin.GetString(), "%f %f %f", &start[0], &start[1], &start[2] );
  338. }
  339. sscanf( cm_testBox.GetString(), "%f %f %f %f %f %f", &bounds[0][0], &bounds[0][1], &bounds[0][2],
  340. &bounds[1][0], &bounds[1][1], &bounds[1][2] );
  341. sscanf( cm_testBoxRotation.GetString(), "%f %f %f", &boxAngles[0], &boxAngles[1], &boxAngles[2] );
  342. boxAxis = boxAngles.ToMat3();
  343. modelAxis.Identity();
  344. idTraceModel itm( bounds );
  345. idRandom random( 0 );
  346. idTimer timer;
  347. if ( cm_testRandomMany.GetBool() ) {
  348. // if many traces in one random direction
  349. for ( i = 0; i < 3; i++ ) {
  350. testend[0][i] = start[i] + random.CRandomFloat() * cm_testLength.GetFloat();
  351. }
  352. for ( k = 1; k < cm_testTimes.GetInteger(); k++ ) {
  353. testend[k] = testend[0];
  354. }
  355. } else {
  356. // many traces each in a different random direction
  357. for ( k = 0; k < cm_testTimes.GetInteger(); k++ ) {
  358. for ( i = 0; i < 3; i++ ) {
  359. testend[k][i] = start[i] + random.CRandomFloat() * cm_testLength.GetFloat();
  360. }
  361. }
  362. }
  363. // translational collision detection
  364. timer.Clear();
  365. timer.Start();
  366. for ( i = 0; i < cm_testTimes.GetInteger(); i++ ) {
  367. Translation( &trace, start, testend[i], &itm, boxAxis, CONTENTS_SOLID|CONTENTS_PLAYERCLIP, cm_testModel.GetInteger(), vec3_origin, modelAxis );
  368. }
  369. timer.Stop();
  370. t = timer.Milliseconds();
  371. if ( t < min_translation ) min_translation = t;
  372. if ( t > max_translation ) max_translation = t;
  373. num_translation++;
  374. total_translation += t;
  375. if ( cm_testTimes.GetInteger() > 9999 ) {
  376. sprintf( buf, "%3dK", (int ) ( cm_testTimes.GetInteger() / 1000 ) );
  377. } else {
  378. sprintf( buf, "%4d", cm_testTimes.GetInteger() );
  379. }
  380. common->Printf("%s translations: %4d milliseconds, (min = %d, max = %d, av = %1.1f)\n", buf, t, min_translation, max_translation, (float) total_translation / num_translation );
  381. if ( cm_testRandomMany.GetBool() ) {
  382. // if many traces in one random direction
  383. for ( i = 0; i < 3; i++ ) {
  384. testend[0][i] = start[i] + random.CRandomFloat() * cm_testRadius.GetFloat();
  385. }
  386. for ( k = 1; k < cm_testTimes.GetInteger(); k++ ) {
  387. testend[k] = testend[0];
  388. }
  389. } else {
  390. // many traces each in a different random direction
  391. for ( k = 0; k < cm_testTimes.GetInteger(); k++ ) {
  392. for ( i = 0; i < 3; i++ ) {
  393. testend[k][i] = start[i] + random.CRandomFloat() * cm_testRadius.GetFloat();
  394. }
  395. }
  396. }
  397. if ( cm_testRotation.GetBool() ) {
  398. // rotational collision detection
  399. idVec3 vec( random.CRandomFloat(), random.CRandomFloat(), random.RandomFloat() );
  400. vec.Normalize();
  401. idRotation rotation( vec3_origin, vec, cm_testAngle.GetFloat() );
  402. timer.Clear();
  403. timer.Start();
  404. for ( i = 0; i < cm_testTimes.GetInteger(); i++ ) {
  405. rotation.SetOrigin( testend[i] );
  406. Rotation( &trace, start, rotation, &itm, boxAxis, CONTENTS_SOLID|CONTENTS_PLAYERCLIP, cm_testModel.GetInteger(), vec3_origin, modelAxis );
  407. }
  408. timer.Stop();
  409. t = timer.Milliseconds();
  410. if ( t < min_rotation ) min_rotation = t;
  411. if ( t > max_rotation ) max_rotation = t;
  412. num_rotation++;
  413. total_rotation += t;
  414. if ( cm_testTimes.GetInteger() > 9999 ) {
  415. sprintf( buf, "%3dK", (int ) ( cm_testTimes.GetInteger() / 1000 ) );
  416. } else {
  417. sprintf( buf, "%4d", cm_testTimes.GetInteger() );
  418. }
  419. common->Printf("%s rotation: %4d milliseconds, (min = %d, max = %d, av = %1.1f)\n", buf, t, min_rotation, max_rotation, (float) total_rotation / num_rotation );
  420. }
  421. Mem_Free( testend );
  422. testend = NULL;
  423. }