Created
April 4, 2019 13:46
-
-
Save zawa-works/adfd2ffbd95e8f9757153a44e93c295d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayList<EasingBall> easingBall = new ArrayList(); | |
int frameCount; | |
void setup() { | |
size(640, 360); | |
for (int i = 0; i < 50; i++) { | |
EasingBall eb = new EasingBall(); | |
eb.setRadius(20 - (i/10)); | |
eb.setColor((int)random(#000000, #ffffff)); | |
easingBall.add(eb); | |
} | |
} | |
void draw() { | |
background(#ffffff); | |
for (EasingBall eb : easingBall)eb.display(); | |
} | |
class EasingBall { | |
float easing; | |
float targetX, targetY; | |
float posX, posY; | |
float r; | |
color c; | |
void setRadius(float _r) { | |
r = _r; | |
easing = 0.5/r; | |
setBallPos(); | |
setTargetPosition(); | |
} | |
void setColor(color _c) { | |
c = _c; | |
} | |
void setBallPos() { | |
posX = random(r, width-r); | |
posY = random(r, height-r); | |
} | |
void setTargetPosition() { | |
targetX = random(r, width-r); | |
targetY = random(r, height-r); | |
} | |
void display() { | |
updatePos(); | |
noStroke(); | |
fill(c); | |
ellipse(posX, posY, 2*r, 2*r); | |
if (dist(posX, posY, targetX, targetY) < 10)setTargetPosition(); | |
} | |
void updatePos() { | |
float dx = targetX - posX; | |
dx *= easing; | |
float dy = targetY - posY; | |
dy *= easing; | |
posX += dx; | |
posY += dy; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment