Skip to content

Instantly share code, notes, and snippets.

@KentaKomai
Created June 24, 2014 01:10
Show Gist options
  • Save KentaKomai/3077f87e749ee332e155 to your computer and use it in GitHub Desktop.
Save KentaKomai/3077f87e749ee332e155 to your computer and use it in GitHub Desktop.
跳ね返るボールのサンプル
float FRICTION = 0.99;
float x, y;
float speedY, speedX;
int radius = 10;
void setup(){
size(200, 200);
colorMode(HSB, 100);
background(99);
noStroke();
frameRate(15);
x = width/2;
y = height/2;
speedY = random(-30, 30);
speedX = random(-30, 30);
}
void draw(){
//background(99);
fadeToWhite();
speedX = speedX * FRICTION;
speedY = speedY * FRICTION;
x += speedX;
y += speedY;
bounce();
fill(0);
ellipse(x,y, radius*2, radius*2);
}
void bounce() {
float bounceMinX = radius;
float bounceMaxX = width - radius;
float bounceMinY = radius;
float bounceMaxY = height - radius;
if(x < bounceMinX || x > bounceMaxX){
speedX = -speedX;
if(x < bounceMinX) x = bounceMinX - (x - bounceMinX);
if(x > bounceMaxX) x = bounceMaxX - (x - bounceMaxX);
}
if(y < bounceMinY || y > bounceMaxY){
speedY = -speedY;
if(y < bounceMinY) y = bounceMinY - (y - bounceMinY);
if(y > bounceMaxY) y = bounceMaxY - (y - bounceMaxY);
}
}
void fadeToWhite() {
noStroke();
fill(99, 60);
rectMode(CORNER);
rect(0,0,width, height);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment