collision_cylinder_sphere.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. /*******************************************************************
  23. * *
  24. * cylinder-sphere collider by Christoph Beyer ([email protected]) *
  25. * *
  26. * In Cylinder/Sphere-collisions, there are three possibilies: *
  27. * 1. collision with the cylinder's nappe *
  28. * 2. collision with one of the cylinder's disc *
  29. * 3. collision with one of the disc's border *
  30. * *
  31. * This collider computes two distances (s, t) and based on them, *
  32. * it decides, which collision we have. *
  33. * This collider always generates 1 (or 0, if we have no collison) *
  34. * contacts. *
  35. * It is able to "separate" cylinder and sphere in all *
  36. * configurations, but it never pays attention to velocity. *
  37. * So, in extrem situations, "tunneling-effect" is possible. *
  38. * *
  39. *******************************************************************/
  40. #include <ode/collision.h>
  41. #include <ode/rotation.h>
  42. #include <ode/objects.h>
  43. #include "config.h"
  44. #include "matrix.h"
  45. #include "odemath.h"
  46. #include "collision_kernel.h" // for dxGeom
  47. #include "collision_util.h"
  48. int dCollideCylinderSphere(dxGeom* Cylinder, dxGeom* Sphere,
  49. int flags, dContactGeom *contact, int skip)
  50. {
  51. dIASSERT (skip >= (int)sizeof(dContactGeom));
  52. dIASSERT (Cylinder->type == dCylinderClass);
  53. dIASSERT (Sphere->type == dSphereClass);
  54. dIASSERT ((flags & NUMC_MASK) >= 1);
  55. //unsigned char* pContactData = (unsigned char*)contact;
  56. int GeomCount = 0; // count of used contacts
  57. #ifdef dSINGLE
  58. const dReal toleranz = REAL(0.0001);
  59. #endif
  60. #ifdef dDOUBLE
  61. const dReal toleranz = REAL(0.0000001);
  62. #endif
  63. // get the data from the geoms
  64. dReal radius, length;
  65. dGeomCylinderGetParams(Cylinder, &radius, &length);
  66. dVector3 &cylpos = Cylinder->final_posr->pos;
  67. //const dReal* pfRot1 = dGeomGetRotation(Cylinder);
  68. dReal radius2;
  69. radius2 = dGeomSphereGetRadius(Sphere);
  70. const dReal* SpherePos = dGeomGetPosition(Sphere);
  71. // G1Pos1 is the middle of the first disc
  72. // G1Pos2 is the middle of the second disc
  73. // vDir1 is the unit direction of the cylinderaxis
  74. dVector3 G1Pos1, G1Pos2, vDir1;
  75. vDir1[0] = Cylinder->final_posr->R[2];
  76. vDir1[1] = Cylinder->final_posr->R[6];
  77. vDir1[2] = Cylinder->final_posr->R[10];
  78. dReal s;
  79. s = length * REAL(0.5); // just a precomputed factor
  80. G1Pos2[0] = vDir1[0] * s + cylpos[0];
  81. G1Pos2[1] = vDir1[1] * s + cylpos[1];
  82. G1Pos2[2] = vDir1[2] * s + cylpos[2];
  83. G1Pos1[0] = vDir1[0] * -s + cylpos[0];
  84. G1Pos1[1] = vDir1[1] * -s + cylpos[1];
  85. G1Pos1[2] = vDir1[2] * -s + cylpos[2];
  86. dVector3 C;
  87. dReal t;
  88. // Step 1: compute the two distances 's' and 't'
  89. // 's' is the distance from the first disc (in vDir1-/Zylinderaxis-direction), the disc with G1Pos1 in the middle
  90. s = (SpherePos[0] - G1Pos1[0]) * vDir1[0] - (G1Pos1[1] - SpherePos[1]) * vDir1[1] - (G1Pos1[2] - SpherePos[2]) * vDir1[2];
  91. if(s < (-radius2) || s > (length + radius2) )
  92. {
  93. // Sphere is too far away from the discs
  94. // no collision
  95. return 0;
  96. }
  97. // C is the direction from Sphere-middle to the cylinder-axis (vDir1); C is orthogonal to the cylinder-axis
  98. C[0] = s * vDir1[0] + G1Pos1[0] - SpherePos[0];
  99. C[1] = s * vDir1[1] + G1Pos1[1] - SpherePos[1];
  100. C[2] = s * vDir1[2] + G1Pos1[2] - SpherePos[2];
  101. // t is the distance from the Sphere-middle to the cylinder-axis!
  102. t = dVector3Length(C);
  103. if(t > (radius + radius2) )
  104. {
  105. // Sphere is too far away from the cylinder axis!
  106. // no collision
  107. return 0;
  108. }
  109. // decide which kind of collision we have:
  110. if(t > radius && (s < 0 || s > length) )
  111. {
  112. // 3. collision
  113. if(s <= 0)
  114. {
  115. contact->depth = radius2 - dSqrt( (s) * (s) + (t - radius) * (t - radius) );
  116. if(contact->depth < 0)
  117. {
  118. // no collision!
  119. return 0;
  120. }
  121. contact->pos[0] = C[0] / t * -radius + G1Pos1[0];
  122. contact->pos[1] = C[1] / t * -radius + G1Pos1[1];
  123. contact->pos[2] = C[2] / t * -radius + G1Pos1[2];
  124. contact->normal[0] = (contact->pos[0] - SpherePos[0]) / (radius2 - contact->depth);
  125. contact->normal[1] = (contact->pos[1] - SpherePos[1]) / (radius2 - contact->depth);
  126. contact->normal[2] = (contact->pos[2] - SpherePos[2]) / (radius2 - contact->depth);
  127. contact->g1 = Cylinder;
  128. contact->g2 = Sphere;
  129. contact->side1 = -1;
  130. contact->side2 = -1;
  131. GeomCount++;
  132. return GeomCount;
  133. }
  134. else
  135. {
  136. // now s is bigger than length here!
  137. contact->depth = radius2 - dSqrt( (s - length) * (s - length) + (t - radius) * (t - radius) );
  138. if(contact->depth < 0)
  139. {
  140. // no collision!
  141. return 0;
  142. }
  143. contact->pos[0] = C[0] / t * -radius + G1Pos2[0];
  144. contact->pos[1] = C[1] / t * -radius + G1Pos2[1];
  145. contact->pos[2] = C[2] / t * -radius + G1Pos2[2];
  146. contact->normal[0] = (contact->pos[0] - SpherePos[0]) / (radius2 - contact->depth);
  147. contact->normal[1] = (contact->pos[1] - SpherePos[1]) / (radius2 - contact->depth);
  148. contact->normal[2] = (contact->pos[2] - SpherePos[2]) / (radius2 - contact->depth);
  149. contact->g1 = Cylinder;
  150. contact->g2 = Sphere;
  151. contact->side1 = -1;
  152. contact->side2 = -1;
  153. GeomCount++;
  154. return GeomCount;
  155. }
  156. }
  157. else if( (radius - t) <= s && (radius - t) <= (length - s) )
  158. {
  159. // 1. collsision
  160. if(t > (radius2 + toleranz))
  161. {
  162. // cylinder-axis is outside the sphere
  163. contact->depth = (radius2 + radius) - t;
  164. if(contact->depth < 0)
  165. {
  166. // should never happen, but just for safeness
  167. return 0;
  168. }
  169. else
  170. {
  171. C[0] /= t;
  172. C[1] /= t;
  173. C[2] /= t;
  174. contact->pos[0] = C[0] * radius2 + SpherePos[0];
  175. contact->pos[1] = C[1] * radius2 + SpherePos[1];
  176. contact->pos[2] = C[2] * radius2 + SpherePos[2];
  177. contact->normal[0] = C[0];
  178. contact->normal[1] = C[1];
  179. contact->normal[2] = C[2];
  180. contact->g1 = Cylinder;
  181. contact->g2 = Sphere;
  182. contact->side1 = -1;
  183. contact->side2 = -1;
  184. GeomCount++;
  185. return GeomCount;
  186. }
  187. }
  188. else
  189. {
  190. // cylinder-axis is outside of the sphere
  191. contact->depth = (radius2 + radius) - t;
  192. if(contact->depth < 0)
  193. {
  194. // should never happen, but just for safeness
  195. return 0;
  196. }
  197. else
  198. {
  199. contact->pos[0] = C[0] + SpherePos[0];
  200. contact->pos[1] = C[1] + SpherePos[1];
  201. contact->pos[2] = C[2] + SpherePos[2];
  202. contact->normal[0] = C[0] / t;
  203. contact->normal[1] = C[1] / t;
  204. contact->normal[2] = C[2] / t;
  205. contact->g1 = Cylinder;
  206. contact->g2 = Sphere;
  207. contact->side1 = -1;
  208. contact->side2 = -1;
  209. GeomCount++;
  210. return GeomCount;
  211. }
  212. }
  213. }
  214. else
  215. {
  216. // 2. collision
  217. if(s <= (length * REAL(0.5)) )
  218. {
  219. // collsision with the first disc
  220. contact->depth = s + radius2;
  221. if(contact->depth < 0)
  222. {
  223. // should never happen, but just for safeness
  224. return 0;
  225. }
  226. contact->pos[0] = radius2 * vDir1[0] + SpherePos[0];
  227. contact->pos[1] = radius2 * vDir1[1] + SpherePos[1];
  228. contact->pos[2] = radius2 * vDir1[2] + SpherePos[2];
  229. contact->normal[0] = vDir1[0];
  230. contact->normal[1] = vDir1[1];
  231. contact->normal[2] = vDir1[2];
  232. contact->g1 = Cylinder;
  233. contact->g2 = Sphere;
  234. contact->side1 = -1;
  235. contact->side2 = -1;
  236. GeomCount++;
  237. return GeomCount;
  238. }
  239. else
  240. {
  241. // collsision with the second disc
  242. contact->depth = (radius2 + length - s);
  243. if(contact->depth < 0)
  244. {
  245. // should never happen, but just for safeness
  246. return 0;
  247. }
  248. contact->pos[0] = radius2 * -vDir1[0] + SpherePos[0];
  249. contact->pos[1] = radius2 * -vDir1[1] + SpherePos[1];
  250. contact->pos[2] = radius2 * -vDir1[2] + SpherePos[2];
  251. contact->normal[0] = -vDir1[0];
  252. contact->normal[1] = -vDir1[1];
  253. contact->normal[2] = -vDir1[2];
  254. contact->g1 = Cylinder;
  255. contact->g2 = Sphere;
  256. contact->side1 = -1;
  257. contact->side2 = -1;
  258. GeomCount++;
  259. return GeomCount;
  260. }
  261. }
  262. return GeomCount;
  263. }