assignment 5(2)

this ia a sigle bouce ball , when it moves, the color will change. if the mouse pressed, it will show the trace of the ball slao with random color.

float fro = -0.001;
float grav = 0.1;
float c = – 0.95;
int sx = 300;
int sy = 300;
float rad = 10;
int deltacolo = 1;
int colo = 0;
color bg = color(255);
int n = 100;
Ball b = new Ball(0, 500, 10, 10, 0, 0, colo);
Ball[] trace = new Ball [n];
void setup() {
smooth();
for (int i = 0; i < n; i += 1){
trace[i] = b;
}
colorMode(HSB);
size(sx, sy);
noStroke();
}
class Ball {
float x, y, vx, vy, ax, ay;
int colo;
Ball(float xx, float yy, float vvx, float vvy, float aax, float aay, int c) {
x = xx;
y = yy;
vx = vvx;
vy = vvy;
ax = aax;
ay = aay;
colo = c;
}
void upd() {
colo += deltacolo; 
if (x < rad) {
vx *= c;
x = rad;
}
if (x > sx – rad) {
vx *= c;
x = sx – rad;
}
if (y < rad) {
vy *= c;
y = rad;
}
if (y > sy – rad) {
vy *= c;
y = sy – rad;
}
ax = fro * vx;
ay = fro * vy;
vx += ax;
vy += ay + grav;
x += vx;
y += vy;
}
void drawb(float rad) {
fill(color(colo%360, 255, 255));
ellipse(x, y, 2 * rad, 2 * rad);
}
}
void draw() {
background(bg);
b.upd();
b.drawb(rad);
for (int i = n – 1; i > 0; i -= 1){
trace[i] = trace [i – 1];
trace[i].drawb(rad * (n – i) / n);
}
trace[0] = b;
if (mousePressed == true) {

b = new Ball ( mouseX, mouseY, 0, 0, 0, 0, b.colo);
}
}
void mouseReleased() {
b = new Ball ( mouseX, mouseY, mouseX – pmouseX, mouseY – pmouseY, 0, 0, b.colo);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s