Search code examples
pythonphysics

weird physics bug when a ball hits the left wall


for some reason when a ball hits the left wall it gains momentum

i tried using "* -1" instead of "-"

heres the code


y = y + vel_y
x = x + vel_x
vel_y = vel_y + 1
if y >= 900:
    y = 900
    vel_y = -(vel_y - 10)
if x >= 900:
    x = 900
    vel_x = -(vel_x - 10)
if y <= 100:
    y = 100
    vel_y = -(vel_y - 10)
if x <= 100:
    x = 100
    vel_x = -(vel_x - 10)

im using python and pygame


Solution

  • Since vel_x is negative when going left, the calculation -(-vel_x - 10) gives vel_x + 10, which will be bigger than the velocity before you hit the wall. Say the initial velocity is -50, you get -(-50 - 10) = 50 + 10 = 60. Make sure subtraction of the speed reduction makes sense.