step/stepcore
collisionsolver.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00023 #ifndef STEPCORE_COLLISIONSOLVER_H
00024 #define STEPCORE_COLLISIONSOLVER_H
00025
00026 #include "object.h"
00027 #include "world.h"
00028 #include "vector.h"
00029 #include "solver.h"
00030
00031 namespace StepCore
00032 {
00033
00034 class BasePolygon;
00035 class Body;
00036
00040 struct Contact {
00041 enum {
00042 Unknown = 0,
00044 Separated,
00045 Separating,
00046 Contacted,
00047 Colliding,
00048 Intersected
00049 };
00050 enum {
00051 UnknownType,
00052 PolygonPolygonType,
00053 PolygonDiskType,
00054 PolygonParticleType,
00055 DiskDiskType,
00056 DiskParticleType
00057 };
00058 int type;
00059 Body* body0;
00060 Body* body1;
00061 int state;
00062 double distance;
00063 Vector2d normal;
00064 Vector2d normalDerivative;
00065 int pointsCount;
00066 int pointsState[2];
00067 Vector2d points[2];
00068 double vrel[2];
00070
00071
00072 int _w1[2];
00073 };
00074
00080 class CollisionSolver : public Object
00081 {
00082 STEPCORE_OBJECT(CollisionSolver)
00083
00084 public:
00085 CollisionSolver(): _toleranceAbs(0.001), _localError(0) {}
00086 virtual ~CollisionSolver() {}
00087
00089 double toleranceAbs() const { return _toleranceAbs; }
00091 virtual void setToleranceAbs(double toleranceAbs) { _toleranceAbs = toleranceAbs; }
00093 double localError() const { return _localError; }
00094
00099
00100
00106 virtual int checkContacts(BodyList& bodies,
00107 ConstraintsInfo* info, bool collisions = false) = 0;
00108
00109
00112 virtual int solveCollisions(BodyList& bodies) = 0;
00113
00117 virtual void resetCaches() {}
00118
00119 virtual void bodyAdded(BodyList&, Body*) {}
00120 virtual void bodyRemoved(BodyList&, Body*) {}
00121
00122 public:
00123 enum {
00124 InternalError = Solver::CollisionError
00125 };
00126
00127 protected:
00128 double _toleranceAbs;
00129
00130 double _localError;
00131 };
00132
00133 typedef std::vector<Contact> ContactValueList;
00134
00144 class GJKCollisionSolver : public CollisionSolver
00145 {
00146 STEPCORE_OBJECT(GJKCollisionSolver)
00147
00148 public:
00149 GJKCollisionSolver() : _contactsIsValid(false) {}
00150
00151
00152 GJKCollisionSolver(const GJKCollisionSolver& solver)
00153 : CollisionSolver(solver), _contactsIsValid(false) {}
00154 GJKCollisionSolver& operator=(const GJKCollisionSolver&) {
00155 _contactsIsValid = false; return *this; }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 int checkContacts(BodyList& bodies, ConstraintsInfo* info, bool collisions = false);
00166
00167
00168 int solveCollisions(BodyList& bodies);
00169
00170
00171 void resetCaches();
00172 void bodyAdded(BodyList& bodies, Body* body);
00173 void bodyRemoved(BodyList& bodies, Body* body);
00174
00175 protected:
00176 int checkContact(Contact* contact);
00177
00178 int checkPolygonPolygon(Contact* contact);
00179 int solvePolygonPolygon(Contact* contact);
00180
00181 int checkPolygonParticle(Contact* contact);
00182 int solvePolygonParticle(Contact* contact);
00183
00184 int checkPolygonDisk(Contact* contact);
00185 int solvePolygonDisk(Contact* contact);
00186
00187 int checkDiskDisk(Contact* contact);
00188 int solveDiskDisk(Contact* contact);
00189
00190 int checkDiskParticle(Contact* contact);
00191 int solveDiskParticle(Contact* contact);
00192
00193 void addContact(Body* body0, Body* body1);
00194 void checkCache(BodyList& bodies);
00195
00196 protected:
00197 ContactValueList _contacts;
00198 bool _contactsIsValid;
00199 };
00200
00201 }
00202
00203 #endif
00204