-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
170 lines (140 loc) · 4.66 KB
/
main.js
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
162
163
164
165
166
167
168
169
170
let cnvMain = document.getElementById("cnvMain");
let ctxMain = cnvMain.getContext("2d");
let inpStatic = document.getElementById("inpStatic");
let inpDynamic = document.getElementById("inpDynamic");
let balls = [];
let prevFrameTime = Date.now();
let userTargetBall = null;
let userDragging = false;
let userDragPos = new Vec2(0, 0);
let ballFriction = 0.3;
cnvMain.addEventListener("mousedown", (e) => {
let mousePos = mouseToCanvasPos(e.x, e.y);
//find target ball (if any)
for (let ball of balls)
{
if (ball.getPosition().sub(mousePos).length() < ball.getRadius())
{
userTargetBall = ball;
userDragging = true;
userDragPos = mousePos;
break;
}
}
});
cnvMain.addEventListener("mouseup", (e) => {
if (userDragging)
{
userDragging = false;
let mousePos = mouseToCanvasPos(e.x, e.y);
let vel = userTargetBall.getPosition().sub(mousePos);
vel.x *= 5;
vel.y *= 5;
userTargetBall.setVelocity(userTargetBall.getVelocity().add(vel));
userTargetBall = null;
}
});
cnvMain.addEventListener("mousemove", (e) => {
if (userDragging) userDragPos = mouseToCanvasPos(e.x, e.y);
});
init();
function init()
{
let offset = new Vec2(800, 360);
balls.push(new Ball(400, offset.y, 10));
balls.push(new Ball(offset.x, offset.y, 10));
for (let column = 1; column <= 4; column++)
{
let start = g(column * 2, 30);
let end = g(column * 2, -30);
let divisions = column;
balls.push(new Ball(offset.x + start.x, offset.y + start.y, 10));
for (let d = 1; d < divisions; d++)
{
let p = start.mul(1 - d/divisions).add(end.mul(d/divisions));
balls.push(new Ball(offset.x + p.x, offset.y + p.y, 10));
}
balls.push(new Ball(offset.x + end.x, offset.y + end.y, 10));
}
update();
}
function g(r, t)
{
t = t / 180 * Math.PI;
return new Vec2(Math.cos(t) * r * 10, Math.sin(t) * r * 10);
}
function update()
{
requestAnimationFrame(update);
let dt = (Date.now() - prevFrameTime) / 1000;
prevFrameTime = Date.now();
//move balls by the velocities they were given last frame
for (let ball of balls)
{
let newPosition = ball.getPosition().add(ball.getVelocity().mul(dt));
let newVelocity = ball.getVelocity();
const radius = ball.getRadius();
//bounce off screen sides
if (newPosition.x - radius < 0)
{
newPosition.x = 2 * radius - newPosition.x;
newVelocity.x = Math.abs(newVelocity.x);
}
if (newPosition.x + radius > cnvMain.width)
{
newPosition.x = 2 * (cnvMain.width - radius) - newPosition.x;
newVelocity.x = -Math.abs(newVelocity.x);
}
if (newPosition.y - radius < 0)
{
newPosition.y = 2 * radius - newPosition.y;
newVelocity.y = Math.abs(newVelocity.y);
}
if (newPosition.y + radius > cnvMain.height)
{
newPosition.y = 2 * (cnvMain.height - radius) - newPosition.y;
newVelocity.y = -Math.abs(newVelocity.y);
}
ball.setPosition(newPosition);
//dampen velocity a bit
if (ball.getVelocity().length() < 0.1) newVelocity = new Vec2(0, 0);
else
{
let ballDeceleration = ball.getVelocity().mul(ballFriction);
newVelocity.x = newVelocity.x - ballDeceleration.x * dt;
newVelocity.y = newVelocity.y - ballDeceleration.y * dt;
}
ball.setVelocity(newVelocity);
}
//handle collisions
for (let i = 0; i < balls.length - 1; i++)
{
for (let j = i + 1; j < balls.length; j++)
{
handleCollision(balls[i], balls[j]);
}
}
//draw balls
ctxMain.clearRect(0, 0, cnvMain.width, cnvMain.height);
for (let ball of balls)
{
ctxMain.beginPath();
ctxMain.arc(ball.getPosition().x, ball.getPosition().y, ball.getRadius(), 0, 2 * Math.PI);
ctxMain.stroke();
}
//draw drag line
if (userDragging)
{
ctxMain.moveTo(userDragPos.x, userDragPos.y);
ctxMain.lineTo(userTargetBall.getPosition().x, userTargetBall.getPosition().y);
ctxMain.stroke();
}
}
function mouseToCanvasPos(mousex, mousey)
{
let canvasBounds = cnvMain.getBoundingClientRect();
let mouseRelativeToCanvas = new Vec2(mousex - canvasBounds.x, mousey - canvasBounds.y);
let canvasPixelsPerScreenPixel = cnvMain.width / canvasBounds.width;
let mouseInCanvasPixels = mouseRelativeToCanvas.mul(canvasPixelsPerScreenPixel);
return mouseInCanvasPixels;
}