dRay_Plane.cpp 976 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Ripped from Paul Bourke
  2. #include "Include\dRay.h"
  3. #include "dxRay.h"
  4. int dCollidePR(dxGeom* RayGeom, dxGeom* PlaneGeom, int Flags, dContactGeom* Contact, int Stride){
  5. dVector3 Plane;
  6. dGeomPlaneGetParams(PlaneGeom, Plane);
  7. dVector3 Origin, Direction;
  8. dGeomRayGet(RayGeom, Origin, Direction);
  9. dReal Length = dGeomRayGetLength(RayGeom);
  10. dReal Denom = Plane[0] * Direction[0] + Plane[1] * Direction[1] + Plane[2] * Direction[2];
  11. if (dFabs(Denom) < 0.00001f){
  12. return 0; // Ray never hits
  13. }
  14. float T = -(Plane[3] + Plane[0] * Origin[0] + Plane[1] * Origin[1] + Plane[2] * Origin[2]) / Denom;
  15. if (T < 0 || T > Length){
  16. return 0; // Ray hits but not within boundaries
  17. }
  18. Contact->pos[0] = Origin[0] + T * Direction[0];
  19. Contact->pos[1] = Origin[1] + T * Direction[1];
  20. Contact->pos[2] = Origin[2] + T * Direction[2];
  21. Contact->pos[3] = REAL(0.0);
  22. //Contact->normal = 0;
  23. Contact->depth = 0.0f;
  24. Contact->g1 = RayGeom;
  25. Contact->g2 = PlaneGeom;
  26. return 1;
  27. }