Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

physics/GJKCollisionDetector

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)1996 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: GJKCollisionDetector 1031 2004-02-11 20:46:36Z jungd $
00019   $Revision: 1.6 $
00020   $Date: 2004-02-11 15:46:36 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #ifndef _PHYSICS_GJKCOLLISIONDETECTOR_
00026 #define _PHYSICS_GJKCOLLISIONDETECTOR_
00027 
00028 #include <physics/physics>
00029 #include <base/array>
00030 #include <physics/CollisionDetector>
00031 #include <physics/GJKCollisionModel>
00032 #include <base/Transform>
00033 
00034 #include <osg/Array>
00035 
00036 
00037 namespace physics {
00038 
00039 
00040 class GJKCollisionDetector : public CollisionDetector
00041 {
00042 public:
00043   GJKCollisionDetector();
00044   virtual ~GJKCollisionDetector();
00045 
00046   virtual String className() const { return String("GJKCollisionDetector"); }
00047   virtual Object& clone() const { return *NewNamedObj(className()) GJKCollisionDetector(); }
00048 
00049   virtual CollisionModel::CollisionModelType getType() const
00050     { return CollisionModel::GJKModel; }
00051 
00052   virtual bool collision(ref<const Solid> solid1, ref<const Solid> solid2, CollisionQueryType queryType);
00053   
00054   
00055   virtual bool collisionEnable(bool enabled) 
00056     {
00057       bool oldEnable = collisionDetectionEnabled;
00058       collisionDetectionEnabled=enabled; 
00059       return oldEnable;
00060     }
00061   
00062   // Visual
00063   virtual bool visualTypeSupported(VisualType type) const { return (type==Visual::OSGVisual); }
00064 
00065   virtual osg::Node* createOSGVisual(Visual::Attributes visualAttributes=0) const;
00066 
00067   
00068 protected:
00069   bool collisionDetectionEnabled;
00070   
00071   virtual ref<CollisionState> newCollisionState(ref<const Solid> solid1, ref<const Solid> solid2) const;
00072 
00073   
00074   class GJKCollisionState : public CollisionState
00075   {
00076   public:
00077     GJKCollisionState(ref<const Solid> solid1, ref<const Solid> solid2, CollisionModel::CollisionModelType modelType);
00078     virtual ~GJKCollisionState();
00079 
00080     Vector3 v;
00081     ref<const Polyhedron::Vertex> lastSupport[2]; /// remember the last support vertices
00082     Point3 lastSupportPoint[2];                   /// and their local coords.
00083 
00084     virtual String className() const { return String("GJKCollisionState"); }
00085   };
00086 
00087 
00088   // GJK algorithm proper
00089   bool intersect(ref<const GJKCollisionModel> model1, ref<const GJKCollisionModel> model2,
00090                  const base::Transform& t1, const base::Transform& t2, 
00091                  ref<GJKCollisionModel::GJKModelState> modelState1,
00092                  ref<GJKCollisionModel::GJKModelState> modelState2,
00093                  ref<GJKCollisionState> collisionState);
00094 
00095   bool closest(Vector3& v);
00096   void compute_vector(Int bits, Vector3& v);
00097   void compute_points(int bits, Point3& p1, Point3& p2);
00098   bool valid(Int s);
00099   void compute_det();
00100 
00101 
00102   Point3  p[4];    // support points of object A in local coordinates 
00103   Point3  q[4];    // support points of object B in local coordinates 
00104   Vector3 y[4];    // support points of A - B in world coordinates
00105   
00106   Int bits;      // identifies current simplex
00107   Int last;      // identifies last found support point
00108   Int last_bit;  // last_bit = 1<<last
00109   Int all_bits;  // all_bits = bits|last_bit 
00110   
00111   Real det[16][4]; // cached sub-determinants
00112 
00113   mutable Visual::Attributes attributes;
00114   mutable ref_ptr<osg::Node> node;
00115   mutable osg::Vec3Array* verts;
00116   mutable osg::ref_ptr<osg::Geometry> geom;
00117 };
00118 
00119 
00120 } // physics
00121 
00122 #endif

Generated on Thu Jul 29 15:56:23 2004 for OpenSim by doxygen 1.3.6