Skip to content

Commit a8d6d3f

Browse files
authored
Merge pull request #389 from 17bcs029/simulate
Updating Simulate example to ES6 Standards
2 parents 08552bd + 1e8eff8 commit a8d6d3f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1268
-1268
lines changed

src/data/examples/en/09_Simulate/00_Forces.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,51 @@
33
* @description Demonstration of multiple force acting on bodies
44
* (<a href="http://natureofcode.com">natureofcode.com</a>)
55
*/
6-
// Demonstration of multiple force acting on
6+
// Demonstration of multiple force acting on
77
// bodies (Mover class)
88
// Bodies experience gravity continuously
99
// Bodies experience fluid resistance when in "water"
1010

1111
// Five moving bodies
12-
var movers = [];
12+
let movers = [];
1313

1414
// Liquid
15-
var liquid;
15+
let liquid;
1616

1717
function setup() {
1818
createCanvas(640, 360);
1919
reset();
2020
// Create liquid object
21-
liquid = new Liquid(0, height/2, width, height/2, 0.1);
21+
liquid = new Liquid(0, height / 2, width, height / 2, 0.1);
2222
}
2323

2424
function draw() {
2525
background(127);
26-
26+
2727
// Draw water
2828
liquid.display();
2929

30-
for (var i = 0; i < movers.length; i++) {
31-
30+
for (let i = 0; i < movers.length; i++) {
31+
3232
// Is the Mover in the liquid?
3333
if (liquid.contains(movers[i])) {
3434
// Calculate drag force
35-
var dragForce = liquid.calculateDrag(movers[i]);
35+
let dragForce = liquid.calculateDrag(movers[i]);
3636
// Apply drag force to Mover
3737
movers[i].applyForce(dragForce);
3838
}
3939

4040
// Gravity is scaled by mass here!
41-
var gravity = createVector(0, 0.1*movers[i].mass);
41+
let gravity = createVector(0, 0.1 * movers[i].mass);
4242
// Apply gravity
4343
movers[i].applyForce(gravity);
44-
44+
4545
// Update and display
4646
movers[i].update();
4747
movers[i].display();
4848
movers[i].checkEdges();
4949
}
50-
50+
5151
}
5252

5353

@@ -57,63 +57,63 @@ function mousePressed() {
5757

5858
// Restart all the Mover objects randomly
5959
function reset() {
60-
for (var i = 0; i < 9; i++) {
61-
movers[i] = new Mover(random(0.5, 3), 40+i*70, 0);
60+
for (let i = 0; i < 9; i++) {
61+
movers[i] = new Mover(random(0.5, 3), 40 + i * 70, 0);
6262
}
6363
}
6464

65-
var Liquid = function(x, y, w, h, c) {
65+
let Liquid = function(x, y, w, h, c) {
6666
this.x = x;
6767
this.y = y;
6868
this.w = w;
6969
this.h = h;
7070
this.c = c;
7171
};
72-
72+
7373
// Is the Mover in the Liquid?
7474
Liquid.prototype.contains = function(m) {
75-
var l = m.position;
75+
let l = m.position;
7676
return l.x > this.x && l.x < this.x + this.w &&
7777
l.y > this.y && l.y < this.y + this.h;
7878
};
79-
79+
8080
// Calculate drag force
8181
Liquid.prototype.calculateDrag = function(m) {
8282
// Magnitude is coefficient * speed squared
83-
var speed = m.velocity.mag();
84-
var dragMagnitude = this.c * speed * speed;
83+
let speed = m.velocity.mag();
84+
let dragMagnitude = this.c * speed * speed;
8585

8686
// Direction is inverse of velocity
87-
var dragForce = m.velocity.copy();
87+
let dragForce = m.velocity.copy();
8888
dragForce.mult(-1);
89-
89+
9090
// Scale according to magnitude
9191
// dragForce.setMag(dragMagnitude);
9292
dragForce.normalize();
9393
dragForce.mult(dragMagnitude);
9494
return dragForce;
9595
};
96-
96+
9797
Liquid.prototype.display = function() {
9898
noStroke();
9999
fill(50);
100100
rect(this.x, this.y, this.w, this.h);
101101
};
102102

103-
function Mover(m,x,y) {
103+
function Mover(m, x, y) {
104104
this.mass = m;
105-
this.position = createVector(x,y);
106-
this.velocity = createVector(0,0);
107-
this.acceleration = createVector(0,0);
105+
this.position = createVector(x, y);
106+
this.velocity = createVector(0, 0);
107+
this.acceleration = createVector(0, 0);
108108
}
109109

110110
// Newton's 2nd law: F = M * A
111111
// or A = F / M
112112
Mover.prototype.applyForce = function(force) {
113-
var f = p5.Vector.div(force,this.mass);
113+
let f = p5.Vector.div(force, this.mass);
114114
this.acceleration.add(f);
115115
};
116-
116+
117117
Mover.prototype.update = function() {
118118
// Velocity changes according to acceleration
119119
this.velocity.add(this.acceleration);
@@ -127,15 +127,15 @@ Mover.prototype.display = function() {
127127
stroke(0);
128128
strokeWeight(2);
129129
fill(255,127);
130-
ellipse(this.position.x,this.position.y,this.mass*16,this.mass*16);
130+
ellipse(this.position.x, this.position.y, this.mass * 16, this.mass * 16);
131131
};
132132

133133
// Bounce off bottom of window
134134
Mover.prototype.checkEdges = function() {
135-
if (this.position.y > (height - this.mass*8)) {
135+
if (this.position.y > (height - this.mass * 8)) {
136136
// A little dampening when hitting the bottom
137137
this.velocity.y *= -0.9;
138-
this.position.y = (height - this.mass*8);
138+
this.position.y = (height - this.mass * 8);
139139
}
140140
};
141141

src/data/examples/en/09_Simulate/01_ParticleSystem.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
* @description This is a basic Particle System
44
* (<a href="http://natureofcode.com">natureofcode.com</a>)
55
*/
6-
var system;
6+
let system;
77

88
function setup() {
99
createCanvas(720, 400);
10-
system = new ParticleSystem(createVector(width/2, 50));
10+
system = new ParticleSystem(createVector(width / 2, 50));
1111
}
1212

1313
function draw() {
@@ -17,7 +17,7 @@ function draw() {
1717
}
1818

1919
// A simple Particle class
20-
var Particle = function(position) {
20+
let Particle = function(position) {
2121
this.acceleration = createVector(0, 0.05);
2222
this.velocity = createVector(random(-1, 1), random(-1, 0));
2323
this.position = position.copy();
@@ -49,7 +49,7 @@ Particle.prototype.isDead = function(){
4949
return this.lifespan < 0;
5050
};
5151

52-
var ParticleSystem = function(position) {
52+
let ParticleSystem = function(position) {
5353
this.origin = position.copy();
5454
this.particles = [];
5555
};
@@ -59,8 +59,8 @@ ParticleSystem.prototype.addParticle = function() {
5959
};
6060

6161
ParticleSystem.prototype.run = function() {
62-
for (var i = this.particles.length-1; i >= 0; i--) {
63-
var p = this.particles[i];
62+
for (let i = this.particles.length-1; i >= 0; i--) {
63+
let p = this.particles[i];
6464
p.run();
6565
if (p.isDead()) {
6666
this.particles.splice(i, 1);

0 commit comments

Comments
 (0)