嗨,我在 ruby 中有以下球对球碰撞检测的实现,它适用于大多数碰撞。当球在某些角度相互撞击时,怎么会有一些缺陷。
如果您需要更多信息告诉我,我已将我的实现放在这里。但我想知道更多笼统的说法。是什么导致球在某些冲击角度下相互旋转。
def ball_collider! ball
for ball2 in @balls do
next if ball.object_id == ball2.object_id
next unless box_overlap ball2.boundbox, ball.boundbox
next unless ball_overlap ball, ball2
dx = ball2.x - ball.x
dy = ball2.y - ball.y
dist=Math.sqrt(dx**2 + dy**2)
bonuce_point_x = ball2.x - (ball.radii + ball2.radii) * dx / dist
bonuce_point_y = ball2.y - (ball.radii + ball2.radii) * dy / dist
bounce_line = [[bonuce_point_x,bonuce_point_y],[bonuce_point_x-dy,bonuce_point_y+dx]]
ball2.bounce! bounce_line
ball.bounce! bounce_line
motion_left = ball.unmove! bounce_line, true
ball_controller! ball if motion_left > 0.1
end
end
def box_overlap box1, box2
return (box1[:width] + box2[:width] > (box1[:x] - box2[:x]).abs) && (box1[:width] + box2[:width] > (box1[:y] - box2[:y]).abs)
end
def ball_overlap ball1, ball2
dx = ball2.x - ball1.x
dy = ball2.y - ball1.y
return (dx**2 + dy**2) < (ball1.radii+ball2.radii)**2
end