bouncing ball
How could I change the code to make a bouncing ball that loses some of its height with each bounce as a real ping pong ball would if it were dropped onto a hard floor?
- float x =32, y =128;
float dx,dy;
float gravity = 5;
float ballsize =20;
void setup()
{
size(512, 512, P3D);
fill(255); noStroke();
frameRate(20);
background(0);
dx=5;
dy=10;
}
void draw()
{
background(0);
translate(x,y);
ellipse(0,0, ballsize,ballsize);
if(x>width-ballsize/2 || x<ballsize/2)
dx = -dx;
if (y>height - ballsize/2)
dy = -dy;
else
dy += gravity;
x += dx;
y += dy;
}