-
Notifications
You must be signed in to change notification settings - Fork 0
/
fomula
118 lines (88 loc) · 2.48 KB
/
fomula
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
三角函数
sine of angle = opposite / hypotenuse
cosine of angle = adjacent / hypotenuse
tangent of angle = opposite / adjacent
角度弧度
radians = degrees * Math.PI / 180 ;
degrees = radians * 180 / Math.PI ;
朝鼠标(或者任意一点)旋转
dx = mouse.x - object.x;
dy = mouse.y - object.y;
object.rotation = Math.atan2(dy, dx) * 180 / Math.PI;
创建波
(function drawFrame() {
window.requestAnimationFrame(drawFrame, canvas);
value = center + Math.sin(angle) * range;
angle += speed;
})();
创建圆形
(function drawFrame() {
window.requestAnimationFrame(drawFrame, canvas);
xposition = centerX + Math.cos(angle) * radius;
yposition = centerY + Math.sin(angle) * radius;
angle += speed;
})();
创建椭圆形
(function drawFrame() {
window.requestAnimationFrame(drawFrame, canvas);
xposition = centerX + Math.cos(angle) * radiusX;
yposition = centerY + Math.sin(angle) * radiusY;
angle += speed;
})();
获取两点间的距离
dx = x2 - x1;
dy = y2 - y1;
dist = Math.sqrt(dx * dx + dy * dy);
将角速度分解为X、Y轴上的速度向量
vx = speed * Math.cos(angle);
vy = speed * Math.sin(angle);
将角加速度分解为X、Y轴上的加速度
ax = force * Math.cos(angle);
ay = force * Math.sin(angle);
将加速度加入速度向量
vx += ax;
vy += ay;
将速度向量加入位置坐标
object.x += vx;
object.y += vy;
移除越界物体
if (object.x - object.width / 2 > right ||
object.x + object.width / 2 < left ||
object.y - object.height / 2 > bottom ||
object.y + object.height / 2 < top) {
//code to remove object
}
重置越界物体
if (object.x - object.width / 2 > right ||
object.x + object.width / 2 < left ||
object.y - object.height / 2 > bottom ||
object.y + object.height / 2 < top) {
//reset object position and velocity
}
越界物体的屏幕环绕
if (object.x - object.width / 2 > right) {
object.x = left - object.width / 2;
}
else if (object.x + object.width / 2 < left) {
object.x = right + object.width / 2;
}
if (object.y - object.height / 2 > bottom) {
object.y = top - object.height / 2;
}
else if (object.y + object.height / 2 < top) {
object.y = bottom + object.height / 2;
}
应用摩擦力(精确)
speed = Math.sqrt(vx * vx + vy * vy);
angle = Math.atan2(vy, vx);
if (speed > friction) {
speed -= friction;
}
else {
speed = 0;
}
vx = Math.cos(angle) * speed;
vy = Math.sin(angle) * speed;
应用摩擦力(简便方法)
vx *= friction;
vy *= friction;