dRay.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "Include\dRay.h"
  2. #include "dxRay.h"
  3. int dRayClass = -1;
  4. void dAABBRay(dxGeom* Ray, dReal AABB[6]){
  5. dVector3 Start, End;
  6. dGeomRayGet(Ray, Start, End);
  7. dReal Length = dGeomRayGetLength(Ray);
  8. End[0] = Start[0] + End[0] * Length;
  9. End[1] = Start[1] + End[1] * Length;
  10. End[2] = Start[2] + End[2] * Length;
  11. End[3] = Start[3] + End[3] * Length;
  12. if (Start[0] < End[0]){
  13. AABB[0] = Start[0];
  14. AABB[1] = End[0];
  15. }
  16. else{
  17. AABB[0] = End[0];
  18. AABB[1] = Start[0];
  19. }
  20. if (Start[1] < End[1]){
  21. AABB[2] = Start[1];
  22. AABB[3] = End[1];
  23. }
  24. else{
  25. AABB[2] = End[1];
  26. AABB[3] = Start[1];
  27. }
  28. if (Start[2] < End[2]){
  29. AABB[4] = Start[2];
  30. AABB[5] = End[2];
  31. }
  32. else{
  33. AABB[4] = End[2];
  34. AABB[5] = Start[2];
  35. }
  36. // Should we tweak the box to have a minimum size for axis aligned lines? How small should it be?
  37. }
  38. dColliderFn* dRayColliderFn(int num){
  39. if (num == dPlaneClass) return (dColliderFn*)&dCollidePR;
  40. if (num == dSphereClass) return (dColliderFn*)&dCollideSR;
  41. if (num == dBoxClass) return (dColliderFn*)&dCollideBR;
  42. if (num == dCCylinderClass) return (dColliderFn*)&dCollideCCR;
  43. return 0;
  44. }
  45. dxGeom* dGeomCreateRay(dSpaceID space, dReal Length){
  46. if (dRayClass == -1){
  47. dGeomClass c;
  48. c.bytes = sizeof(dxRay);
  49. c.collider = &dRayColliderFn;
  50. c.aabb = &dAABBRay;
  51. c.aabb_test = 0;
  52. c.dtor = 0;
  53. dRayClass = dCreateGeomClass(&c);
  54. }
  55. dxGeom* g = dCreateGeom(dRayClass);
  56. if (space) dSpaceAdd(space, g);
  57. dGeomRaySetLength(g, Length);
  58. return g;
  59. }
  60. void dGeomRaySetLength(dxGeom* g, dReal Length){
  61. ((dxRay*)dGeomGetClassData(g))->Length = Length;
  62. }
  63. dReal dGeomRayGetLength(dxGeom* g){
  64. return ((dxRay*)dGeomGetClassData(g))->Length;
  65. }
  66. void dGeomRaySet(dxGeom* g, dVector3 Origin, dVector3 Direction){
  67. dGeomSetPosition(g, Origin[0], Origin[1], Origin[2]);
  68. dVector3 Up, Right;
  69. dPlaneSpace(Direction, Up, Right);
  70. Origin[3] = Up[3] = Right[3] = REAL(0.0);
  71. dMatrix3 Rotation;
  72. Rotation[0 * 4 + 0] = Right[0];
  73. Rotation[1 * 4 + 0] = Right[1];
  74. Rotation[2 * 4 + 0] = Right[2];
  75. Rotation[3 * 4 + 0] = Right[3];
  76. Rotation[0 * 4 + 1] = Up[0];
  77. Rotation[1 * 4 + 1] = Up[1];
  78. Rotation[2 * 4 + 1] = Up[2];
  79. Rotation[3 * 4 + 1] = Up[3];
  80. Rotation[0 * 4 + 2] = Direction[0];
  81. Rotation[1 * 4 + 2] = Direction[1];
  82. Rotation[2 * 4 + 2] = Direction[2];
  83. Rotation[3 * 4 + 2] = Direction[3];
  84. dGeomSetRotation(g, Rotation);
  85. }
  86. void dGeomRayGet(dxGeom* g, dVector3 Origin, dVector3 Direction){
  87. const dReal* Position = dGeomGetPosition(g);
  88. Origin[0] = Position[0];
  89. Origin[1] = Position[1];
  90. Origin[2] = Position[2];
  91. Origin[3] = Position[3];
  92. const dReal* Rotation = dGeomGetRotation(g);
  93. Direction[0] = Rotation[0 * 4 + 2];
  94. Direction[1] = Rotation[1 * 4 + 2];
  95. Direction[2] = Rotation[2 * 4 + 2];
  96. Direction[3] = Rotation[3 * 4 + 2];
  97. }