Open Dynamics Engine

collision_std.h

00001 /*************************************************************************
00002  *                                                                       *
00003  * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       *
00004  * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
00005  *                                                                       *
00006  * This library is free software; you can redistribute it and/or         *
00007  * modify it under the terms of EITHER:                                  *
00008  *   (1) The GNU Lesser General Public License as published by the Free  *
00009  *       Software Foundation; either version 2.1 of the License, or (at  *
00010  *       your option) any later version. The text of the GNU Lesser      *
00011  *       General Public License is included with this library in the     *
00012  *       file LICENSE.TXT.                                               *
00013  *   (2) The BSD-style license that is included with this library in     *
00014  *       the file LICENSE-BSD.TXT.                                       *
00015  *                                                                       *
00016  * This library is distributed in the hope that it will be useful,       *
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
00019  * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
00020  *                                                                       *
00021  *************************************************************************/
00022 
00023 /*
00024 
00025 the standard ODE geometry primitives.
00026 
00027 */
00028 
00029 #ifndef _ODE_COLLISION_STD_H_
00030 #define _ODE_COLLISION_STD_H_
00031 
00032 #include <ode/common.h>
00033 #include "collision_kernel.h"
00034 
00035 
00036 // primitive collision functions - these have the dColliderFn interface, i.e.
00037 // the same interface as dCollide(). the first and second geom arguments must
00038 // have the specified types.
00039 
00040 int dCollideSphereSphere (dxGeom *o1, dxGeom *o2, int flags,
00041            dContactGeom *contact, int skip);
00042 int dCollideSphereBox (dxGeom *o1, dxGeom *o2, int flags,
00043              dContactGeom *contact, int skip);
00044 int dCollideSpherePlane (dxGeom *o1, dxGeom *o2, int flags,
00045           dContactGeom *contact, int skip);
00046 int dCollideBoxBox (dxGeom *o1, dxGeom *o2, int flags,
00047           dContactGeom *contact, int skip);
00048 int dCollideBoxPlane (dxGeom *o1, dxGeom *o2,
00049             int flags, dContactGeom *contact, int skip);
00050 int dCollideCapsuleSphere (dxGeom *o1, dxGeom *o2, int flags,
00051               dContactGeom *contact, int skip);
00052 int dCollideCapsuleBox (dxGeom *o1, dxGeom *o2, int flags,
00053            dContactGeom *contact, int skip);
00054 int dCollideCapsuleCapsule (dxGeom *o1, dxGeom *o2,
00055             int flags, dContactGeom *contact, int skip);
00056 int dCollideCapsulePlane (dxGeom *o1, dxGeom *o2, int flags,
00057              dContactGeom *contact, int skip);
00058 int dCollideRaySphere (dxGeom *o1, dxGeom *o2, int flags,
00059              dContactGeom *contact, int skip);
00060 int dCollideRayBox (dxGeom *o1, dxGeom *o2, int flags,
00061           dContactGeom *contact, int skip);
00062 int dCollideRayCapsule (dxGeom *o1, dxGeom *o2,
00063            int flags, dContactGeom *contact, int skip);
00064 int dCollideRayPlane (dxGeom *o1, dxGeom *o2, int flags,
00065             dContactGeom *contact, int skip);
00066 int dCollideRayCylinder (dxGeom *o1, dxGeom *o2, int flags,
00067             dContactGeom *contact, int skip);
00068 
00069 // Cylinder - Box/Sphere by (C) CroTeam
00070 // Ported by Nguyen Binh
00071 int dCollideCylinderBox(dxGeom *o1, dxGeom *o2, 
00072                         int flags, dContactGeom *contact, int skip);
00073 int dCollideCylinderSphere(dxGeom *gCylinder, dxGeom *gSphere, 
00074                            int flags, dContactGeom *contact, int skip); 
00075 int dCollideCylinderPlane(dxGeom *gCylinder, dxGeom *gPlane, 
00076                            int flags, dContactGeom *contact, int skip); 
00077 
00078 //--> Convex Collision
00079 int dCollideConvexPlane (dxGeom *o1, dxGeom *o2, int flags,
00080           dContactGeom *contact, int skip);
00081 int dCollideSphereConvex (dxGeom *o1, dxGeom *o2, int flags,
00082            dContactGeom *contact, int skip);
00083 int dCollideConvexBox (dxGeom *o1, dxGeom *o2, int flags,
00084              dContactGeom *contact, int skip);
00085 int dCollideConvexCapsule (dxGeom *o1, dxGeom *o2,
00086             int flags, dContactGeom *contact, int skip);
00087 int dCollideConvexConvex (dxGeom *o1, dxGeom *o2, int flags, 
00088            dContactGeom *contact, int skip);
00089 int dCollideRayConvex (dxGeom *o1, dxGeom *o2, int flags, 
00090              dContactGeom *contact, int skip);
00091 //<-- Convex Collision
00092 
00093 // dHeightfield
00094 int dCollideHeightfield( dxGeom *o1, dxGeom *o2, 
00095                    int flags, dContactGeom *contact, int skip );
00096 
00097 //****************************************************************************
00098 // the basic geometry objects
00099 
00100 struct dxSphere : public dxGeom {
00101   dReal radius;      // sphere radius
00102   dxSphere (dSpaceID space, dReal _radius);
00103   void computeAABB();
00104 };
00105 
00106 
00107 struct dxBox : public dxGeom {
00108   dVector3 side;  // side lengths (x,y,z)
00109   dxBox (dSpaceID space, dReal lx, dReal ly, dReal lz);
00110   void computeAABB();
00111 };
00112 
00113 
00114 struct dxCapsule : public dxGeom {
00115   dReal radius,lz;   // radius, length along z axis
00116   dxCapsule (dSpaceID space, dReal _radius, dReal _length);
00117   void computeAABB();
00118 };
00119 
00120 
00121 struct dxCylinder : public dxGeom {
00122         dReal radius,lz;        // radius, length along z axis
00123         dxCylinder (dSpaceID space, dReal _radius, dReal _length);
00124         void computeAABB();
00125 };
00126 
00127 
00128 struct dxPlane : public dxGeom {
00129   dReal p[4];
00130   dxPlane (dSpaceID space, dReal a, dReal b, dReal c, dReal d);
00131   void computeAABB();
00132 };
00133 
00134 
00135 struct dxRay : public dxGeom {
00136   dReal length;
00137   dxRay (dSpaceID space, dReal _length);
00138   void computeAABB();
00139 };
00140 
00141 struct dxConvex : public dxGeom 
00142 {  
00143   dReal *planes; 
00146   dReal *points; 
00147   unsigned int *polygons; 
00148   unsigned int planecount; 
00149   unsigned int pointcount;
00150   unsigned int edgecount;
00151   dReal saabb[6];
00152   dxConvex(dSpaceID space,
00153       dReal *planes,
00154       unsigned int planecount,
00155       dReal *points,
00156       unsigned int pointcount,
00157       unsigned int *polygons);
00158   ~dxConvex()
00159   {
00160      if((edgecount!=0)&&(edges!=NULL)) delete[] edges;
00161   }
00162   void computeAABB();
00163   struct edge
00164   {
00165    unsigned int first;
00166    unsigned int second;
00167   };
00168   edge* edges;
00169 
00174    inline unsigned int SupportIndex(dVector3 dir)
00175    {
00176       dVector3 rdir;
00177       unsigned int index=0;
00178       dMultiply1_331 (rdir,final_posr->R,dir);
00179       dReal max = dCalcVectorDot3(points,rdir);
00180       dReal tmp;
00181       for (unsigned int i = 1; i < pointcount; ++i) 
00182       {
00183          tmp = dCalcVectorDot3(points+(i*3),rdir);
00184          if (tmp > max) 
00185          {
00186             index=i;
00187             max = tmp; 
00188          }
00189       }
00190       return index;
00191    }
00192 
00193  private:
00194   // For Internal Use Only
00197   void FillEdges();
00198 #if 0
00199   /*
00200   What this does is the same as the Support function by doing some preprocessing
00201   for optimization. Not complete yet.
00202   */
00203   // Based on Eberly's Game Physics Book page 307
00204   struct Arc
00205   {
00206      // indices of polyhedron normals that form the spherical arc
00207      int normals[2];
00208      // index of edge shared by polyhedron faces
00209      int edge;
00210   };
00211   struct Polygon
00212   {
00213      // indices of polyhedron normals that form the spherical polygon
00214      std::vector<int> normals;
00215      // index of extreme vertex corresponding to this polygon
00216      int vertex;
00217   };
00218   // This is for extrem feature query and not the usual level BSP structure (that comes later)
00219   struct BSPNode
00220   {
00221    // Normal index (interior node), vertex index (leaf node)
00222    int normal;
00223    // if Dot (E,D)>=0, D gets propagated to this child
00224    BSPNode* right;
00225    // if Dot (E,D)<0, D gets propagated to this child
00226    BSPNode* left;
00227   };
00228   void CreateTree();
00229   BSPNode* CreateNode(std::vector<Arc> Arcs,std::vector<Polygon> Polygons);
00230   void GetFacesSharedByVertex(int i, std::vector<int> f);
00231   void GetFacesSharedByEdge(int i, int* f);
00232   void GetFaceNormal(int i, dVector3 normal);
00233   BSPNode* tree;
00234 #endif
00235 };
00236 
00237 
00238 #endif