Я создаю игру, в которой шары отскакивают от внутри гораздо большего круга. Больший круг не двигается.
Вот код, который я сейчас использую для этих столкновений:
def collideCircle(circle, ball):
"""Check for collision between a ball and a circle"""
dx = circle.x - ball.x
dy = circle.y - ball.y
distance = math.hypot(dx, dy)
if distance >= circle.size + ball.size:
# We don't need to change anything about the circle, just the ball
tangent = math.atan2(dy, dx)
ball.angle = 2 * tangent - ball.angle
ball.speed *= elasticity + 0.251
angle = 0.5 * math.pi + tangent
ball.x -= math.sin(angle)
ball.y += math.cos(angle)
Он основан на замечательном учебнике Питера Коллингриджа здесь .
Круг и шар объекты являются обоими классами, с (x, y), радиусом, углом и скоростью.
Однако у меня есть две проблемы с этим методом:
Рассмотрев возможные решения уже здесь, в частности «обнаружение столкновений быстрого круга» [ссылка удалена из-за ограничения на спам-ссылки], который, хотя в Java используется один и тот же метод, все они имеют дело с внешними столкновениями, тогда как я смотрю на отскок мяча внутри круга.
Вот также определения класса для мяч () и круг ():
class Ball():
def __init__(self, (x,y), size):
"""Setting up the new instance"""
self.x = x
self.y = y
self.size = size
self.colour = (0,128,255)
self.thickness = 0
self.speed = 0.01
self.angle = math.pi/2
def display(self):
"""Draw the ball"""
pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)
def move(self):
"""Move the ball according to angle and speed"""
self.x += math.sin(self.angle) * self.speed
self.y -= math.cos(self.angle) * self.speed
(self.angle, self.speed) = addVectors((self.angle, self.speed), gravity)
self.speed *= drag
class Circle():
def __init__(self, (x,y), size):
"""Set up the new instance of the Circle class"""
self.x = x
self.y = y
self.size = size
self.colour = (236, 236, 236)
self.thickness = 0
self.angle = 0 # Needed for collision...
self.speed = 0 # detection against balls
def display(self):
"""Draw the circle"""
pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness
Заранее спасибо, Натан