bullet recoil, use tau

This commit is contained in:
Donald Ball 2025-11-17 20:31:14 -05:00
parent f5df911707
commit 5bcf22a6f8
2 changed files with 8 additions and 13 deletions

View file

@ -10,7 +10,7 @@ local function initGame()
radius = 30, radius = 30,
bullet = { bullet = {
radius = 5, radius = 5,
recoil = -10, recoil = 10,
speed = 500, speed = 500,
time = 4, time = 4,
sound = love.sound.newSoundData('resources/bullet.mp3'), sound = love.sound.newSoundData('resources/bullet.mp3'),
@ -104,7 +104,7 @@ function love.update(dt)
end end
local spin = state.controls.joystick:getGamepadAxis('leftx') * game.controls.ship.turnRate local spin = state.controls.joystick:getGamepadAxis('leftx') * game.controls.ship.turnRate
state.ship.facing = (state.ship.facing + spin * dt) % phys.tau state.ship.facing = (state.ship.facing + spin * dt)
end end
update(state.ship, dt) update(state.ship, dt)
@ -121,8 +121,8 @@ function love.update(dt)
if phys.intersects(bullet, asteroid) then if phys.intersects(bullet, asteroid) then
table.remove(state.bullets, bulletIndex) table.remove(state.bullets, bulletIndex)
if asteroid.stage > 1 then if asteroid.stage > 1 then
local angle1 = love.math.random() * (2 * math.pi) local angle1 = love.math.random() * phys.tau
local angle2 = (angle1 - math.pi) % (2 * math.pi) local angle2 = angle1 - phys.tau / 2
local radius = game.asteroids.stages[asteroid.stage - 1].radius local radius = game.asteroids.stages[asteroid.stage - 1].radius
local speed = game.asteroids.stages[asteroid.stage - 1].speed local speed = game.asteroids.stages[asteroid.stage - 1].speed
table.insert(state.asteroids, { table.insert(state.asteroids, {
@ -158,7 +158,7 @@ function love.update(dt)
end end
if #state.asteroids == 0 then if #state.asteroids == 0 then
--reset() reset()
end end
end end
@ -180,8 +180,7 @@ function love.gamepadpressed(joystick, button)
} }
table.insert(state.bullets, bullet) table.insert(state.bullets, bullet)
bullet.sound:play() bullet.sound:play()
--state.ship.speed.x = state.ship.speed.x + math.cos(state.ship.angle) * game.ship.bullet.recoil phys.apply(state.ship, { speed = game.ship.bullet.recoil, angle = state.ship.facing + phys.tau / 2 })
--state.ship.speed.y = state.ship.speed.y + math.sin(state.ship.angle) * game.ship.bullet.recoil
end end
if button == "x" then if button == "x" then
print(inspect(state.ship)) print(inspect(state.ship))
@ -198,7 +197,7 @@ function love.draw()
love.graphics.draw(game.ship.sprite, love.graphics.draw(game.ship.sprite,
state.ship.x, state.ship.x,
state.ship.y, state.ship.y,
(state.ship.facing + math.pi / 2) % (math.pi * 2), (state.ship.facing + phys.tau / 4) % phys.tau,
scale, scale,
scale, scale,
game.ship.sprite:getWidth() / 2, game.ship.sprite:getWidth() / 2,

View file

@ -18,10 +18,6 @@ function phys.update(actor, dt)
actor.y = actor.y + math.sin(actor.angle) * dv actor.y = actor.y + math.sin(actor.angle) * dv
end end
function phys.rotate(actor, angle)
actor.facing = (actor.facing + angle) % phys.tau
end
function phys.apply(actor, force) function phys.apply(actor, force)
local ax = math.cos(actor.angle) * actor.speed local ax = math.cos(actor.angle) * actor.speed
local ay = math.sin(actor.angle) * actor.speed local ay = math.sin(actor.angle) * actor.speed
@ -31,7 +27,7 @@ function phys.apply(actor, force)
local y = ay + fy local y = ay + fy
local speed = math.sqrt(x ^ 2 + y ^ 2) local speed = math.sqrt(x ^ 2 + y ^ 2)
local angle = math.atan2(y, x) % phys.tau local angle = math.atan2(y, x)
actor.speed = speed actor.speed = speed
actor.angle = angle actor.angle = angle
end end