-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
85 lines (64 loc) · 1.78 KB
/
sketch.js
File metadata and controls
85 lines (64 loc) · 1.78 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
let system
let frame_text
let reset_button
let tick_button
let autotick
let timestep
let timestep_text
let substep
let substep_text
function update() {
const v_substep = Math.floor(substep.value())
const v_timestep = timestep.value()
const real_timestep = v_timestep / v_substep
for (let index = 0; index < v_substep; index++) {
system.update(real_timestep)
}
system.increment_frame()
frame_text.html(`Frame #${system.frame.toFixed(0)}<br>Update #${system.updates.toFixed()}<br>T ${system.time.toFixed(2)}`)
}
function setup() {
const a = new Vector2(1, -1)
const b = new Vector2(0, 1)
const c = a.reflect(b)
console.log(a, b, c)
createCanvas(400, 400);
system = new System({ w: width, h: height, padding: 10 })
frame_text = createP('Frame #0')
reset_button = createButton('Reset')
reset_button.mouseClicked(() => {
system.reset()
})
tick_button = createButton("Simulate 1 tick")
tick_button.mouseClicked(() => {
if (!autotick.checked()) {
update()
}
})
autotick = createCheckbox('Update automatically', false)
createP('Timestep')
timestep_text = createP('Timestep')
timestep = createSlider(0.0001, 2, 0.6, 0.0001)
createP('Substeps')
substep_text = createP('Substeps')
substep = createSlider(1, 16, 1, 1)
}
function draw() {
timestep_text.html(timestep.value().toFixed(4))
substep_text.html(substep.value().toFixed(0))
background(220);
if (autotick.checked()) {
update()
}
const state = system.get_state()
if (state.circles) {
strokeWeight(1)
for (const s_circle of state.circles) {
circle(s_circle.pos.x, s_circle.pos.y, s_circle.diameter)
}
strokeWeight(1)
for (const s_edge of state.edges) {
line(s_edge.a.x, s_edge.a.y, s_edge.b.x, s_edge.b.y)
}
}
}