home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch7_3 / collide.cc
Encoding:
C/C++ Source or Header  |  1995-03-04  |  4.4 KB  |  106 lines

  1. // -*- C++ -*-
  2. // by Bill Bouma and George Vanecek Jr. Aug, 1994.
  3. // Compile by: g++ -O2 -s -o cull cull.cc algebra3.o -lm
  4. #include "algebra3.h"           // See Graphics Gems IV, pg534-557
  5. typedef vec3          Point;    // Points are not Vectors
  6. typedef vec3          Vector;   // Vectors are not Points
  7. typedef unsigned int  Index;    // Array Indices
  8. typedef unsigned int  Counter;
  9.  
  10. class Polygon {
  11. public:
  12.   Polygon           ( const char         pId,
  13.                       const Vector&      nV,
  14.                       const Counter      nPs,
  15.                       const Point* const p )
  16.   : id(pId), pts(p), nPts(nPs), normalVector(nV){ }
  17.   const Vector& normal( ) const { return normalVector; }
  18.   char            name( ) const { return id; }
  19.   Counter      nPoints( ) const { return nPts; }
  20.   const Point&   point( const Index i ) const { return pts[i]; }
  21. private:
  22.   const char          id;            // Unique Id
  23.   const Counter       nPts;          // pts[0..nPts-1]
  24.   const Point*  const pts;           // Points around Polygon
  25.   const Vector        normalVector;  // Unit Vector
  26. };
  27.  
  28. class MovingPolyhedron {
  29. public:
  30.   MovingPolyhedron ( const char           pId,
  31.                      const Vector&        rv,
  32.                      const Vector&        vv,
  33.                      const Vector&        wv,
  34.                      const mat4&          m,
  35.                      const Counter        nP,
  36.                      const Polygon* const ps )
  37.   : id(pId), r(rv), v(vv), w(wv), R(m), polys(ps), nPolys(nP) { }
  38.   const Polygon&    polygon( const Index i ) const { return polys[i]; }
  39.   void                 cull( const MovingPolyhedron& ) const;
  40. private:
  41.   const char           id;      // Unique Id
  42.   const Polygon* const polys;   // Points in local coordinates
  43.   const Counter        nPolys;  // polys[0..nPolys-1]
  44.   Vector               r;       // Center of Rotation (in world coords.)
  45.   Vector               v;       // Linear Velocity (in world coords.)
  46.   Vector               w;       // Angular Velocity (in world coords.)
  47.   mat4                 R;       // Orientation Matrix
  48. };
  49.  
  50. void MovingPolyhedron::cull( const MovingPolyhedron& j ) const
  51. {
  52.   const mat4   RIi = ((mat4&)R).transpose();
  53.   const Vector aij = RIi * (v - j.v - (w ^ r) + (j.w ^ j.r)); 
  54.   const Vector wij = RIi * (j.w - w);
  55.   for( Index gi = 0; gi < nPolys; ++gi ) {
  56.     const Polygon& g = polygon(gi);
  57.     for( Index pi = 0; pi < g.nPoints(); ++pi )
  58.       if( ( aij + (g.point(pi) ^ wij )) * g.normal() > 0.0 )
  59.         break;
  60.     cout << "Polygon " << g.name() << " of Polyhedron " << id
  61.          << " is" << ( pi == g.nPoints()  ? " " : " not ")
  62.          << "culled." << endl;
  63.   }
  64. }
  65.  
  66. const Counter NPolyPoints = 4;
  67. const Counter NFaces      = 6;
  68. static const Point leftPoints[NPolyPoints]  = {
  69.   Point(-1,-1,-1), Point(-1,-1, 1), Point(-1, 1, 1), Point(-1, 1,-1) };
  70. static const Point rightPoints[NPolyPoints] = {
  71.   Point( 1,-1,-1), Point( 1, 1,-1), Point( 1, 1, 1), Point( 1,-1, 1) };
  72. static const Point topPoints[NPolyPoints]   = {
  73.   Point(-1, 1,-1), Point(-1, 1, 1), Point( 1, 1, 1), Point( 1, 1,-1) };
  74. static const Point bottomPoints[NPolyPoints]= {
  75.   Point(-1,-1,-1), Point( 1,-1,-1), Point( 1,-1, 1), Point(-1,-1, 1) };
  76. static const Point backPoints[NPolyPoints]  = {
  77.   Point(-1,-1,-1), Point(-1, 1,-1), Point( 1, 1,-1), Point( 1,-1,-1) };
  78. static const Point frontPoints[NPolyPoints] = {
  79.   Point(-1,-1, 1), Point( 1,-1, 1), Point( 1, 1, 1), Point(-1, 1, 1) };
  80. static const Polygon cube[NFaces]= {
  81.   Polygon( 'a', Vector(-1, 0, 0), NPolyPoints, leftPoints   ),
  82.   Polygon( 'b', Vector( 1, 0, 0), NPolyPoints, rightPoints  ),
  83.   Polygon( 'c', Vector( 0, 1, 0), NPolyPoints, topPoints    ),
  84.   Polygon( 'd', Vector( 0,-1, 0), NPolyPoints, bottomPoints ),
  85.   Polygon( 'e', Vector( 0, 0,-1), NPolyPoints, backPoints   ),
  86.   Polygon( 'f', Vector( 0, 0, 1), NPolyPoints, frontPoints  )
  87. };
  88.  
  89. int main()
  90. {
  91.   MovingPolyhedron A( 'A',
  92.                       Vector(10,10, 0 ), // Position
  93.                       Vector( 0, 0, 0 ), // Velocity
  94.                       Vector( 0, 0, 0 ), // Angular Velocity
  95.                       identity3D(),
  96.                       NFaces, cube );
  97.   MovingPolyhedron B( 'B',
  98.                       Vector(10,10,10 ), // Position
  99.                       Vector( 0, 0,-1 ), // Velocity
  100.                       Vector( 0, 1, 0 ), // Angular Velocity
  101.                       identity3D(),
  102.                       NFaces, cube );
  103.   A.cull( B );
  104.   B.cull( A );               
  105. }
  106.