-
Notifications
You must be signed in to change notification settings - Fork 0
/
collision.h
113 lines (92 loc) · 2.97 KB
/
collision.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once
#include "mathlib.h"
#include "octree.h"
namespace Collision {
class Triangle {
public:
// bounding box
Math::Vector3 mMin;
Math::Vector3 mMax;
Math::Vector3 mNormal;
// vertices
Math::Vector3 mA;
Math::Vector3 mB;
Math::Vector3 mC;
// edges
Math::Vector3 mAB;
Math::Vector3 mBC;
Math::Vector3 mCA;
// precalculated, for barycentric method
Math::Vector3 mBA;
float mCADotCA;
float mCADotBA;
float mBADotBA;
float mInvDenom;
// precalculated edge rays
Math::Ray mABRay;
Math::Ray mBCRay;
Math::Ray mCARay;
// precalculated distance from center of coordinates
float mDistance;
Triangle( const Math::Vector3 & a, const Math::Vector3 & b, const Math::Vector3 & c );
bool CheckPoint( const Math::Vector3 & point ) const;
bool IsIntersectAABB( const Math::Vector3 & bbMin, const Math::Vector3 & bbMax );
bool IsIntersectRay( const Math::Ray & ray, Math::Vector3 & intPoint );
};
class Contact {
public:
Math::Vector3 mNormal;
Math::Vector3 mPosition;
class Body * mBody;
};
Math::Vector3 ProjectPointOnLine( const Math::Vector3 & point, const Math::Vector3 & a, const Math::Vector3 & b );
Math::Vector3 ProjectVectorOnPlane( const Math::Vector3 & a, const Math::Vector3 & planeNormal );
bool IsPointInsideAABB( const Math::Vector3 & point, const Math::Vector3 & a, const Math::Vector3 & b );
class RayTraceResult {
public:
Math::Vector3 mPosition;
Math::Vector3 mNormal;
class Body * mBody;
};
bool TraceRay( const Math::Ray & ray, RayTraceResult & out, class Body * ignoreBody = nullptr );
class Body {
public:
static Body * msRoot;
Body * mNext;
Body * mPrev;
enum class Type {
Sphere,
Polygon
};
Math::Vector3 mPosition;
Math::Vector3 mLinearVelocity;
Math::Vector3 mGravity;
static const int MaxContacts = 4;
Contact mContacts[MaxContacts];
int mContactCount;
Octree * mOctree;
Type mType;
// Sphere
float mRadius;
// Collision filtering
Array<Body*> mIgnoreBodies;
// Polygon
Triangle * mTriangles;
int mTriangleCount;
Body( Type type );
~Body();
static void DeleteAll();
void AddContact( const Math::Vector3 & point, const Math::Vector3 & normal, Body * body );
void AddTriangles( const Vertex * vertices, int vertexCount );
void BuildOctree( );
bool IsSphereRayIntersection( const Math::Ray & ray, Math::Vector3 * closestIntersectionPoint = nullptr );
bool IsSpherePointIntersection( const Math::Vector3 & point, Math::Vector3 & intersectionPoint, bool & isEdgeIntersection );
bool IsSphereEdgeIntersection( const Math::Ray & edge, Math::Vector3 & intersectionPoint, bool & isEdgeIntersection );
bool IsSphereTriangleIntersection( const Triangle & triangle, Math::Vector3 & intersectionPoint, bool & isEdgeIntersection );
void SolveSpherePolygonCollision( Body * polygon );
bool IsSphereSphereIntersection( Body * sphere, float & outPenetrationDepth );
void SolveSphereSphereCollision( Body * sphere );
static void SolveCollisions();
static void PushBodies( const Math::Vector3 & point, float radius, float strength );
};
};