[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Why does my ball vibrate?



Greg Ewing wrote:

> Matt Smith wrote:
>> I can't quite get my head around WHY the ball sinks into the ground if
>> I don't set ypos back to 384 but it certainly works.
> 
> It's probably because you're applying gravity to the ball
> even when it's touching the ground. So it repeatdly gets
> accelerated downwards for one time step, then moved back
> up to ground level.
> 
> In real life, when the ball is touching the ground, the
> downward force of gravity is balanced by an upward force
> from the ground, so the net acceleration is zero. You
> can model that by only applying gravity if the ball is
> above ground level.
> 


I have a slightly different take on it: assuming an instantaneous
bounce, the ball should have travelled up in proportion to the distance
it would have sunk below, like so:

    FLOOR = 384

    if ypos >= FLOOR and velocity > 0:
        overstep = ypos - FLOOR
	ypos = FLOOR - overstep * bounce
        velocity = -velocity * bounce

This is unrealistic, of course, since a ball spends a period of time
deforming before bouncing back up, but at least it is consistent, while
just truncating gives you a different error each time.

But, anyway, the reason it sinks is it always goes down further than it
can come up, because bounce < 1 and gravity points down, so the upward
velocity is always less than the downward. For a period towards the end
of the journey, the upward velocity will not be enough to lift the ball
above the floor before it turns to drop again. Rounding might exacerbate
this.


douglas