I am trying to create a simple bouncy ball simulator, and I want the balls to move at approximately the same speed as they would in real life. However, currently, after being dropped from 5 feet, the ball hits the ground almost instantly and bounces a few times before stopping in than a second. I know I could just experiment with my gravity value until it's semi-realistic, but I'm confused as to why my current gravity value doesn't work. Here's how I got the current one:
Gravity in real life = 9.8 m/sec^2
= 32.152 ft/sec^2
= 1.072 ft per 1/30th of a sec^2 (my frame rate is set to 30 in my program)
= 102.887 pixels per 1/30th of a sec^2 (a foot is 96 pixels in my program)
Here's my code for moving the ball (using Processing 3.2.1):
void move() {
dy+=102.88704; //gravity
x+=dx;
y+=dy;
z+=dz;
if(y+size*8>480) {
dy*=-0.85;
}
y=constrain(y,-100000.0,480-(size*8));
}
Currently, x and z just stay at 0. Since it's being dropped from 5 feet, it hits the ground when it gets to 480-size*8 (size is in inches). The 0.85 value is temporary and I might tweak it later, but it shouldn't have any impact on this issue. Any and all help is greatly appreciated. Thanks!
Your mistake is in converting your measurements from seconds to 1/30 seconds. Note that the unit is ft/sec^2: that is seconds squared. So to convert the time unit from seconds to 1/30 seconds you must also square the 1/30. Therefore
32.152 ft/sec^2
= 32.152/30^2 ft/(1/30 sec)^2
= 0.035724 ft/(1/30 sec)^2
= 0.035724 * 96 pixels/(1/30 sec)^2
= 3.4295 pixels/(1/30 sec)^2
So try replacing the number 102.88704
with 3.4295
and see if that fixes the problem.