-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplePhysicsEngine.js
More file actions
79 lines (67 loc) · 1.81 KB
/
simplePhysicsEngine.js
File metadata and controls
79 lines (67 loc) · 1.81 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
let wind;
let gravity;
let coefOfFriction;
let normalForce;
let frictionMag;
let movers = [];
let liquid;
function setup() {
createCanvas(640, 480);
background(255);
wind = createVector(0.5, 0);
gravity = createVector(0, 0.01);
coefOfFriction = 0.01;
normalForce = 1;
frictionMag = coefOfFriction * normalForce;
for (let i = 0; i < 9; i++) {
let mass = random(0.1, 2);
let gravity = createVector(0,1);
movers[i] = new Mover(40 + i * 70, random(5, 30), mass, gravity);
}
liquid = new Liquid(0, height / 2, width, height / 2, 0.1);
}
function draw() {
//print(`gravity: ${gravity}`);
background(255);
liquid.show();
movers.forEach(mover => {
if (liquid.contains(mover)) {
let dragForce = liquid.calculateDrag(mover);
print(`${dragForce}`);
mover.applyForce(dragForce.limit(-2.0));
//print(`In contact!`);
}
mover.applyForce(mover.gravity);
mover.collision();
mover.bounceEdges();
mover.update();
mover.show();
})
}
class Liquid {
constructor(x, y, w, h, c) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.c = c; // coefficient of drag
}
show() {
noStroke();
fill(175);
rect(this.x, this.y, this.w, this.h);
}
contains(mover) {
let pos = mover.position;
return (pos.x > this.x && pos.x < this.x + this.w &&
pos.y > this.y && pos.y < this.y + this.h);
}
calculateDrag(mover) {
let speed = mover.velocity.mag();
let dragMagnitude = this.c * speed * speed;
let dragForce = mover.velocity.copy();
dragForce.mult(-0.5);
dragForce.setMag(dragMagnitude);
return dragForce;
}
}