Permalink
Browse files

minor fixes

  • Loading branch information...
1 parent 914047a commit 35a0cc61cf42f8914119554f4c69840db6ac2071 @shiffman committed Jul 1, 2016
@@ -1,13 +1,13 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
-
- // Liquid class
- class Liquid {
-
+// Liquid class
+class Liquid {
+
+
// Liquid is a rectangle
- float x,y,w,h;
+ float x, y, w, h;
// Coefficient of drag
float c;
@@ -18,13 +18,13 @@
h = h_;
c = c_;
}
-
+
// Is the Mover in the Liquid?
boolean contains(Mover m) {
PVector l = m.position;
return l.x > x && l.x < x + w && l.y > y && l.y < y + h;
}
-
+
// Calculate drag force
PVector drag(Mover m) {
// Magnitude is coefficient * speed squared
@@ -34,19 +34,17 @@
// Direction is inverse of velocity
PVector dragForce = m.velocity.get();
dragForce.mult(-1);
-
+
// Scale according to magnitude
// dragForce.setMag(dragMagnitude);
dragForce.normalize();
dragForce.mult(dragMagnitude);
return dragForce;
}
-
+
void display() {
noStroke();
fill(50);
- rect(x,y,w,h);
+ rect(x, y, w, h);
}
-
-}
-
+}
@@ -8,7 +8,7 @@ class Mover {
PVector position;
PVector velocity;
PVector acceleration;
-
+
// Mass is tied to size
float mass;
@@ -29,30 +29,28 @@ class Mover {
}
void update() {
-
+
// Velocity changes according to acceleration
velocity.add(acceleration);
// position changes by velocity
position.add(velocity);
// We must clear acceleration each frame
acceleration.mult(0);
}
-
+
// Draw Mover
void display() {
stroke(0);
strokeWeight(2);
fill(127, 200);
ellipse(position.x, position.y, mass*16, mass*16);
}
-
+
// Bounce off bottom of window
void checkEdges() {
if (position.y > height) {
velocity.y *= -0.9; // A little dampening when hitting the bottom
position.y = height;
}
}
-}
-
-
+}
@@ -3,7 +3,7 @@
// http://natureofcode.com
// Forces (Gravity and Fluid Resistence) with Vectors
-
+
// Demonstration of multiple force acting on bodies (Mover class)
// Bodies experience gravity continuously
// Bodies experience fluid resistance when in "water"
@@ -23,12 +23,12 @@ void setup() {
void draw() {
background(255);
-
+
// Draw water
liquid.display();
for (int i = 0; i < movers.length; i++) {
-
+
// Is the Mover in the liquid?
if (liquid.contains(movers[i])) {
// Calculate drag force
@@ -41,16 +41,15 @@ void draw() {
PVector gravity = new PVector(0, 0.1*movers[i].mass);
// Apply gravity
movers[i].applyForce(gravity);
-
+
// Update and display
movers[i].update();
movers[i].display();
movers[i].checkEdges();
}
-
+
fill(0);
- text("click mouse to reset",10,30);
-
+ text("click mouse to reset", 10, 30);
}
void mousePressed() {
@@ -62,11 +61,4 @@ void reset() {
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(0.5, 3), 40+i*70, 0);
}
-}
-
-
-
-
-
-
-
+}
@@ -5,15 +5,15 @@
ArrayList<Particle> particles;
void setup() {
- size(640,360);
+ size(640, 360);
particles = new ArrayList<Particle>();
}
void draw() {
background(255);
- particles.add(new Particle(new PVector(width/2,50)));
-
+ particles.add(new Particle(new PVector(width/2, 50)));
+
// Looping through backwards to delete
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
@@ -22,8 +22,4 @@ void draw() {
particles.remove(i);
}
}
-}
-
-
-
-
+}
@@ -13,7 +13,7 @@ class Particle {
Particle(PVector l) {
acceleration = new PVector(0, 0.05);
velocity = new PVector(random(-1, 1), random(-2, 0));
- position = l.get();
+ position = l.copy();
lifespan = 255.0;
}
@@ -46,5 +46,4 @@ class Particle {
return false;
}
}
-}
-
+}

0 comments on commit 35a0cc6

Please sign in to comment.