IceRay.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // Precompiled Header
  50. #include "Stdafx.h"
  51. using namespace IceMaths;
  52. float Ray::SquareDistance(const Point& point, float* t) const
  53. {
  54. Point Diff = point - mOrig;
  55. float fT = Diff | mDir;
  56. if(fT<=0.0f)
  57. {
  58. fT = 0.0f;
  59. }
  60. else
  61. {
  62. fT /= mDir.SquareMagnitude();
  63. Diff -= fT*mDir;
  64. }
  65. if(t) *t = fT;
  66. return Diff.SquareMagnitude();
  67. }