1
喜球,我有以下执行球在红宝石球碰撞检测,其工作对大多数碰撞。当某些天使球碰到对方时,怎么会有一些缺陷。碰撞检测的实现是什么故障导致击中对方漩涡
我已经把我的执行到这里,如果你的信息告诉我需要更多。但林总体上想知道更多。什么导致球在某些天使的影响下围绕对方旋转。
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