Skip to content

Commit 1cb6358

Browse files
committed
added different world generators
1 parent 2c086d7 commit 1cb6358

3 files changed

Lines changed: 122 additions & 28 deletions

File tree

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<!-- Include the processing.js library -->
1414
<!-- See https://khanacademy.zendesk.com/hc/en-us/articles/202260404-What-parts-of-ProcessingJS-does-Khan-Academy-support- for differences -->
1515
<script src="https://cdn.jsdelivr.net/processing.js/1.4.8/processing.min.js"></script>
16+
<script src="particleGenerators.js"></script>
1617
<script src="script.js">
1718

1819
</script>

particleGenerators.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
class World{
2+
constructor(particleCount, particleGenerator, containX = true, containY = true) {
3+
this.particleCount = particleCount;
4+
this.particleGenerator = particleGenerator;
5+
this.containX = containX;
6+
this.containY = containY;
7+
}
8+
}
9+
10+
11+
12+
// Heat transmission wave
13+
function heatWaveGen(index) {
14+
let rnd = () => Math.random();
15+
let w = window.innerWidth;
16+
let h = window.innerHeight;
17+
let size = 50;
18+
let position = new Vector(rnd() * w, rnd() * h);
19+
let velocity;
20+
if (position.x > 500) {
21+
velocity = new Vector(rnd() * 10, rnd());
22+
}
23+
else {
24+
position = new Vector(position.x + 400, position.y);
25+
velocity = new Vector(rnd() * 10, rnd());
26+
}
27+
28+
return [size, position, velocity];
29+
}
30+
var HeatTransmissionWave = new World(600, heatWaveGen, true, true);
31+
32+
33+
// hot line between cold gases
34+
35+
function hotLineGenerator(index) {
36+
let rnd = () => Math.random();
37+
let w = window.innerWidth;
38+
let h = window.innerHeight;
39+
let size = 20;
40+
let position = new Vector(rnd() * w, rnd() * h);
41+
let velocity;
42+
if (position.y < 450) {
43+
velocity = new Vector(2, rnd());
44+
}
45+
else if (position.y > 450 && position.y < 550) {
46+
// position = new Vector(position.x, position.y + 200);
47+
velocity = new Vector(20, rnd());
48+
}
49+
else {
50+
velocity = new Vector(2, rnd());
51+
}
52+
return [size, position, velocity];
53+
}
54+
var EnergyLine = new World(500, hotLineGenerator, false, true);
55+
56+
// high speed line above cold gas
57+
58+
function highSpeedLineGen(index) {
59+
let rnd = () => Math.random();
60+
let w = window.innerWidth;
61+
let h = window.innerHeight;
62+
let size = 20;
63+
let position = new Vector(rnd() * w, rnd() * h);
64+
let velocity;
65+
if (position.y < 300) {
66+
velocity = new Vector(20, rnd());
67+
}
68+
else if (position.y > 300 && position.y < 400) {
69+
position = new Vector(position.x, position.y + 200);
70+
velocity = new Vector(2, rnd());
71+
}
72+
else {
73+
velocity = new Vector(2, rnd());
74+
}
75+
return [size, position, velocity];
76+
}
77+
var blowOver = new World(500, highSpeedLineGen, false, true);
78+
79+
80+
// fast fluid crashing into slow fluid

script.js

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,7 @@ function triangulate(colliders) {
1818
}
1919
}
2020

21-
function particleGenerator(index){
22-
let rnd = () => Math.random();
23-
let w = window.innerWidth;
24-
let h = window.innerHeight;
25-
let size = 10;
26-
let position = new Vector(rnd() * w, rnd() * h);
27-
let velocity;
28-
if(position.y < 450) {
29-
velocity = new Vector(2,rnd());
30-
}
31-
else if (position.y > 450 && position.y < 500) {
32-
// position = new Vector(position.x, position.y + 200);
33-
velocity = new Vector(20, rnd());
34-
}
35-
else {
36-
velocity = new Vector(2, rnd());
37-
}
38-
return [size, position, velocity];
39-
}
21+
thisWorld = HeatTransmissionWave; // change to EnergyLine for the hot line between cold gases
4022
class Vector {
4123
constructor(x, y) {
4224
this.x = x
@@ -50,17 +32,22 @@ class Vector {
5032
this.x -= v.x
5133
this.y -= v.y
5234
}
35+
scale(s) {
36+
this.x *= s
37+
this.y *= s
38+
}
5339
}
5440
var programCode = function (processingInstance) {
5541
with (processingInstance) {
5642
size(window.innerWidth, window.innerHeight)
5743
frameRate(30)
5844

5945
class Particle {
60-
constructor(size, position, velocity) {
46+
constructor(size, position, velocity, density = 0.01) {
6147
this.size = size
6248
this.position = position
6349
this.velocity = velocity
50+
this.mass = size * density
6451
}
6552
display() {
6653
noStroke();
@@ -75,16 +62,42 @@ var programCode = function (processingInstance) {
7562
this.size,
7663
this.size
7764
);
65+
7866
}
7967
move() {
80-
if (this.position.x > width || this.position.x < 0) {
81-
this.position.x = 0;
82-
// this.velocity.x = -this.velocity.x;
68+
if (this.position.x > width) {
69+
if (thisWorld.containX) {
70+
this.velocity.x = -this.velocity.x;
71+
}
72+
else {
73+
this.position.x = 0;
74+
}
75+
}
76+
else if (this.position.x < 0) {
77+
if (thisWorld.containX) {
78+
this.velocity.x = -this.velocity.x;
79+
} else {
80+
this.position.x = width;
81+
}
8382
}
84-
if (this.position.y > height || this.position.y < 0) {
85-
// this.position.y = 0;
86-
this.velocity.y = -this.velocity.y;
83+
84+
if (this.position.y > height) {
85+
if (thisWorld.containY) {
86+
this.velocity.y = -this.velocity.y;
87+
}
88+
else {
89+
this.position.y = 0;
90+
}
8791
}
92+
else if (this.position.y < 0) {
93+
if (thisWorld.containY) {
94+
this.velocity.y = -this.velocity.y;
95+
}
96+
else {
97+
this.position.y = height;
98+
}
99+
}
100+
88101
this.position.add(this.velocity);
89102
}
90103
collide(other) {
@@ -120,8 +133,8 @@ var programCode = function (processingInstance) {
120133

121134
let particles = [];
122135

123-
for (let k = 0; k < 500; k++) {
124-
let [size, position, velocity] = particleGenerator(k);
136+
for (let k = 0; k < thisWorld.particleCount; k++) {
137+
let [size, position, velocity] = thisWorld.particleGenerator(k);
125138
particles.push(new Particle(size, position, velocity));
126139
}
127140

0 commit comments

Comments
 (0)