-
Notifications
You must be signed in to change notification settings - Fork 0
/
circleAndRectCollide.html
161 lines (139 loc) · 5.12 KB
/
circleAndRectCollide.html
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
</head>
<body>
<div class="main">
<canvas id="canvas" width="600" height="400"></canvas>
</div>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = ctx.stokeStyle = '#ccc';
ctx.fillRect(0, 0, canvas.width, canvas.height);
var rect2 = {
x : 300,
y : 100,
width : 100,
height : 50,
angle : 60
};
var cir = {
x : 400,
y : 200,
rad : 20
}
circleRectCollide(rect2, cir);
function circleRectCollide (obj1, obj2) {
var rectPoint = rotateRect(obj1);
var circle = obj2;
ctx.beginPath();
ctx.arc(circle.x, circle.y, circle.rad, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
console.dir(rectPoint);
for (var i = 0; i < rectPoint.length; i++) {
var point = rectPoint[i];
var distance = Math.sqrt((circle.x - point.x) * (circle.x - point.x) + (circle.y - point.y) * (circle.y - point.y));
if (distance < circle.rad) {
console.log('collide');
return true;
}
var point2 = rectPoint[i + 1];
if (i == rectPoint.length - 1) {
point2 = rectPoint[0];
}
// 获得一条边的向量
var vector = {x : point2.x - point.x, y : point2.y - point.y};
// 圆心到该顶点的向量
var circleToPoint = {x : circle.x - point.x, y : circle.y - point.y};
var angle = Math.atan2(vector.y, vector.x) - Math.atan2(circleToPoint.y, circleToPoint.x);
console.log(point.x, point.y);
var dis = Math.sin(angle) * Math.sqrt(circleToPoint.x * circleToPoint.x + circleToPoint.y * circleToPoint.y);
if (dis < circle.rad) {
console.log('collide line');
return true;
}
}
console.log('no collide');
return false;
}
/*返回旋转后的坐标数组*/
function rotateRect(obj) {
// 画出未旋转的图形
ctx.fillStyle = '#000';
ctx.fillRect(obj.x, obj.y, obj.width, obj.height);
var points = [
{
x : -obj.width / 2,
y : -obj.height / 2
},
{
x : obj.width / 2,
y : -obj.height / 2
},
{
x : obj.width / 2,
y : obj.height / 2
},
{
x : -obj.width / 2,
y : obj.height / 2
}
];
var centerPoint = {
x : obj.x + obj.width / 2,
y : obj.y + obj.height / 2
};
var resPoint = [];
for (var i = 0 ; i < points.length; i++) {
var point = points[i];
var pointTmp = getRotatePointPos(point, obj.angle);
resPoint.push({x : pointTmp.x + centerPoint.x, y : pointTmp.y + centerPoint.y});
}
ctx.fillStyle = '#f00';
ctx.beginPath();
for (var j = 0; j < resPoint.length; j++) {
var point = resPoint[j];
ctx.lineTo(point.x, point.y);
}
ctx.closePath();
ctx.fill();
return resPoint;
}
/*获取旋转之后的坐标*/
function getRotatePointPos (point, angle) {
var matrix = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 1]
];
var cos = Math.cos,
sin = Math.sin,
angle = angle / 180 * Math.PI;
matrix[0][0] = cos(angle);
matrix[0][1] = sin(angle);
matrix[1][0] = -sin(angle);
matrix[1][1] = cos(angle);
var res = {};
res.x = point.x * cos(angle) - point.y * sin(angle);
res.y = point.x * sin(angle) + point.y * cos(angle);
return res;
}
/*获得该点在在向量上的投影点坐标*/
function vectorToPoint (vector, point) {
var slope = (point.x * vector.x + point.y * vector.y) / (vector.x * vector.x + vector.y * vector.y);
// 获取对应的投影点
var pointTmp = {
x : slope * vector.x,
y : slope * vector.y
};
// console.log(vector.x, vector.y, pointTmp.x, pointTmp.y);
// console.dir(pointTmp);
// 返回标量值
return slope;
}
</script>
</body>
</html>