-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathPhysicsCollision.c
90 lines (77 loc) · 2.91 KB
/
PhysicsCollision.c
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
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2014-7-23
* Update : 2019-1-18
* Author : scott.cgi
*/
#include "Engine/Physics/PhysicsCollision.h"
typedef enum
{
PhysicsShape_PolygonPolygon = PhysicsShape_Polygon,
PhysicsShape_PolygonLine = PhysicsShape_Polygon | PhysicsShape_Line,
PhysicsShape_LineLine = PhysicsShape_Line,
PhysicsShape_PolygonPoint = PhysicsShape_Polygon | PhysicsShape_Point,
}
PhysicsShapeCollision;
static bool TestCollision(PhysicsBody* bodyA, PhysicsBody* bodyB)
{
PhysicsShapeCollision shapeCollision = (PhysicsShapeCollision) (bodyA->shape | bodyB->shape);
switch (shapeCollision)
{
case PhysicsShape_PolygonPolygon:
return AMath->TestPolygonPolygon(bodyA->transformedVertexArr, bodyB->transformedVertexArr);
case PhysicsShape_PolygonLine:
// only consider line vertex in polygon
if (bodyA->shape == PhysicsShape_Line)
{
return AMath->TestPolygonABStrict(bodyA->transformedVertexArr, bodyB->transformedVertexArr);
}
else
{
return AMath->TestPolygonABStrict(bodyB->transformedVertexArr, bodyA->transformedVertexArr);
}
case PhysicsShape_LineLine:
return AMath->TestLineLine(bodyA->transformedVertexArr, bodyB->transformedVertexArr);
case PhysicsShape_PolygonPoint:
// only consider point in polygon
if (bodyA->shape == PhysicsShape_Polygon)
{
return AMath->TestPolygonPoint
(
bodyA->transformedVertexArr,
AArray_Get(bodyB->transformedVertexArr, 0, float),
AArray_Get(bodyB->transformedVertexArr, 1, float)
);
}
else
{
return AMath->TestPolygonPoint
(
bodyB->transformedVertexArr,
AArray_Get(bodyA->transformedVertexArr, 0, float),
AArray_Get(bodyA->transformedVertexArr, 1, float)
);
}
default:
ALog_A
(
false,
"APhysicsCollision cannot test collision between shape %d and %d",
bodyA->shape,
bodyB->shape
);
}
return false;
}
struct APhysicsCollision APhysicsCollision[1] =
{{
TestCollision,
}};