This commit is contained in:
Casey Reas
2011-09-06 05:22:47 +00:00
parent 3b84c790cd
commit 9775dedaad
17 changed files with 89 additions and 311 deletions

View File

@@ -9,14 +9,14 @@
Spring2D s1, s2;
float gravity = 6.0;
float gravity = 9.0;
float mass = 2.0;
void setup()
{
size(200, 200);
size(640, 360);
smooth();
fill(0);
fill(255, 126);
// Inputs: x, y, mass, gravity
s1 = new Spring2D(0.0, width/2, mass, gravity);
s2 = new Spring2D(0.0, width/2, mass, gravity);
@@ -24,7 +24,7 @@ void setup()
void draw()
{
background(204);
background(0);
s1.update(mouseX, mouseY);
s1.display(mouseX, mouseY);
s2.update(s1.x, s1.y);
@@ -36,7 +36,7 @@ class Spring2D {
float x, y; // The x- and y-coordinates
float gravity;
float mass;
float radius = 20;
float radius = 30;
float stiffness = 0.2;
float damping = 0.7;

View File

@@ -63,7 +63,7 @@ class Boid {
// Takes a second argument, if true, it slows down as it approaches the target
PVector steer(PVector target, boolean slowdown) {
PVector steer; // The steering vector
PVector desired = target.sub(target,loc); // A vector pointing from the location to the target
PVector desired = PVector.sub(target,loc); // A vector pointing from the location to the target
float d = desired.mag(); // Distance from the target is the magnitude of the vector
// If the distance is greater than 0, calc steering (otherwise return zero vector)
if (d > 0) {
@@ -73,7 +73,7 @@ class Boid {
if ((slowdown) && (d < 100.0)) desired.mult(maxspeed*(d/100.0)); // This damping is somewhat arbitrary
else desired.mult(maxspeed);
// Steering = Desired minus Velocity
steer = target.sub(desired,vel);
steer = PVector.sub(desired,vel);
steer.limit(maxforce); // Limit to maximum steering force
}
else {

View File

@@ -11,7 +11,7 @@ Random generator;
void setup() {
size(640, 200);
size(640, 360);
colorMode(RGB, 255, 255, 255, 100);
// Using a Java random number generator for Gaussian random numbers

View File

@@ -5,47 +5,47 @@
*/
// Spring drawing constants for top bar
int s_height = 16; // Height
int left = 50; // Left position
int right = 150; // Right position
int max = 100; // Maximum Y value
int min = 20; // Minimum Y value
boolean over = false; // If mouse over
boolean move = false; // If mouse down and over
int springHeight = 16; // Height
int left; // Left position
int right; // Right position
int max = 200; // Maximum Y value
int min = 100; // Minimum Y value
boolean over = false; // If mouse over
boolean move = false; // If mouse down and over
// Spring simulation constants
float M = 0.8; // Mass
float K = 0.2; // Spring constant
float D = 0.92; // Damping
float R = 60; // Rest position
float R = 150; // Rest position
// Spring simulation variables
float ps = 60.0; // Position
float ps = R; // Position
float vs = 0.0; // Velocity
float as = 0; // Acceleration
float f = 0; // Force
void setup()
{
size(200, 200);
void setup() {
size(640, 360);
rectMode(CORNERS);
noStroke();
left = width/2 - 100;
right = width/2 + 100;
}
void draw()
{
void draw() {
background(102);
updateSpring();
drawSpring();
}
void drawSpring()
{
void drawSpring() {
// Draw base
fill(0.2);
float b_width = 0.5 * ps + -8;
rect(width/2 - b_width, ps + s_height, width/2 + b_width, 150);
float baseWidth = 0.5 * ps + -8;
rect(width/2 - baseWidth, ps + springHeight, width/2 + baseWidth, height);
// Set color and draw top bar
if(over || move) {
@@ -53,12 +53,11 @@ void drawSpring()
} else {
fill(204);
}
rect(left, ps, right, ps + s_height);
rect(left, ps, right, ps + springHeight);
}
void updateSpring()
{
void updateSpring() {
// Update the spring position
if(!move) {
f = -K * (ps - R); // f=-ky
@@ -71,7 +70,7 @@ void updateSpring()
}
// Test if mouse is over the top bar
if(mouseX > left && mouseX < right && mouseY > ps && mouseY < ps + s_height) {
if(mouseX > left && mouseX < right && mouseY > ps && mouseY < ps + springHeight) {
over = true;
} else {
over = false;
@@ -79,9 +78,8 @@ void updateSpring()
// Set and constrain the position of top bar
if(move) {
ps = mouseY - s_height/2;
if (ps < min) { ps = min; }
if (ps > max) { ps = max; }
ps = mouseY - springHeight/2;
ps = constrain(ps, min, max);
}
}
@@ -91,7 +89,6 @@ void mousePressed() {
}
}
void mouseReleased()
{
void mouseReleased() {
move = false;
}

View File

@@ -12,12 +12,12 @@ Spring[] springs = new Spring[num];
void setup()
{
size(200, 200);
size(640, 360);
noStroke();
smooth();
springs[0] = new Spring( 70, 160, 20, 0.98, 8.0, 0.1, springs, 0);
springs[1] = new Spring(150, 110, 60, 0.95, 9.0, 0.1, springs, 1);
springs[2] = new Spring( 40, 70, 120, 0.90, 9.9, 0.1, springs, 2);
springs[0] = new Spring(240, 260, 40, 0.98, 8.0, 0.1, springs, 0);
springs[1] = new Spring(320, 210, 120, 0.95, 9.0, 0.1, springs, 1);
springs[2] = new Spring(180, 170, 200, 0.90, 9.9, 0.1, springs, 2);
}
void draw()
@@ -104,7 +104,7 @@ class Spring
tempxpos = tempxpos + velx; // Updated position
if ((over() || move) && !otherOver() ) {
if ((overEvent() || move) && !otherOver() ) {
over = true;
} else {
over = false;
@@ -112,7 +112,7 @@ class Spring
}
// Test to see if mouse is over this spring
boolean over() {
boolean overEvent() {
float disX = tempxpos - mouseX;
float disY = tempypos - mouseY;
if (sqrt(sq(disX) + sq(disY)) < size/2 ) {