From 5bcf22a6f8dce820f0d978703ae6bc74a75b233f Mon Sep 17 00:00:00 2001 From: Donald Ball Date: Mon, 17 Nov 2025 20:31:14 -0500 Subject: [PATCH] bullet recoil, use tau --- asteroids/main.lua | 15 +++++++-------- asteroids/phys.lua | 6 +----- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/asteroids/main.lua b/asteroids/main.lua index cb45bbf..1f50818 100644 --- a/asteroids/main.lua +++ b/asteroids/main.lua @@ -10,7 +10,7 @@ local function initGame() radius = 30, bullet = { radius = 5, - recoil = -10, + recoil = 10, speed = 500, time = 4, sound = love.sound.newSoundData('resources/bullet.mp3'), @@ -104,7 +104,7 @@ function love.update(dt) end 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 update(state.ship, dt) @@ -121,8 +121,8 @@ function love.update(dt) if phys.intersects(bullet, asteroid) then table.remove(state.bullets, bulletIndex) if asteroid.stage > 1 then - local angle1 = love.math.random() * (2 * math.pi) - local angle2 = (angle1 - math.pi) % (2 * math.pi) + local angle1 = love.math.random() * phys.tau + local angle2 = angle1 - phys.tau / 2 local radius = game.asteroids.stages[asteroid.stage - 1].radius local speed = game.asteroids.stages[asteroid.stage - 1].speed table.insert(state.asteroids, { @@ -158,7 +158,7 @@ function love.update(dt) end if #state.asteroids == 0 then - --reset() + reset() end end @@ -180,8 +180,7 @@ function love.gamepadpressed(joystick, button) } table.insert(state.bullets, bullet) bullet.sound:play() - --state.ship.speed.x = state.ship.speed.x + math.cos(state.ship.angle) * game.ship.bullet.recoil - --state.ship.speed.y = state.ship.speed.y + math.sin(state.ship.angle) * game.ship.bullet.recoil + phys.apply(state.ship, { speed = game.ship.bullet.recoil, angle = state.ship.facing + phys.tau / 2 }) end if button == "x" then print(inspect(state.ship)) @@ -198,7 +197,7 @@ function love.draw() love.graphics.draw(game.ship.sprite, state.ship.x, state.ship.y, - (state.ship.facing + math.pi / 2) % (math.pi * 2), + (state.ship.facing + phys.tau / 4) % phys.tau, scale, scale, game.ship.sprite:getWidth() / 2, diff --git a/asteroids/phys.lua b/asteroids/phys.lua index b850788..3772423 100644 --- a/asteroids/phys.lua +++ b/asteroids/phys.lua @@ -18,10 +18,6 @@ function phys.update(actor, dt) actor.y = actor.y + math.sin(actor.angle) * dv end -function phys.rotate(actor, angle) - actor.facing = (actor.facing + angle) % phys.tau -end - function phys.apply(actor, force) local ax = math.cos(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 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.angle = angle end