asteroid collision

This commit is contained in:
Donald Ball 2025-11-14 16:41:04 -05:00
parent 3ba30a2fb1
commit 3502dfbdd7

View file

@ -31,6 +31,7 @@ function love.load()
for asteroidIndex, asteroid in ipairs(asteroids) do for asteroidIndex, asteroid in ipairs(asteroids) do
asteroid.angle = love.math.random() * (2 * math.pi) asteroid.angle = love.math.random() * (2 * math.pi)
end end
asteroidRadius = 80
end end
function love.update(dt) function love.update(dt)
@ -77,10 +78,19 @@ function love.update(dt)
end end
end end
local function areCirclesIntersecting(aX, aY, aRadius, bX, bY, bRadius)
return (aX - bX) ^ 2 + (aY - bY) ^ 2 <= (aRadius + bRadius) ^ 2
end
for asteroidIndex, asteroid in ipairs(asteroids) do for asteroidIndex, asteroid in ipairs(asteroids) do
local asteroidSpeed = 20 local asteroidSpeed = 20
asteroid.x = (asteroid.x + math.cos(asteroid.angle) * asteroidSpeed * dt) % arenaWidth asteroid.x = (asteroid.x + math.cos(asteroid.angle) * asteroidSpeed * dt) % arenaWidth
asteroid.y = (asteroid.y + math.cos(asteroid.angle) * asteroidSpeed * dt) % arenaHeight asteroid.y = (asteroid.y + math.sin(asteroid.angle) * asteroidSpeed * dt) % arenaHeight
if areCirclesIntersecting(shipX, shipY, shipRadius, asteroid.x, asteroid.y, asteroidRadius) then
love.load()
break
end
end end
end end
@ -107,7 +117,7 @@ function love.draw()
for asteroidIndex, asteroid in ipairs(asteroids) do for asteroidIndex, asteroid in ipairs(asteroids) do
love.graphics.setColor(1, 1, 0) love.graphics.setColor(1, 1, 0)
love.graphics.circle('fill', asteroid.x, asteroid.y, 80) love.graphics.circle('fill', asteroid.x, asteroid.y, asteroidRadius)
end end
end end
end end
@ -118,5 +128,6 @@ function love.draw()
'shipAngle:' .. shipAngle, 'shipAngle:' .. shipAngle,
'shipX: ' .. shipX, 'shipX: ' .. shipX,
'shipSpeedX: ' .. shipSpeedX, 'shipSpeedX: ' .. shipSpeedX,
'a1: ' .. asteroids[1].angle
}, '\n')) }, '\n'))
end end