« XNA doesn't like Vista | Main | Reflections in Adobe Illustrator and XAML »

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!

TrackBack

TrackBack URL for this entry:
http://www.primordia.com/blog/mt-tb.cgi/621

Listed below are links to weblogs that reference Bouncing ball:

» Bouncing glass ball... almost from Primordial Ooze
I’m making some progress on my planned glassy bouncy ball animation. I did this with Microsoft’s XNA Game Studio. However, the ballw as a pre-rendered PNG file… not very sexy. Here is a screenshot of a resolution-independent screen g... [Read More]