Ball – Ball Collision

Below is a python code written in codeskulptor to test the reflection of a ball when it collides with another ball. Different values can be given to v and v1 to test it if is working in the manner we want to.

from math import *
v = [1, 0]
v1= [0, 0]
angle = pi/4

def update_collision(v,angle):
v1[0]=v[0]*(sin(angle)**2-cos(angle)**2)-2*v[1]*sin(angle)*cos(angle)
v1[1]=-v[1]*(sin(angle)**2-cos(angle)**2)-2*v[0]*sin(angle)*cos(angle)
return v1

print update_collision(v)