home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 09 Racing and Sports AI / 03 Biasillo / Listing1.cpp
Encoding:
C/C++ Source or Header  |  2001-12-09  |  1.6 KB  |  50 lines

  1. /* Copyright (C) Gari Biasillo, 2001. 
  2.  * All rights reserved worldwide.
  3.  *
  4.  * This software is provided "as is" without express or implied
  5.  * warranties. You may freely copy and compile this source into
  6.  * applications you distribute provided that the copyright text
  7.  * below is included in the resulting source code, for example:
  8.  * "Portions Copyright (C) Gari Biasillo, 2001"
  9.  */
  10.  
  11. // To determine where the car intersects the interface, a 
  12. // line-to-plane intersection test is calculated; the car's 
  13. // previous and current position specify the line and the starting 
  14. // interface of the newly occupied sector defines the plane. Because 
  15. // this test is only performed when the car enters a new sector, we 
  16. // can guarantee that the line straddles the plane.
  17.  
  18.  
  19. // Plane Equation = Ax + By + Cz + D = 0, where
  20. // A = normal.x, B = normal.y, C = normal.z, D = dist
  21. // x,y,z = vector3 to test against plane, therefore
  22. // Dist to plane = DotProduct(vector3, normal) + dist
  23. class CPlane
  24. {
  25.     public:
  26.         vector3   normal;
  27.         float     dist;
  28. } ;
  29.  
  30. vector3 LineToPlane(const vector3& start, const vector3& end, const
  31.                     CPlane& plane)
  32. {
  33.     float s, e, t;
  34.     s = DotProduct(plane.normal, start) + plane.dist;
  35.     e = DotProduct(plane.normal, end) + plane.dist;
  36.     t = s / (s - e);
  37.     vector3 delta = end - start;
  38.     return (start + delta * t);
  39. }
  40.  
  41. float TimeOfPointOnLine(const vector3& point, const vector3& start, 
  42.                         vector3& end)
  43. {
  44.     vector3 delta = end - start;
  45.     float length = delta.length();
  46.     delta = point - start;
  47.     float t = delta.length();
  48.     return (t / length);
  49. }
  50.