How to make a ball bounce with gravity
in
Programming Questions
•
2 years ago
Im trying to make a ball bounce when it hits the bottom of the screen. I want it to bounce as if it were a real ball, with every bounce covering less height than the one before. I've already programmed the gravity, its just the bounce which im finding difficult.
Here is the code.
int x = 200;
int y = 50;
int speed = 0;
int grav = 1 ;
//////////////////////
void setup() {
size (400,800);
smooth();
noStroke();
}
/////////////////////
void draw() {
background (0);
ellipse (x,y,30,30);
//This is where it makes gravity plus the ellpse move
y = y + speed;
speed = speed + grav;
//This is the if statement im stuck on. Regarding the bounce.
//If i leave it with speed = - speed then it bounces and comes
//down but everytime it goes higher.
if (y > 785){
speed= -speed
}
println(speed);
}
2