collision.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <UnitTest++.h>
  2. #include <ode/ode.h>
  3. TEST(test_collision_trimesh_sphere_exact)
  4. {
  5. /*
  6. * This tests some extreme cases, where a sphere barely touches some triangles
  7. * with zero depth.
  8. */
  9. #ifdef dTRIMESH_GIMPACT
  10. /*
  11. * Although GIMPACT is algorithmically able to handle this extreme case,
  12. * the numerical approximation used for the square root produces inexact results.
  13. */
  14. return;
  15. #endif
  16. dInitODE();
  17. {
  18. const int VertexCount = 4;
  19. const int IndexCount = 2*3;
  20. // this is a square on the XY plane
  21. /*
  22. 3 2
  23. +----+
  24. | /|
  25. | / |
  26. | / |
  27. |/ |
  28. +----+
  29. 0 1
  30. */
  31. float vertices[VertexCount * 3] = {
  32. -1,-1,0,
  33. 1,-1,0,
  34. 1,1,0,
  35. -1,1,0
  36. };
  37. dTriIndex indices[IndexCount] = {
  38. 0,1,2,
  39. 0,2,3
  40. };
  41. dTriMeshDataID data = dGeomTriMeshDataCreate();
  42. dGeomTriMeshDataBuildSingle(data,
  43. vertices,
  44. 3 * sizeof(float),
  45. VertexCount,
  46. indices,
  47. IndexCount,
  48. 3 * sizeof(dTriIndex));
  49. dGeomID trimesh = dCreateTriMesh(0, data, 0, 0, 0);
  50. const dReal radius = 4;
  51. dGeomID sphere = dCreateSphere(0, radius);
  52. dContactGeom cg[4];
  53. int nc;
  54. dVector3 trinormal = { 0, 0, -1 };
  55. // Test case: sphere touches the diagonal edge
  56. dGeomSetPosition(sphere, 0,0,radius);
  57. nc = dCollide(trimesh, sphere, 4, &cg[0], sizeof cg[0]);
  58. CHECK_EQUAL(2, nc);
  59. for (int i=0; i<nc; ++i) {
  60. CHECK_EQUAL(0, cg[i].depth);
  61. CHECK_ARRAY_EQUAL(trinormal, cg[i].normal, 3);
  62. }
  63. // now translate both geoms
  64. dGeomSetPosition(trimesh, 10,30,40);
  65. dGeomSetPosition(sphere, 10,30,40+radius);
  66. // check extreme case, again
  67. nc = dCollide(trimesh, sphere, 4, &cg[0], sizeof cg[0]);
  68. CHECK_EQUAL(2, nc);
  69. for (int i=0; i<nc; ++i) {
  70. CHECK_EQUAL(0, cg[i].depth);
  71. CHECK_ARRAY_EQUAL(trinormal, cg[i].normal, 3);
  72. }
  73. // and now, let's rotate the trimesh, 90 degrees on X
  74. dMatrix3 rot = { 1, 0, 0, 0,
  75. 0, 0, -1, 0,
  76. 0, 1, 0, 0 };
  77. dGeomSetPosition(trimesh, 10,30,40);
  78. dGeomSetRotation(trimesh, rot);
  79. dGeomSetPosition(sphere, 10,30-radius,40);
  80. // check extreme case, again
  81. nc = dCollide(trimesh, sphere, 4, &cg[0], sizeof cg[0]);
  82. CHECK_EQUAL(2, nc);
  83. dVector3 rtrinormal = { 0, 1, 0 };
  84. for (int i=0; i<nc; ++i) {
  85. CHECK_EQUAL(0, cg[i].depth);
  86. CHECK_ARRAY_EQUAL(rtrinormal, cg[i].normal, 3);
  87. }
  88. }
  89. dCloseODE();
  90. }
  91. TEST(test_collision_heightfield_ray_fail)
  92. {
  93. /*
  94. * This test demonstrated a bug in the AABB handling of the
  95. * heightfield.
  96. */
  97. dInitODE();
  98. {
  99. // Create quick heightfield with dummy data
  100. dHeightfieldDataID heightfieldData = dGeomHeightfieldDataCreate();
  101. unsigned char dataBuffer[16+1] = "1234567890123456";
  102. dGeomHeightfieldDataBuildByte(heightfieldData, dataBuffer, 0, 4, 4, 4, 4, 1, 0, 0, 0);
  103. dGeomHeightfieldDataSetBounds(heightfieldData, '0', '9');
  104. dGeomID height = dCreateHeightfield(0, heightfieldData, 1);
  105. // Create ray outside bounds
  106. dGeomID ray = dCreateRay(0, 20);
  107. dGeomRaySet(ray, 5, 10, 1, 0, -1, 0);
  108. dContact contactBuf[10];
  109. // Make sure it does not crash!
  110. dCollide(ray, height, 10, &(contactBuf[0].geom), sizeof(dContact));
  111. dGeomDestroy(height);
  112. dGeomDestroy(ray);
  113. dGeomHeightfieldDataDestroy(heightfieldData);
  114. }
  115. dCloseODE();
  116. }