Refactor collision detection

main
Tyler Scott 2022-02-06 08:03:57 +07:00
parent 860ed47d63
commit 8ee2233d58
1 changed files with 13 additions and 17 deletions

View File

@ -201,20 +201,15 @@ function love.update(dt)
if game.next_bubble.velocity_x ~= 0 or game.next_bubble.velocity_y ~= 0 then if game.next_bubble.velocity_x ~= 0 or game.next_bubble.velocity_y ~= 0 then
local next_x = game.next_bubble.x + game.next_bubble.velocity_x local next_x = game.next_bubble.x + game.next_bubble.velocity_x
local next_y = game.next_bubble.y + game.next_bubble.velocity_y local next_y = game.next_bubble.y + game.next_bubble.velocity_y
local collision_detection_finished = false local bubble_should_stop = false
-- collision with ceiling -- collision with ceiling
if next_y - game.bubble_radius <= game.level_top then if next_y - game.bubble_radius <= game.level_top then
local nearest_slot_index = find_nearest_slot(next_x, next_y) bubble_should_stop = true
game.next_bubble.x = game.bubble_slots[nearest_slot_index].x
game.next_bubble.y = game.bubble_slots[nearest_slot_index].y
game.next_bubble.velocity_x = 0
game.next_bubble.velocity_y = 0
collision_detection_finished = true
end end
-- collision with walls -- collision with walls
if not collision_detection_finished then if not bubble_should_stop then
if next_x + game.bubble_radius >= game.level_right or if next_x + game.bubble_radius >= game.level_right or
next_x - game.bubble_radius <= game.level_left then next_x - game.bubble_radius <= game.level_left then
local impact_x = game.level_left + game.bubble_radius local impact_x = game.level_left + game.bubble_radius
@ -233,7 +228,7 @@ function love.update(dt)
end end
-- collision with another bubble -- collision with another bubble
if not collision_detection_finished then if not bubble_should_stop then
for i = 1, #game.bubble_slots do for i = 1, #game.bubble_slots do
local slot = game.bubble_slots[i] local slot = game.bubble_slots[i]
if slot.bubble_type ~= 0 then if slot.bubble_type ~= 0 then
@ -242,24 +237,25 @@ function love.update(dt)
local dist = math.sqrt(diff_x * diff_x + diff_y * diff_y) local dist = math.sqrt(diff_x * diff_x + diff_y * diff_y)
if dist <= game.bubble_diameter then if dist <= game.bubble_diameter then
local depth = game.bubble_diameter - dist local depth = game.bubble_diameter - dist
print('depth', depth)
local direction_x = diff_x / dist local direction_x = diff_x / dist
local direction_y = diff_y / dist local direction_y = diff_y / dist
next_x = next_x + direction_x * depth next_x = next_x + direction_x * depth
next_y = next_y + direction_y * depth next_y = next_y + direction_y * depth
local nearest_slot_index = find_nearest_slot(next_x, next_y) bubble_should_stop = true
game.next_bubble.x = game.bubble_slots[nearest_slot_index].x
game.next_bubble.y = game.bubble_slots[nearest_slot_index].y
game.next_bubble.velocity_x = 0
game.next_bubble.velocity_y = 0
collision_detection_finished = true
break break
end end
end end
end end
end end
if not collision_detection_finished then if bubble_should_stop then
local nearest_slot_index = find_nearest_slot(next_x, next_y)
game.bubble_slots[nearest_slot_index].bubble_type = game.next_bubble.bubble_type
game.next_bubble.x = game.bubble_slots[nearest_slot_index].x
game.next_bubble.y = game.bubble_slots[nearest_slot_index].y
game.next_bubble.velocity_x = 0
game.next_bubble.velocity_y = 0
else
game.next_bubble.x = next_x game.next_bubble.x = next_x
game.next_bubble.y = next_y game.next_bubble.y = next_y
end end