• Решение задачи асболютно упругого соударения двух шаров?

    @PankovEA
    Спасибо. Код рабочий. Было бы интересно всё же понять что такое a, b, p1, p2 и p3.
    Переписал на пайтон. Это рабочий кусок программы, написанной, на основе модуля turtle:

    import turtle as t
    from random import randint
    
    class Ball(t.Turtle):
        def __init__(self, x=0, y=0, dx=0, dy=0):
            super().__init__()
            self.penup()
            if x or y:
                self.hideturtle()
                self.goto(x,y)
                self.showturtle()
            self.shape('circle')
            self.dx = dx
            self.dy = dy
            self.speed(0)
    
        def next_step(self):
            'Двигает шар на следующий шаг по dx, dy'
            x, y = self.position()
            if x + self.dx <= -150 or x + self.dx >= 150:
                self.dx = -self.dx
            if y + self.dy <= -150 or y + self.dy >= 150:
                self.dy = -self.dy
            x += self.dx
            y += self.dy
            self.goto(x, y)
    
    class Balls():
        def __init__(self, n):
            # Создание шаров из класса Ball()
            self.list = [Ball(randint(-150,150), randint(-150,150), randint(-5,5), randint(-5,5))
                            for i in range(int(n))]
    
        def find_collision(self):
            diametr = 20
            # Просмотрим все пары
            for i, ball1 in enumerate(self.list[:-1]):
                for ball2 in self.list[i+1:]:
                    x1, y1 = ball1.position()
                    x2, y2 = ball2.position()
                    dist = ((x1-x2)**2 + (y1-y2)**2)**0.5
                    if dist and dist < diametr:
                        a = x1 - x2 # вспомогательные переменные типа extended
                        b = y1 - y2
                        p1 = a*b / dist**2
                        p2 = (a/dist)**2
                        p3 = (b/dist)**2
                        d1 = ball1.dy * p1 + ball1.dx * p2 - ball2.dy * p1 - ball2.dx * p2
                        d2 = ball1.dx * p1 + ball1.dy * p3 - ball2.dx * p1 - ball2.dy * p3
                        ball1.dx -= d1 # меняем значение приращения координаты шаров при движении
                        ball1.dy -= d2
                        ball2.dx += d1
                        ball2.dy += d2
                        #при соударении шары всегда "проникают" друг в друга, поэтому раздвигаем их
                        p3 = (diametr - dist)/2
                        p1 = p3 * (a/dist)
                        p2 = p3 * (b/dist)
                        ball1.goto(x1 + p1, y1 + p1)
                        ball2.goto(x2 - p1, y2 - p1)
    
    balls = Balls(5) # Список шаров
    
    # Основной игровой цикл
    while True:
        for ball in balls.list:
            ball.next_step()
        balls.find_collision()