Hi
It's easy to do - you've got yourself tangled up in who does what.
All the time, the object has gravity acting on it, which causes it to fall.
Separately, you need to deal with what happens when the object hits the ground.
I've made some amendments to your code (apart from clearer variable names and punctuation):
- I've set gravity to 0.98
- I've created a new variable called vy for the velocity in the y axis. This is a good habit to get into as the obvious extension to your program is to have sideways velocity too: vx
- I've created a bounce variable - play with this to see what happens
- and I've made all the variables into floats
- float xpos = 200;
float ypos = 50;
float vy = 0;
float gravity = 0.98;
float bounce = -1;
void setup()
{
size(400,800);
smooth();
noStroke();
}
- void draw()
{
background(0);
ellipse(xpos, ypos, 30, 30);
- // I've separated out the change in velocity and the change in position
- //so that you can see what's happening
vy += gravity;
ypos += vy;
- if(ypos > height - 15)
{
vy *= bounce;
}
println(vy);
}