-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collider.cs
142 lines (133 loc) · 4.09 KB
/
Collider.cs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace My2D_Engine
{
class Collider
{
public int ColliderID;
public GameObject gameObj;
public double r;
public Collider() { }
public Collider(GameObject gobj)
{
gameObj = gobj;
}
public bool isCollideWith(Collider collider)
{
return ColliderDectDelegate.Collide(this, collider);
}
}
class RoundCollider : Collider
{
public RoundCollider()
{
base.ColliderID = 1;
}
public double r;
public bool isCollideWith(Collider collider)
{
return false;
}
}
class RectCollider : Collider
{
public double EdgeHorizontal;
public double EdgeVertical;
public Rectangle box = new Rectangle();
public Point point = new Point();
public double r()
{
return Math.Sqrt((EdgeHorizontal * EdgeHorizontal + EdgeVertical * EdgeVertical) / 2);
}
public RectCollider(double hor, double ver)
{
base.ColliderID = 2;
EdgeHorizontal = hor;
EdgeVertical = ver;
}
public void setBoundary(double hor, double ver)
{
EdgeHorizontal = hor;
EdgeVertical = ver;
}
public bool isCollideWith(Collider collider)
{
return false;
}
}
class SelfDEfCollider : Collider
{
public SelfDEfCollider()
{
base.ColliderID = 3;
}
public SelfDEfCollider(List<_Point> points)
{
base.ColliderID = 3;
this.points = points;
}
public List<_Point> points;
public bool isCollideWith(Collider collider)
{
return false;
}
}
class DefaultCollider : Collider
{
public DefaultCollider()
{
base.ColliderID = 0;
}
public bool isCollideWith(Collider collider)
{
return false;
}
}
static class ColliderDectDelegate
{
public static bool Collide(Collider co1, Collider co2)
{
if (co1.ColliderID + co2.ColliderID == 2)
{
return co1.gameObj.location._DistanceSquare(co2.gameObj.location) <= (co1.r + co2.r) * (co1.r + co2.r);
}
if (co1.ColliderID + co2.ColliderID == 3)
{
if(co1.ColliderID == 1)
{
return CrossCircleAndRectangle((RoundCollider)co1, (RectCollider)co2);
}
else
{
return CrossCircleAndRectangle((RoundCollider)co2, (RectCollider)co1);
}
}
if (co1.ColliderID == 2 && co2.ColliderID == 2)
{
return RectAndRect((RectCollider)co1, (RectCollider)co2);
}
//then self def
//
return false;
}
public static bool CrossCircleAndRectangle(RoundCollider roc, RectCollider rec)
{
VectorXY v = new VectorXY(Math.Abs(roc.gameObj.location.xPoint - rec.gameObj.location.xPoint), Math.Abs(roc.gameObj.location.yPoint - rec.gameObj.location.yPoint));
VectorXY h = new VectorXY(rec.EdgeHorizontal / 2, rec.EdgeVertical / 2);
VectorXY u = v - h;
u.vx = (Math.Max(u.vx, 0));
u.vy = (Math.Max(u.vy, 0));
return u.NormSuquare() - Math.Pow(roc.r, 2) <= 0;
}
public static bool RectAndRect(RectCollider rec1,RectCollider rec2)
{
Rectangle r1 = new Rectangle((int)rec1.gameObj.location.xPoint, (int)rec1.gameObj.location.yPoint, (int)rec1.EdgeHorizontal, (int)rec1.EdgeVertical);
Rectangle r2 = new Rectangle((int)rec2.gameObj.location.xPoint, (int)rec2.gameObj.location.yPoint, (int)rec2.EdgeHorizontal, (int)rec2.EdgeVertical);
return r1.IntersectsWith(r2);
}
}
}