float X1, X2, Y1, Y2;
float Spx1, Spy1, Spx2, Spy2;
int r;
void setup(){
size(400, 400);
colorMode(HSB, 100);
X1 = random(r, width-r);
X2 = random(r, width-r);
Y1 = random(r, height-r);
Y2 = random(r, height-r);
Spx1 = 5;
Spy1 = Spx1;
Spx2 = 3;
Spy2 = -Spx2;
r = 20;
frameRate(60);
smooth();
}
void draw(){
fadeToWhite(99, 100);
bounce();
collide();
X1 += Spx1;
Y1 += Spy1;
X2 += Spx2;
Y2 += Spy2;
fill(90, 60, 60, 90);
ellipse(X1, Y1, r*2, r*2);
fill(40, 60, 60, 90);
ellipse(X2, Y2, r*2, r*2);
}
void bounce() {
if (X1 <= r || X1 >= width-r){
Spx1 = -Spx1;
if (X1 <= r) { X1 = 2*r - X1; }
if (X1 >= width-r) { X1 = 2*(width-r) - X1; }
}
if (X2 <= r || X2 >= width-r){
Spx2 = -Spx2;
if (X2 <= r){ X2 = 2*r - X2; }
if (X2 >= width-r) { X2 = 2*(width-r) - X2; }
}
if (Y1 <= r || Y1 >= height-r){
Spy1 = -Spy1;
if (Y1 <= r) { Y1 = 2*r - Y1; }
if (Y1 >= height-r) { Y1 = 2*(height-r) - Y1; }
}
if (Y2 <= r || Y2 >= height-r){
Spy2 = -Spy2;
if (Y2 <= r){ Y2 = 2*r - Y2; }
if (Y2 >= height-r) { Y2 = 2*(height-r) - Y2; }
}
}
void collide() {
float dd = dist(X1, Y1, X2, Y2);
float min = r * 2;
if (dd <= min){
Spx1 = -Spx1;
Spy1 = -Spy1;
Spx2 = -Spx2;
Spy2 = -Spy2;
X1 = X1 - (min - dd);
X2 = X2 - (min - dd);
Y1 = Y1 - (min - dd);
Y2 = Y2 - (min - dd);
}
}
void fadeToWhite(int max_color, float fade_speed) {
noStroke();
fill(max_color, fade_speed);
rectMode(CORNER);
rect(0, 0, width, height);
}
Tuesday, September 14, 2010
Processing: collition2
衝突でボールが離れなくなるバグ有り
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment