-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
107 lines (100 loc) · 3.59 KB
/
app.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
const ball = document.querySelector('.ball');
const ground = document.querySelector('.ground');
const stick = document.querySelector('.stick');
const t = 1.5;
const airForceCoefficient = 0.000002;
let xpos = 100;
let ypos = 300;
drawBall();
let a = 15.81;
let ballWidth = ball.getBoundingClientRect().width;
let vx = 100.0;
let vy = 0.0;
let vstick = 0.0;
const screenWidth = document.body.clientWidth;
let safeVerticalCollision = true;
let safeHorizontalBallCollision = true;
let safeHorizontalStickCollision = true;
let safeStickBallCollision = true;
dragElement();
function dragElement() {
ball.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e.preventDefault();
if(e.clientX < xpos + ballWidth / 2 && e.clientX > 0 &&
e.clientY > 0 && e.clientY < ground.getBoundingClientRect().top){
ball.style.top = e.clientY - ballWidth / 2+ "px";
ball.style.left = e.clientX - ballWidth / 2+ "px";
}
}
function closeDragElement(e) {
vx = (xpos - e.clientX) * 1.5;
vy = (ypos - e.clientY) * 1.5;
ypos = e.clientY - ballWidth / 2;
xpos = e.clientX - ballWidth / 2;
move();
document.onmouseup = null;
document.onmousemove = null;
}
}
airForceUpdate = (vx, vy, size, t) => {
vx -= size * t * vx * airForceCoefficient;
vy -= size * t * vy * airForceCoefficient;
return [parseFloat(vx), parseFloat(vy)];
};
function drawBall () {
ball.style.left = `${xpos}px`;
ball.style.top = `${ypos}px`;
}
function move () {
setInterval(() => {
console.log(vx, vy);
const stickCollision = collision();
[vx, vy] = airForceUpdate(vx, vy, ballWidth, t);
if ((ball.getBoundingClientRect().bottom >= ground.getBoundingClientRect().top ||
ball.getBoundingClientRect().top <= 0) && safeVerticalCollision) {
vy = parseFloat(-0.80 * vy);
safeVerticalCollision = !safeVerticalCollision;
} else {
vy = vy + parseFloat(a * t / 100.0);
safeVerticalCollision = true;
}
if (safeHorizontalBallCollision && (screenWidth <= ball.getBoundingClientRect().right ||
0 >= xpos || ( safeStickBallCollision && stickCollision))) {
if(stickCollision){
vstick = vx + vstick;
vx = vstick + vx;
safeStickBallCollision = false;
} else {
safeStickBallCollision = true;
}
vx = parseFloat(-0.80 * vx);
safeHorizontalBallCollision = false;
} else {
safeHorizontalBallCollision = true;
safeStickBallCollision = true;
}
if ((stick.getBoundingClientRect().left <= 0 || stick.getBoundingClientRect().right >= screenWidth) &&
safeHorizontalStickCollision) {
vstick = -0.8 * vstick;
safeHorizontalStickCollision = false;
} else {
safeHorizontalStickCollision = true;
}
ypos += vy * t / 50;
xpos += vx * t / 50;
stick.style.left = stick.getBoundingClientRect().left + vstick * t / 50 + 'px';
drawBall();
}, t);
}
collision = () => {
return ball.getBoundingClientRect().right <= stick.getBoundingClientRect().right &&
ball.getBoundingClientRect().right >= stick.getBoundingClientRect().left &&
ball.getBoundingClientRect().bottom >= stick.getBoundingClientRect().top
};