This is my collision response code for a billiards simulation of 2 moving balls colliding. I used newton's laws of equation and then parametrised the components of the velocity vectors,
Alpha is the trajectory of ball 1
Beta is the trajectory of ball 2
Theta is the angle of the line of centres with respect to the axis
My idea was to come up with a general formula for collision response in the local frame of the 2 balls then reproject it back into my fixed frame, but I noticed that there are some angle problems, I can't figure out for my life what conditions should be for i.e. alpha
the last bit is just to make sure the balls aren't overlapping after the collision and get stuck in an infinite loop of colliding
def collision(b1,b2):
dx=b1.x-b2.x
dy=b1.y-b2.y
Theta = atan2(dy,dx)
Vx1=b1.speedx
Vy1=b1.speedy
Vx2=b2.speedx
Vy2=b2.speedy
V1 = sqrt((Vx1)**2+(Vx1)**2)
V2 = sqrt((Vx2)**2+(Vx1)**2)
Alpha = asin(Vx1/V1)
Beta = asin(Vx2/V2)
b1.speedx = (((1-e)/2)*V1*sin(Alpha-Theta) - ((1+e)/2)*V2*sin(Beta-Theta))*sin(Theta) - V1*cos(Alpha-Theta)*cos(Theta)
b1.speedy = (((1-e)/2)*V1*sin(Alpha-Theta) - ((1+e)/2)*V2*sin(Beta-Theta))*cos(Theta) + V1*cos(Alpha-Theta)*sin(Theta)
b2.speedx = (((1+e)/2)*V1*sin(Alpha-Theta) - ((1-e)/2)*V2*sin(Beta-Theta))*sin(Theta) + V2*cos(Beta-Theta)*cos(Theta)
b2.speedy = (((1+e)/2)*V1*sin(Alpha-Theta) - ((1-e)/2)*V2*sin(Beta-Theta))*cos(Theta) - V2*cos(Beta-Theta)*sin(Theta)
b2is initially stationary, thenV2is zero, so after the collisionb1.speedx=b1.speedy, in other wordsb1winds up traveling in the direction (1,1) no matter how it hitb2. Your physics is just wrong. - Beta