I have made a ball sprite which is supposed to bounce around the screen. It works for the bottom and right side of the screen, but not the left or the top. (Note that the x, y is counted from the top left corner, i.e x is 0 at the left side and y is 0 at the top). Once the ball touches the top or the left, it just goes into it and gets stuck there. Like this:
Heres the edge detection code:
def edgedetect(self):
if self.position.x + self.radius >= width or self.position.x <= self.radius:
self.velocity.x *= -0.9
self.velocity.y *= 0.99
if self.position.y + self.radius >= height or self.position.y <= self.radius:
self.velocity.y *= -0.9
self.velocity.x *= 0.99
(x,y is counted from the top and left respectively)
self.position: a vector which holds the coords of the center of the ball
self.velocity: a vector which holds the velocity of the ball, which is added onto the position every frame
Is there a better way to do this ?