Bouncing ball
I tweaked my XNA Game, adding gravity and bouncy behavior.
The code is pretty easy to get 90% of it right, but there is still some strangeness that I could spend some time cleaning up.
Gravity is merely adding speed over time. So, the stock code provided by the MS tutorial simply kept the “speed” at 1 and updated the Y height by 1 on each update. When the ball hit the bottom, it then moved up at a “speed” of 1.
For this demo, I modified the speed adding gravity which sped up the ball. Speed simply increases and I update the Y height by speed on each update.
When I hit the bottom, speed reverses in a perfect elastic collission. On the way up, speed is negative since Y=0 is at the top. Gravity, however, keeps pulling speed from negative to 0, and then back into positive, causing the ball to fall back down.
I added a friction coefficient which slows down the ball over time, multiplying speed (positive or negative) by a constant factor.
If you’d like me to post my code, let me know. If you’d like to correct my math, please do!