Tkinter frame with bouncing balls in it. from sccd.runtime.libs import ui_v2 as ui import random import numpy as np CANVAS_DIMS = (800, 550) self.nr_of_fields = 0 self.balls = {} self.collisions = [] return ((point1['x'] - point2['x']) ** 2 + (point1['y'] - point2['y']) ** 2) ** 0.5 self.window_id = window_id; self.event_name = event_name; self.canvas_id = canvas_id; self.r = 20.0; self.vel = {'x': random.uniform(-5.0, 5.0), 'y': random.uniform(-5.0, 5.0)}; self.pos = {'x': x, 'y': y}; self.smooth = 0.6; # value between 0 and 1 self.ball1_id = ball1_id self.ball2_id = ball2_id self.ball1_info = ball1_info self.ball2_info = ball2_info # Resolve the velocity into normal and tangential components. normal_velocity = np.dot(vel, normal) * normal tangential_velocity = vel - normal_velocity return normal_velocity, tangential_velocity pos1 = np.array([self.ball1_info['pos']['x'], self.ball1_info['pos']['y']]) vel1 = np.array([self.ball1_info['vel']['x'], self.ball1_info['vel']['y']]) r1 = self.ball1_info['r'] pos2 = np.array([self.ball2_info['pos']['x'], self.ball2_info['pos']['y']]) vel2 = np.array([self.ball2_info['vel']['x'], self.ball2_info['vel']['y']]) r2 = self.ball2_info['r'] # Calculate the normal vector from the center of ball1 to the center of ball2 normal = pos2 - pos1 normal = normal / np.linalg.norm(normal) # Normalize the normal vector # Calculate the relative velocity relative_velocity = vel1 - vel2 # Calculate the velocities in the normal direction vel1_normal, vel1_tangential = self.resolve_velocity(vel1, normal) vel2_normal, vel2_tangential = self.resolve_velocity(vel2, normal) # Calculate new normal velocities after collision (1D elastic collision formula) vel1_normal_new = ((r1 - r2) * vel1_normal + 2 * r2 * vel2_normal) / (r1 + r2) vel2_normal_new = ((r2 - r1) * vel2_normal + 2 * r1 * vel1_normal) / (r1 + r2) # Combine the new normal and original tangential components vel1_new = vel1_normal_new + vel1_tangential vel2_new = vel2_normal_new + vel2_tangential # Update the velocities in the balls self.ball1_info['vel'] = {'x': vel1_new[0], 'y': vel1_new[1]} self.ball2_info['vel'] = {'x': vel2_new[0], 'y': vel2_new[1]}