Repository navigation
Expand file tree
/
Copy pathparticle.js
More file actions
132 lines (114 loc) · 3.84 KB
/
Copy pathparticle.js
File metadata and controls
132 lines (114 loc) · 3.84 KB
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
import { GRAVITY, INTERVAL } from "./constants.js";
export const makeParticle = (
canvasManager,
{
radius,
startPosition,
startVelocity,
gravity = GRAVITY,
terminalVelocity = 12,
onRightTouch = () => {},
onBottomTouch = () => {},
onLeftTouch = () => {},
onTopTouch = () => {},
onRightPassed = () => {},
onBottomPassed = () => {},
onLeftPassed = () => {},
onTopPassed = () => {},
}
) => {
const particleStart = Date.now();
let position = { ...startPosition };
let velocity = { ...startVelocity };
const update = (deltaTime) => {
const deltaTimeMultiplier = deltaTime / INTERVAL;
position.x += deltaTimeMultiplier * velocity.x;
position.y += Math.min(deltaTimeMultiplier * velocity.y, terminalVelocity);
velocity.y += deltaTimeMultiplier * gravity;
if (position.x > canvasManager.getWidth() + radius) {
onRightPassed(position, velocity);
} else if (position.x > canvasManager.getWidth() - radius) {
onRightTouch(position, velocity);
} else if (position.x < -radius) {
onLeftPassed(position, velocity);
} else if (position.x < radius) {
onLeftTouch(position, velocity);
}
if (position.y > canvasManager.getHeight() + radius) {
onBottomPassed(position, velocity);
} else if (position.y > canvasManager.getHeight() - radius) {
onBottomTouch(position, velocity);
} else if (position.y < -radius) {
onTopPassed(position, velocity);
} else if (position.y < radius) {
onTopTouch(position, velocity);
}
};
return {
update,
getStart: () => particleStart,
getDuration: () => Date.now() - particleStart,
getPosition: () => position,
getRadius: () => radius,
getVelocity: () => velocity,
setPosition: (p) => (position = p),
setVelocity: (c) => (velocity = c),
};
};
export const checkParticleCollision = (A, B) => {
const rSum = A.getRadius() + B.getRadius();
const dx = B.getPosition().x - A.getPosition().x;
const dy = B.getPosition().y - A.getPosition().y;
return [rSum * rSum > dx * dx + dy * dy, rSum - Math.sqrt(dx * dx + dy * dy)];
};
export const resolveParticleCollision = (A, B) => {
const relativeVelocity = {
x: B.getVelocity().x - A.getVelocity().x,
y: B.getVelocity().y - A.getVelocity().y,
};
const norm = {
x: B.getPosition().x - A.getPosition().x,
y: B.getPosition().y - A.getPosition().y,
};
const mag = Math.sqrt(norm.x * norm.x + norm.y * norm.y);
norm.x /= mag;
norm.y /= mag;
const velocityAlongNorm =
relativeVelocity.x * norm.x + relativeVelocity.y * norm.y;
if (velocityAlongNorm > 0) return;
const bounce = 0.7;
let j = -(1 + bounce) * velocityAlongNorm;
j /= 1 / A.getRadius() + 1 / B.getRadius();
const impulse = { x: j * norm.x, y: j * norm.y };
A.setVelocity({
x: A.getVelocity().x - (1 / A.getRadius()) * impulse.x,
y: A.getVelocity().y - (1 / A.getRadius()) * impulse.y,
});
B.setVelocity({
x: B.getVelocity().x + (1 / B.getRadius()) * impulse.x,
y: B.getVelocity().y + (1 / B.getRadius()) * impulse.y,
});
};
export const adjustParticlePositions = (A, B, depth) => {
const percent = 0.2;
const slop = 0.01;
let correctionNum =
(Math.max(depth - slop, 0) / (1 / A.getRadius() + 1 / B.getRadius())) *
percent;
const norm = {
x: B.getPosition().x - A.getPosition().x,
y: B.getPosition().y - A.getPosition().y,
};
const mag = Math.sqrt(norm.x * norm.x + norm.y * norm.y);
norm.x /= mag;
norm.y /= mag;
const correction = { x: correctionNum * norm.x, y: correctionNum * norm.y };
A.setPosition({
x: A.getPosition().x - (1 / A.getRadius()) * correction.x,
y: A.getPosition().y - (1 / A.getRadius()) * correction.y,
});
B.setPosition({
x: B.getPosition().x + (1 / B.getRadius()) * correction.x,
y: B.getPosition().y + (1 / B.getRadius()) * correction.y,
});
};