Minor changes to examples, more code color tweaks for 2b8

This commit is contained in:
Casey Reas
2012-12-20 03:21:07 +00:00
parent cebf10855e
commit 234cbdd85f
13 changed files with 114 additions and 138 deletions
@@ -12,17 +12,16 @@ float gravity = 0.03;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];
void setup()
{
void setup() {
size(640, 360);
noStroke();
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), random(30, 70), i, balls);
}
noStroke();
fill(255, 204);
}
void draw()
{
void draw() {
background(0);
for (int i = 0; i < numBalls; i++) {
balls[i].collide();
@@ -32,6 +31,7 @@ void draw()
}
class Ball {
float x, y;
float diameter;
float vx = 0;
@@ -90,7 +90,6 @@ class Ball {
}
void display() {
fill(255, 204);
ellipse(x, y, diameter, diameter);
}
}
@@ -5,7 +5,8 @@
* Based on Keith Peter's Solution in
* Foundation Actionscript Animation: Making Things Move!
*/
Ball[] balls = {
new Ball(100, 400, 20),
new Ball(700, 400, 80)
@@ -24,7 +25,7 @@ void setup() {
void draw() {
background(51);
fill(204);
for (int i=0; i< 2; i++){
for (int i = 0; i < 2; i++){
balls[i].x += vels[i].x;
balls[i].y += vels[i].y;
ellipse(balls[i].x, balls[i].y, balls[i].r*2, balls[i].r*2);
@@ -52,7 +53,8 @@ void checkObjectCollision(Ball[] b, PVector[] v){
/* bTemp will hold rotated ball positions. You
just need to worry about bTemp[1] position*/
Ball[] bTemp = {
new Ball(), new Ball() };
new Ball(), new Ball()
};
/* b[1]'s position is relative to b[0]'s
so you can use the vector between them (bVect) as the
@@ -65,7 +67,8 @@ void checkObjectCollision(Ball[] b, PVector[] v){
// rotate Temporary velocities
PVector[] vTemp = {
new PVector(), new PVector() };
new PVector(), new PVector()
};
vTemp[0].x = cosine * v[0].x + sine * v[0].y;
vTemp[0].y = cosine * v[0].y - sine * v[0].x;
vTemp[1].x = cosine * v[1].x + sine * v[1].y;
@@ -75,14 +78,15 @@ void checkObjectCollision(Ball[] b, PVector[] v){
conservation of momentum equations to calculate
the final velocity along the x-axis. */
PVector[] vFinal = {
new PVector(), new PVector() };
new PVector(), new PVector()
};
// final rotated velocity for b[0]
vFinal[0].x = ((b[0].m - b[1].m) * vTemp[0].x + 2 * b[1].m *
vTemp[1].x) / (b[0].m + b[1].m);
vTemp[1].x) / (b[0].m + b[1].m);
vFinal[0].y = vTemp[0].y;
// final rotated velocity for b[0]
vFinal[1].x = ((b[1].m - b[0].m) * vTemp[1].x + 2 * b[0].m *
vTemp[0].x) / (b[0].m + b[1].m);
vTemp[0].x) / (b[0].m + b[1].m);
vFinal[1].y = vTemp[1].y;
// hack to avoid clumping
@@ -94,7 +98,8 @@ void checkObjectCollision(Ball[] b, PVector[] v){
in the opposite direction */
// rotate balls
Ball[] bFinal = {
new Ball(), new Ball() };
new Ball(), new Ball()
};
bFinal[0].x = cosine * bTemp[0].x - sine * bTemp[0].y;
bFinal[0].y = cosine * bTemp[0].y + sine * bTemp[0].x;
bFinal[1].x = cosine * bTemp[1].x - sine * bTemp[1].y;
@@ -17,16 +17,14 @@ float y = 0.0; // Current y-coordinate
float step = 0.01; // Size of each step along the path
float pct = 0.0; // Percentage traveled (0.0 to 1.0)
void setup()
{
void setup() {
size(640, 360);
noStroke();
distX = endX - beginX;
distY = endY - beginY;
}
void draw()
{
void draw() {
fill(0, 2);
rect(0, 0, width, height);
pct += step;
+23 -22
View File
@@ -9,6 +9,7 @@
* body segments, controlling body shape and jitter.
*/
// For puff head
float headX;
float headY;
@@ -17,13 +18,13 @@ float speedY = .9;
// For puff body
int cells = 1000;
float[]px= new float[cells];
float[]py= new float[cells];
float[]radiiX = new float[cells];
float[]radiiY = new float[cells];
float[]angle = new float[cells];
float[]frequency = new float[cells];
float[]cellRadius = new float[cells];
float[] px= new float[cells];
float[] py= new float[cells];
float[] radiiX = new float[cells];
float[] radiiY = new float[cells];
float[] angle = new float[cells];
float[] frequency = new float[cells];
float[] cellRadius = new float[cells];
void setup(){
@@ -34,7 +35,7 @@ void setup(){
headY = height/2;
// Fill body arrays
for (int i=0; i< cells; i++){
for (int i = 0; i < cells; i++){
radiiX[i] = random(-7, 7);
radiiY[i] = random(-4, 4);
frequency[i]= random(-9, 9);
@@ -49,42 +50,42 @@ void draw(){
fill(255, 255, 255, 5);
// Follow the leader
for (int i =0; i< cells; i++){
if (i==0){
px[i] = headX+sin(radians(angle[i]))*radiiX[i];
py[i] = headY+cos(radians(angle[i]))*radiiY[i];
for (int i = 0; i < cells; i++){
if (i == 0){
px[i] = headX + sin(radians(angle[i]))*radiiX[i];
py[i] = headY + cos(radians(angle[i]))*radiiY[i];
}
else{
px[i] = px[i-1]+cos(radians(angle[i]))*radiiX[i];
py[i] = py[i-1]+sin(radians(angle[i]))*radiiY[i];
px[i] = px[i-1] + cos(radians(angle[i]))*radiiX[i];
py[i] = py[i-1] + sin(radians(angle[i]))*radiiY[i];
// Check collision of body
if (px[i] >= width-cellRadius[i]/2 || px[i] <= cellRadius[i]/2){
if ((px[i] >= width-cellRadius[i]/2) || (px[i] <= cellRadius[i]/2)){
radiiX[i]*=-1;
cellRadius[i] = random(1, 40);
frequency[i]= random(-13, 13);
}
if (py[i] >= height-cellRadius[i]/2 || py[i] <= cellRadius[i]/2){
if ((py[i] >= height-cellRadius[i]/2) || (py[i] <= cellRadius[i]/2)){
radiiY[i]*=-1;
cellRadius[i] = random(1, 40);
frequency[i]= random(-9, 9);
}
}
// Draw puff
ellipse(px[i], py[i], cellRadius[i], cellRadius[i]);
ellipse(px[i], py[i], cellRadius[i], cellRadius[i]);
// Set speed of body
angle[i]+=frequency[i];
angle[i] += frequency[i];
}
// Set velocity of head
headX+=speedX;
headY+=speedY;
headX += speedX;
headY += speedY;
// Check boundary collision of head
if (headX >= width-cellRadius[0]/2 || headX <=cellRadius[0]/2){
if ((headX >= width-cellRadius[0]/2) || (headX <=cellRadius[0]/2)){
speedX*=-1;
}
if (headY >= height-cellRadius[0]/2 || headY <= cellRadius[0]/2){
if ((headY >= height-cellRadius[0]/2) || (headY <= cellRadius[0]/2)){
speedY*=-1;
}
}
@@ -17,7 +17,7 @@ float velocityX, velocityY;
void setup(){
size(640, 360);
frameRate(30);
fill(128);
baseX1 = 0;
baseY1 = height-150;
@@ -32,8 +32,7 @@ void setup(){
directionY = random(0.1, 0.99);
// normalize direction vector
float directionVectLength = sqrt(directionX*directionX +
directionY*directionY);
float directionVectLength = sqrt(directionX*directionX + directionY*directionY);
directionX /= directionVectLength;
directionY /= directionVectLength;
}