-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcollisionfinder.h
62 lines (54 loc) · 1.99 KB
/
collisionfinder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef COLLISIONFINDER_H
#define COLLISIONFINDER_H
#include <vector>
#include "algebra.h"
#include "MyShape.h"
#include <iostream>
class GraphicManager;
struct ContactPoint{
Vector2 position;
Vector2 rA,rB;
double normalMass;
double tangentMass;
double normalImpulse;
double tangentImpulse;
double separatingVel;
};
struct ContactConstraint{
ContactPoint points[2];
int pointCount;// 1 or 2
int shapeIndexA;
int shapeIndexB;
double invMassA, invMassB;
double invIA,invIB;
double friction;
double restitution;
Vector2 normal;
Matrix2 A;
Matrix2 invA;
//for position correction:
enum Type{circles,faceA,faceB};
Type type;
Vector2 localFacePoint;
Vector2 localContact[2];
Vector2 localNormal;
};
class CollisionFinder
{
GraphicManager* debugDrawer;
bool debugDraw;
bool shouldContact(const MyShape& a,const MyShape& b);
bool SATtwoShape(MyShape *a, MyShape *b, Vector2& n, Vector2& contactPoint1, Vector2& contactPoint2, int& numOfContact, ContactConstraint::Type& contactType, Vector2 &facePoint);
bool GetConstraint(int indexA, int indexB, MyShape *pa, MyShape *pb, ContactConstraint& constraint);
bool SATPolygon(MyPolygon *pa, MyPolygon *pb, Vector2& n, Vector2& contactPoint1, Vector2& contactPoint2, int& numOfContact, ContactConstraint::Type& contactType, Vector2& facePoint);
bool SATCircle(MyCircle *pa, MyCircle *pb,Vector2& n, Vector2& contactPoint);
bool SATPolygonCircle(MyPolygon *pa, MyCircle *pb, Vector2& n, Vector2& contactPoint, Vector2& facePoint);
void clipSegment(Vector2& l1,Vector2& l2,Vector2 clipPoint,Vector2 n);
void getMinFromProjection(const std::vector<Vector2>& v,Vector2 n,double& min,std::vector<Vector2>::const_iterator& min_i);
public:
CollisionFinder();
void turnOnDebugDraw();
std::vector<ContactConstraint> FindCollisions(std::vector<MyShape *> &shapes);
void setDebugDrawer(GraphicManager *debugDrawer);
};
#endif // COLLISIONFINDER_H