ok, I had this problem once, a while ago, simulating bouncing rigid balls. lost
the code...
The best I think you can do, is, in pseudo code:
mainloop:
-get the current time in ms
-calculate the new position of the ball
-take speed of ball, take direction of ball, calculate the imaginary end
position of the ball
-keep checking (with calculus), if the line ball-beginpos to ball-endpos
crosses a line, if so, say where and at what angle that line is, and calculate
the new imaginary endpos, mirrored about the line closest to the beginpos. (so,
the position of the ball is correct, even if it bounces a couple of times
inside one time-frame) (http://hyperphysics.phy-astr.gsu.edu/hbase/hframe.html
for a good physics reference, also, choose radians, steer away from degrees
http://en.wikipedia.org/wiki/Sine)
-blit ball/line
-handle input
-get the current time in ms, (decide to sleep some milliseconds till next
planned frame, or just continue without sleeping, if in a state of lag)
If you want to simulate a 'real' bouncing ball, reduce the 'energy' of the ball
(mass times speed) each time the ball bounces, by reducing speed with a
percentage (simulating impression-depression, converting energy to the line and
ball in the form of 'heat')
and slightly reduce speed each 'meter' traveled (to simulate air friction).
if you want, you can make this all just like in the real world: include size
and weight of the ball in the air friction part, make the balls bounce against
each other, or go to the 'non rigid' balls (oeeeeeh!), etc.
For good results, simulate reality. So give your ball a mass. Then, applying
'force' to it (by mouse input, or whatever), you can just slam in Newton's
laws, and balls of different weight all behave nicely to the laws of physics.
dont calculate the position of the ball from the top left of a sprite or rect.
Use the MIDDLE. And keep in mind the size of the ball, when calculating the
bounces, I dont want to see big balls sink halfway through the line, before
they bounce (heheheh.. happened to me :) )
A ball at least has a size, a mass, a position, a speed, and a direction of
speed.
hope you are writing a 2d environment :)
_________________________________________________________________
Share life as it happens with the new Windows Live.Download today it's FREE!
http://www.windowslive.com/share.html?ocid=TXT_TAGLM_Wave2_sharelife_112007