2
0

IceRay.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains code for rays.
  4. * \file IceRay.cpp
  5. * \author Pierre Terdiman
  6. * \date April, 4, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. /**
  11. * Ray class.
  12. * A ray is a half-line P(t) = mOrig + mDir * t, with 0 <= t <= +infinity
  13. * \class Ray
  14. * \author Pierre Terdiman
  15. * \version 1.0
  16. */
  17. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. /*
  19. O = Origin = impact point
  20. i = normalized vector along the x axis
  21. j = normalized vector along the y axis = actually the normal vector in O
  22. D = Direction vector, norm |D| = 1
  23. N = Projection of D on y axis, norm |N| = normal reaction
  24. T = Projection of D on x axis, norm |T| = tangential reaction
  25. R = Reflexion vector
  26. ^y
  27. |
  28. |
  29. |
  30. _ _ _| _ _ _
  31. * * *|
  32. \ | /
  33. \ |N / |
  34. R\ | /D
  35. \ | / |
  36. \ | /
  37. _________\|/______*_______>x
  38. O T
  39. Let define theta = angle between D and N. Then cos(theta) = |N| / |D| = |N| since D is normalized.
  40. j|D = |j|*|D|*cos(theta) => |N| = j|D
  41. Then we simply have:
  42. D = N + T
  43. To compute tangential reaction :
  44. T = D - N
  45. To compute reflexion vector :
  46. R = N - T = N - (D-N) = 2*N - D
  47. */
  48. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. #include "../Opcode.h"
  50. using namespace IceMaths;
  51. float Ray::SquareDistance(const Point& point, float* t) const
  52. {
  53. Point Diff = point - mOrig;
  54. float fT = Diff | mDir;
  55. if(fT<=0.0f)
  56. {
  57. fT = 0.0f;
  58. }
  59. else
  60. {
  61. fT /= mDir.SquareMagnitude();
  62. Diff -= fT*mDir;
  63. }
  64. if(t) *t = fT;
  65. return Diff.SquareMagnitude();
  66. }