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