removing old nature of code examples before adding new ones

This commit is contained in:
shiffman
2012-12-11 20:01:56 +00:00
parent e3aff605a8
commit 9b4891e419
541 changed files with 0 additions and 35009 deletions
@@ -1,74 +0,0 @@
// Attraction
// Daniel Shiffman <http://www.shiffman.net>
// A class for a draggable attractive body in our world
class Attractor {
float mass; // Mass, tied to size
PVector location; // Location
boolean dragging = false; // Is the object being dragged?
boolean rollover = false; // Is the mouse over the ellipse?
PVector drag; // holds the offset for when object is clicked on
Attractor() {
location = new PVector(width/2,height/2);
mass = 10;
drag = new PVector(0.0,0.0);
}
PVector attract(Mover m) {
PVector force = PVector.sub(location,m.location); // Calculate direction of force
float d = force.mag(); // Distance between objects
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction)
float strength = (g * mass * m.mass) / (d * d); // Calculate gravitional force magnitude
force.mult(strength); // Get force vector --> magnitude * direction
return force;
}
// Method to display
void display() {
ellipseMode(CENTER);
stroke(0);
if (dragging) fill (50);
else if (rollover) fill(100);
else fill(0);
ellipse(location.x,location.y,mass*6,mass*6);
}
// The methods below are for mouse interaction
void clicked(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
dragging = true;
drag.x = location.x-mx;
drag.y = location.y-my;
}
}
void rollover(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
rollover = true;
}
else {
rollover = false;
}
}
void stopDragging() {
dragging = false;
}
void drag() {
if (dragging) {
location.x = mouseX + drag.x;
location.y = mouseY + drag.y;
}
}
}
@@ -1,69 +0,0 @@
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover(float m, float x , float y) {
mass = m;
location = new PVector(x,y);
velocity = new PVector(0,0);
acceleration = new PVector(0,0);
}
void applyForce(PVector force) {
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
stroke(0);
fill(175,200);
ellipse(location.x,location.y,mass*2,mass*2);
}
PVector repel(Mover m) {
PVector force = PVector.sub(location,m.location); // Calculate direction of force
float distance = force.mag(); // Distance between objects
distance = constrain(distance,1.0,10000.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction
float strength = (g * mass * m.mass) / (distance * distance); // Calculate gravitional force magnitude
force.mult(-1*strength); // Get force vector --> magnitude * direction
return force;
}
void checkEdges() {
if (location.x > width) {
location.x = width;
velocity.x *= -1;
}
else if (location.x < 0) {
location.x = 0;
velocity.x *= -1;
}
if (location.y > height) {
location.y = height;
velocity.y *= -1;
}
else if (location.y < 0) {
location.y = 0;
velocity.y *= -1;
}
}
}
@@ -1,52 +0,0 @@
Mover[] movers = new Mover[20];
Attractor a;
float g = 1;
void setup() {
size(800,200);
smooth();
a = new Attractor();
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(4,12),random(width),random(height));
}
}
void draw() {
background(255);
a.display();
for (int i = 0; i < movers.length; i++) {
for (int j = 0; j < movers.length; j++) {
if (i != j) {
PVector force = movers[j].repel(movers[i]);
movers[i].applyForce(force);
}
}
PVector force = a.attract(movers[i]);
movers[i].applyForce(force);
movers[i].update();
movers[i].display();
}
}
@@ -1,39 +0,0 @@
// Attraction
// Daniel Shiffman <http://www.shiffman.net>
// A class for a draggable attractive body in our world
class Attractor {
float mass; // Mass, tied to size
PVector location; // Location
float g;
Attractor() {
location = new PVector(0,0);
mass = 20;
g = 0.4;
}
PVector attract(Mover m) {
PVector force = PVector.sub(location,m.location); // Calculate direction of force
float distance = force.mag(); // Distance between objects
distance = constrain(distance,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction)
float strength = (g * mass * m.mass) / (distance * distance); // Calculate gravitional force magnitude
force.mult(strength); // Get force vector --> magnitude * direction
return force;
}
// Method to display
void display() {
stroke(255);
noFill();
pushMatrix();
translate(location.x,location.y,location.z);
sphere(mass*2);
popMatrix();
}
}
@@ -1,51 +0,0 @@
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover(float m, float x, float y, float z) {
mass = m;
location = new PVector(x,y,z);
velocity = new PVector(1,0);
acceleration = new PVector(0,0);
}
void applyForce(PVector force) {
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
noStroke();
fill(255);
pushMatrix();
translate(location.x,location.y,location.z);
sphere(mass*8);
popMatrix();
}
void checkEdges() {
if (location.x > width) {
location.x = 0;
}
else if (location.x < 0) {
location.x = width;
}
if (location.y > height) {
velocity.y *= -1;
location.y = height;
}
}
}
@@ -1,50 +0,0 @@
import processing.opengl.*;
Mover[] movers = new Mover[10];
Attractor a;
float angle = 0;
void setup() {
size(800,200,OPENGL);
smooth();
background(255);
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(0.1,2),random(-width/2,width/2),random(-height/2,height/2),random(-100,100));
}
a = new Attractor();
}
void draw() {
background(0);
sphereDetail(8);
lights();
translate(width/2,height/2);
rotateY(angle);
a.display();
for (int i = 0; i < movers.length; i++) {
PVector force = a.attract(movers[i]);
movers[i].applyForce(force);
movers[i].update();
movers[i].display();
}
angle += 0.003;
}
@@ -1,70 +0,0 @@
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover(float m, float x, float y) {
mass = m;
location = new PVector(x, y);
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
}
void applyForce(PVector force) {
PVector f = PVector.div(force, mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
stroke(0);
fill(175, 200);
ellipse(location.x, location.y, mass*16, mass*16);
}
PVector attract(Mover m) {
PVector force = PVector.sub(location, m.location); // Calculate direction of force
float distance = force.mag(); // Distance between objects
distance = constrain(distance, 5.0, 25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction
float strength = (g * mass * m.mass) / (distance * distance); // Calculate gravitional force magnitude
force.mult(strength); // Get force vector --> magnitude * direction
return force;
}
void boundaries() {
float d = 50;
PVector force = new PVector(0, 0);
if (location.x < d) {
force.x = 1;
}
else if (location.x > width -d) {
force.x = -1;
}
if (location.y < d) {
force.y = 1;
}
else if (location.y > height-d) {
force.y = -1;
}
force.normalize();
force.mult(0.1);
applyForce(force);
}
}
@@ -1,44 +0,0 @@
Mover[] movers = new Mover[20];
float g = 0.4;
void setup() {
size(800,200);
smooth();
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(1,2),random(width),random(height));
}
}
void draw() {
background(255);
for (int i = 0; i < movers.length; i++) {
for (int j = 0; j < movers.length; j++) {
if (i != j) {
PVector force = movers[j].attract(movers[i]);
movers[i].applyForce(force);
}
}
movers[i].boundaries();
movers[i].update();
movers[i].display();
}
}
@@ -1,53 +0,0 @@
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover() {
location = new PVector(30,30);
velocity = new PVector(0,0);
acceleration = new PVector(0,0);
mass = 1;
}
void applyForce(PVector force) {
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
stroke(0);
strokeWeight(2);
fill(127);
ellipse(location.x,location.y,48,48);
}
void checkEdges() {
if (location.x > width) {
location.x = width;
velocity.x *= -1;
} else if (location.x < 0) {
velocity.x *= -1;
location.x = 0;
}
if (location.y > height) {
velocity.y *= -1;
location.y = height;
}
}
}
@@ -1,27 +0,0 @@
Mover m;
void setup() {
size(800,200);
smooth();
m = new Mover();
}
void draw() {
background(255);
PVector wind = new PVector(0.01,0);
PVector gravity = new PVector(0,0.1);
m.applyForce(wind);
m.applyForce(gravity);
m.update();
m.display();
m.checkEdges();
}
@@ -1,53 +0,0 @@
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover(float m, float x , float y) {
mass = m;
location = new PVector(x,y);
velocity = new PVector(0,0);
acceleration = new PVector(0,0);
}
void applyForce(PVector force) {
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
stroke(0);
strokeWeight(2);
fill(0,127);
ellipse(location.x,location.y,mass*16,mass*16);
}
void checkEdges() {
if (location.x > width) {
location.x = width;
velocity.x *= -1;
} else if (location.x < 0) {
velocity.x *= -1;
location.x = 0;
}
if (location.y > height) {
velocity.y *= -1;
location.y = height;
}
}
}
@@ -1,36 +0,0 @@
Mover[] movers = new Mover[20];
void setup() {
size(800,200);
smooth();
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(0.1,4),0,0);
}
}
void draw() {
background(255);
for (int i = 0; i < movers.length; i++) {
PVector wind = new PVector(0.01,0);
PVector gravity = new PVector(0,0.1);
movers[i].applyForce(wind);
movers[i].applyForce(gravity);
movers[i].update();
movers[i].display();
movers[i].checkEdges();
}
}
@@ -1,53 +0,0 @@
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover(float m, float x , float y) {
mass = m;
location = new PVector(x,y);
velocity = new PVector(0,0);
acceleration = new PVector(0,0);
}
void applyForce(PVector force) {
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
stroke(0);
strokeWeight(2);
fill(0,127);
ellipse(location.x,location.y,mass*16,mass*16);
}
void checkEdges() {
if (location.x > width) {
location.x = width;
velocity.x *= -1;
} else if (location.x < 0) {
velocity.x *= -1;
location.x = 0;
}
if (location.y > height) {
velocity.y *= -1;
location.y = height;
}
}
}
@@ -1,34 +0,0 @@
Mover[] movers = new Mover[20];
void setup() {
size(800, 200);
smooth();
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(1, 4), 0, 0);
}
}
void draw() {
background(255);
for (int i = 0; i < movers.length; i++) {
PVector wind = new PVector(0.01, 0);
PVector gravity = new PVector(0, 0.1*movers[i].mass);
movers[i].applyForce(wind);
movers[i].applyForce(gravity);
movers[i].update();
movers[i].display();
movers[i].checkEdges();
}
}
@@ -1,53 +0,0 @@
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover(float m, float x , float y) {
mass = m;
location = new PVector(x,y);
velocity = new PVector(0,0);
acceleration = new PVector(0,0);
}
void applyForce(PVector force) {
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
stroke(0);
strokeWeight(2);
fill(0,127);
ellipse(location.x,location.y,mass*16,mass*16);
}
void checkEdges() {
if (location.x > width) {
location.x = width;
velocity.x *= -1;
} else if (location.x < 0) {
location.x = 0;
velocity.x *= -1;
}
if (location.y > height) {
velocity.y *= -1;
location.y = height;
}
}
}
@@ -1,42 +0,0 @@
Mover[] movers = new Mover[5];
void setup() {
size(383, 200);
randomSeed(1);
smooth();
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(1, 4), random(width), 0);
}
}
void draw() {
background(255);
for (int i = 0; i < movers.length; i++) {
PVector wind = new PVector(0.01, 0);
PVector gravity = new PVector(0, 0.1*movers[i].mass);
float c = 0.05;
PVector friction = movers[i].velocity.get();
friction.mult(-1);
friction.normalize();
friction.mult(c);
movers[i].applyForce(friction);
movers[i].applyForce(wind);
movers[i].applyForce(gravity);
movers[i].update();
movers[i].display();
movers[i].checkEdges();
}
}
@@ -1,53 +0,0 @@
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover(float m, float x , float y) {
mass = m;
location = new PVector(x,y);
velocity = new PVector(0,0);
acceleration = new PVector(0,0);
}
void applyForce(PVector force) {
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
stroke(0);
strokeWeight(2);
fill(0,127);
ellipse(location.x,location.y,mass*16,mass*16);
}
void checkEdges() {
if (location.x > width) {
location.x = width;
velocity.x *= -1;
} else if (location.x < 0) {
location.x = 0;
velocity.x *= -1;
}
if (location.y > height) {
velocity.y *= -1;
location.y = height;
}
}
}
@@ -1,42 +0,0 @@
Mover[] movers = new Mover[5];
void setup() {
size(383, 200);
randomSeed(1);
smooth();
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(1, 4), random(width), 0);
}
}
void draw() {
background(255);
for (int i = 0; i < movers.length; i++) {
PVector wind = new PVector(0.01, 0);
PVector gravity = new PVector(0, 0.1*movers[i].mass);
float c = 0.05;
PVector friction = movers[i].velocity.get();
friction.mult(-1);
friction.normalize();
friction.mult(c);
//movers[i].applyForce(friction);
movers[i].applyForce(wind);
movers[i].applyForce(gravity);
movers[i].update();
movers[i].display();
movers[i].checkEdges();
}
}
@@ -1,62 +0,0 @@
/**
* Forces (Gravity and Fluid Resistence) with Vectors
* by Daniel Shiffman.
*
* Demonstration of multiple force acting on bodies (Mover class)
* Bodies experience gravity continuously
* Bodies experience fluid resistance when in "water"
*/
// Liquid class
class Liquid {
// Liquid is a rectangle
float x,y,w,h;
// Coefficient of drag
float c;
Liquid(float x_, float y_, float w_, float h_, float c_) {
x = x_;
y = y_;
w = w_;
h = h_;
c = c_;
}
// Is the Mover in the Liquid?
boolean contains(Mover m) {
PVector l = m.location;
if (l.x > x && l.x < x + w && l.y > y && l.y < y + h) {
return true;
}
else {
return false;
}
}
// Calculate drag force
PVector drag(Mover m) {
// Magnitude is coefficient * speed squared
float speed = m.velocity.mag();
float dragMagnitude = c * speed * speed;
// 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);
}
}
@@ -1,64 +0,0 @@
/**
* Forces (Gravity and Fluid Resistence) with Vectors
* by Daniel Shiffman.
*
* Demonstration of multiple force acting on bodies (Mover class)
* Bodies experience gravity continuously
* Bodies experience fluid resistance when in "water"
*/
class Mover {
// location, velocity, and acceleration
PVector location;
PVector velocity;
PVector acceleration;
// Mass is tied to size
float mass;
Mover(float m, float x, float y) {
mass = m;
location = new PVector(x, y);
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
}
// Newton's 2nd law: F = M * A
// or A = F / M
void applyForce(PVector force) {
// Divide by mass
PVector f = PVector.div(force, mass);
// Accumulate all forces in acceleration
acceleration.add(f);
}
void update() {
// Velocity changes according to acceleration
velocity.add(acceleration);
// Location changes by velocity
location.add(velocity);
// We must clear acceleration each frame
acceleration.mult(0);
}
// Draw Mover
void display() {
stroke(0);
strokeWeight(2);
fill(127, 200);
ellipse(location.x, location.y, mass*16, mass*16);
}
// Bounce off bottom of window
void checkEdges() {
if (location.y > height) {
velocity.y *= -0.9; // A little dampening when hitting the bottom
location.y = height;
}
}
}
@@ -1,72 +0,0 @@
/**
* Forces (Gravity and Fluid Resistence) with Vectors
* by Daniel Shiffman.
*
* Demonstration of multiple force acting on bodies (Mover class)
* Bodies experience gravity continuously
* Bodies experience fluid resistance when in "water"
*/
// Five moving bodies
Mover[] movers = new Mover[11];
// Liquid
Liquid liquid;
void setup() {
size(800, 200);
smooth();
reset();
// Create liquid object
liquid = new Liquid(0, height/2, width, height/2, 0.1);
}
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
PVector dragForce = liquid.drag(movers[i]);
// Apply drag force to Mover
movers[i].applyForce(dragForce);
}
// Gravity is scaled by mass here!
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);
}
void mousePressed() {
reset();
}
// Restart all the Mover objects randomly
void reset() {
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(0.5, 3), 40+i*70, 0);
}
}
@@ -1,62 +0,0 @@
/**
* Forces (Gravity and Fluid Resistence) with Vectors
* by Daniel Shiffman.
*
* Demonstration of multiple force acting on bodies (Mover class)
* Bodies experience gravity continuously
* Bodies experience fluid resistance when in "water"
*/
// Liquid class
class Liquid {
// Liquid is a rectangle
float x,y,w,h;
// Coefficient of drag
float c;
Liquid(float x_, float y_, float w_, float h_, float c_) {
x = x_;
y = y_;
w = w_;
h = h_;
c = c_;
}
// Is the Mover in the Liquid?
boolean contains(Mover m) {
PVector l = m.location;
if (l.x > x && l.x < x + w && l.y > y && l.y < y + h) {
return true;
}
else {
return false;
}
}
// Calculate drag force
PVector drag(Mover m) {
// Magnitude is coefficient * speed squared
float speed = m.velocity.mag();
float dragMagnitude = c * speed * speed;
// 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);
}
}
@@ -1,64 +0,0 @@
/**
* Forces (Gravity and Fluid Resistence) with Vectors
* by Daniel Shiffman.
*
* Demonstration of multiple force acting on bodies (Mover class)
* Bodies experience gravity continuously
* Bodies experience fluid resistance when in "water"
*/
class Mover {
// location, velocity, and acceleration
PVector location;
PVector velocity;
PVector acceleration;
// Mass is tied to size
float mass;
Mover(float m, float x, float y) {
mass = m;
location = new PVector(x, y);
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
}
// Newton's 2nd law: F = M * A
// or A = F / M
void applyForce(PVector force) {
// Divide by mass
PVector f = PVector.div(force, mass);
// Accumulate all forces in acceleration
acceleration.add(f);
}
void update() {
// Velocity changes according to acceleration
velocity.add(acceleration);
// Location changes by velocity
location.add(velocity);
// We must clear acceleration each frame
acceleration.mult(0);
}
// Draw Mover
void display() {
stroke(0);
strokeWeight(2*2.25);
fill(127,200);
ellipse(location.x, location.y, mass*16, mass*16);
}
// Bounce off bottom of window
void checkEdges() {
if (location.y > height) {
velocity.y *= -0.9; // A little dampening when hitting the bottom
location.y = height;
}
}
}
@@ -1,74 +0,0 @@
/**
* Forces (Gravity and Fluid Resistence) with Vectors
* by Daniel Shiffman.
*
* Demonstration of multiple force acting on bodies (Mover class)
* Bodies experience gravity continuously
* Bodies experience fluid resistance when in "water"
*/
// Five moving bodies
Mover[] movers = new Mover[5];
// Liquid
Liquid liquid;
void setup() {
size(450, 450);
smooth();
randomSeed(1);
reset();
// Create liquid object
liquid = new Liquid(0, height/2, width, height/2, 0.1);
}
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
PVector dragForce = liquid.drag(movers[i]);
// Apply drag force to Mover
movers[i].applyForce(dragForce);
}
// Gravity is scaled by mass here!
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(255);
//text("click mouse to reset",10,30);
if (frameCount % 20 == 0) saveFrame("ch2_05_####.png");
}
void mousePressed() {
reset();
}
// Restart all the Mover objects randomly
void reset() {
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(0.5*2.25,3*2.25), 20*2.25+i*40*2.25, 0);
}
}
@@ -1,77 +0,0 @@
// Attraction
// Daniel Shiffman <http://www.shiffman.net>
// A class for a draggable attractive body in our world
class Attractor {
float mass; // Mass, tied to size
float G; // Gravitational Constant
PVector location; // Location
boolean dragging = false; // Is the object being dragged?
boolean rollover = false; // Is the mouse over the ellipse?
PVector dragOffset; // holds the offset for when object is clicked on
Attractor() {
location = new PVector(width/2,height/2);
mass = 20;
G = 1;
dragOffset = new PVector(0.0,0.0);
}
PVector attract(Mover m) {
PVector force = PVector.sub(location,m.location); // Calculate direction of force
float d = force.mag(); // Distance between objects
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction)
float strength = (G * mass * m.mass) / (d * d); // Calculate gravitional force magnitude
force.mult(strength); // Get force vector --> magnitude * direction
return force;
}
// Method to display
void display() {
ellipseMode(CENTER);
strokeWeight(4);
stroke(0);
if (dragging) fill (50);
else if (rollover) fill(100);
else fill(175,200);
ellipse(location.x,location.y,mass*2,mass*2);
}
// The methods below are for mouse interaction
void clicked(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
dragging = true;
dragOffset.x = location.x-mx;
dragOffset.y = location.y-my;
}
}
void hover(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
rollover = true;
}
else {
rollover = false;
}
}
void stopDragging() {
dragging = false;
}
void drag() {
if (dragging) {
location.x = mouseX + dragOffset.x;
location.y = mouseY + dragOffset.y;
}
}
}
@@ -1,51 +0,0 @@
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover() {
location = new PVector(400,50);
velocity = new PVector(1,0);
acceleration = new PVector(0,0);
mass = 1;
}
void applyForce(PVector force) {
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
stroke(0);
strokeWeight(2);
fill(127);
ellipse(location.x,location.y,16,16);
}
void checkEdges() {
if (location.x > width) {
location.x = 0;
} else if (location.x < 0) {
location.x = width;
}
if (location.y > height) {
velocity.y *= -1;
location.y = height;
}
}
}
@@ -1,36 +0,0 @@
Mover m;
Attractor a;
void setup() {
size(800,200);
smooth();
m = new Mover();
a = new Attractor();
}
void draw() {
background(255);
PVector force = a.attract(m);
m.applyForce(force);
m.update();
a.drag();
a.hover(mouseX,mouseY);
a.display();
m.display();
}
void mousePressed() {
a.clicked(mouseX,mouseY);
}
void mouseReleased() {
a.stopDragging();
}
@@ -1 +0,0 @@
mode=JavaScript
@@ -1,77 +0,0 @@
// Attraction
// Daniel Shiffman <http://www.shiffman.net>
// A class for a draggable attractive body in our world
class Attractor {
float mass; // Mass, tied to size
float G; // Gravitational Constant
PVector location; // Location
boolean dragging = false; // Is the object being dragged?
boolean rollover = false; // Is the mouse over the ellipse?
PVector dragOffset; // holds the offset for when object is clicked on
Attractor() {
location = new PVector(width/2,height/2);
mass = 20;
G = 1;
dragOffset = new PVector(0.0,0.0);
}
PVector attract(Mover m) {
PVector force = PVector.sub(location,m.location); // Calculate direction of force
float d = force.mag(); // Distance between objects
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction)
float strength = (G * mass * m.mass) / (d * d); // Calculate gravitional force magnitude
force.mult(strength); // Get force vector --> magnitude * direction
return force;
}
// Method to display
void display() {
ellipseMode(CENTER);
strokeWeight(4);
stroke(0);
if (dragging) fill (50);
else if (rollover) fill(100);
else fill(175,200);
ellipse(location.x,location.y,mass*2,mass*2);
}
// The methods below are for mouse interaction
void clicked(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
dragging = true;
dragOffset.x = location.x-mx;
dragOffset.y = location.y-my;
}
}
void hover(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
rollover = true;
}
else {
rollover = false;
}
}
void stopDragging() {
dragging = false;
}
void drag() {
if (dragging) {
location.x = mouseX + dragOffset.x;
location.y = mouseY + dragOffset.y;
}
}
}
@@ -1,33 +0,0 @@
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover(float m, float x, float y) {
mass = m;
location = new PVector(random(width), random(height));
velocity = new PVector(1, 0);
acceleration = new PVector(0, 0);
}
void applyForce(PVector force) {
PVector f = PVector.div(force, mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
stroke(0);
strokeWeight(2);
fill(0,100);
ellipse(location.x, location.y, mass*25, mass*25);
}
}
@@ -1,46 +0,0 @@
Mover[] movers = new Mover[10];
Attractor a;
void setup() {
size(800, 200);
smooth();
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(0.1, 2), random(width), random(height));
}
a = new Attractor();
}
void draw() {
background(255);
a.display();
a.drag();
a.hover(mouseX, mouseY);
for (int i = 0; i < movers.length; i++) {
PVector force = a.attract(movers[i]);
movers[i].applyForce(force);
movers[i].update();
movers[i].display();
}
}
void mousePressed() {
a.clicked(mouseX, mouseY);
}
void mouseReleased() {
a.stopDragging();
}
@@ -1,48 +0,0 @@
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover(float m, float x, float y) {
mass = m;
location = new PVector(x, y);
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
}
void applyForce(PVector force) {
PVector f = PVector.div(force, mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
stroke(0);
strokeWeight(2);
fill(0, 100);
ellipse(location.x, location.y, mass*24, mass*24);
}
PVector attract(Mover m) {
PVector force = PVector.sub(location, m.location); // Calculate direction of force
float distance = force.mag(); // Distance between objects
distance = constrain(distance, 5.0, 25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction
float strength = (g * mass * m.mass) / (distance * distance); // Calculate gravitional force magnitude
force.mult(strength); // Get force vector --> magnitude * direction
return force;
}
}
@@ -1,42 +0,0 @@
Mover[] movers = new Mover[20];
float g = 0.4;
void setup() {
size(800,200);
smooth();
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(0.1,2),random(width),random(height));
}
}
void draw() {
background(255);
for (int i = 0; i < movers.length; i++) {
for (int j = 0; j < movers.length; j++) {
if (i != j) {
PVector force = movers[j].attract(movers[i]);
movers[i].applyForce(force);
}
}
movers[i].update();
movers[i].display();
}
}