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

Re: [pygame] Why does my ball vibrate?



Thanks for all the help.

I have modified the code for determining the position of the ball so that the pixel co-ordinates are always integers:

    ypos = ypos + int(((velocity + newvelocity) / 2) * time_passed * 160)

This has cured the vibrating problem and the ball comes to rest quite realistically. I tried consolidating the code checking for the floor but found that the ball sinks into the floor unless I always correct an over shoot of the floor to the co-ordinates of the floor.

# Reverse velocity taking into account bounciness if we hit the ground
    if ypos >= 384 and velocity > 0:
        # Avoid the ball sinking into the ground
        ypos = 384
        velocity = -velocity * bounce

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.

I am interested by the idea of separating the physics calculations from the frame rate calculations but I really don't know where to start.

Thanks again,

Matt