import random as rnd
import billiard4 as blrd
import math
# ============== constants ====================
blrd.size = size = (800, 600) # size of the field for billiard
blrd.r_max = r_max = 20 # maximal radius of the balls
blrd.v_max = v_max = 8. # maximal speed of the balls
blrd.m_max = 15. # maxima mass of the balls
# ======== function to calculate new velocity after collision ========
def calculate_velocity_after_collision(ball1, ball2):
# Получаем координаты и скорости шариков
x1, y1 = ball1.x, ball1.y
x2, y2 = ball2.x, ball2.y
# Определяем углы для разлета в углы
angle1 = math.atan2(y1, x1) + math.pi / 4 # Угол для первого шарика
angle2 = math.atan2(y2, x2) + 3 * math.pi / 4 # Угол для второго шарика
# Устанавливаем скорости
speed1 = v_max
speed2 = v_max
ball1.v_x = speed1 * math.cos(angle1)
ball1.v_y = speed1 * math.sin(angle1)
ball2.v_x = speed2 * math.cos(angle2)
ball2.v_y = speed2 * math.sin(angle2)
# ======== construction of configuration =====
ball1 = blrd.ball(x=100, y=100, v_x=0, v_y=0, r=20, m=3) # Первый шарик
ball2 = blrd.ball(x=200, y=150, v_x=0, v_y=0, r=20, m=3) # Второй шарик
# Задаем начальные скорости после столкновения
calculate_velocity_after_collision(ball1, ball2)
ball_list = [ball1, ball2] # список шариков
blrd.moving(ball_list, dt=0.03, n=60400, cm=[[0, 1]]) # старт движения
на библиотеке billiard4 которую я закреплю вместе с sample2
import random as rnd
import billiard4 as blrd
# ============== constants ====================
blrd.size = size = (800, 600) # size of the field for billiard
blrd.r_max = r_max = 20 # maximal radius of the balls
blrd.v_max = v_max = 8. # maximal speed of the balls
blrd.m_max = 15. # maxima mass of the balls
# ======== construction of configuration =====
ball_list = [blrd.ball( x = 100, y = 100, v_x = -45, v_y = -45, r = 45, m = 3),
blrd.ball( x = 200, y = 150, v_x = 45, v_y = 45, r = -45, m = 3.)
] # end of the list of balls
blrd.moving(ball_list, dt = 0.03, n = 60400, cm = [[0,1]]) # start the moving