From f5df91170727fa8695f196310e22270b1b2bd0a8 Mon Sep 17 00:00:00 2001 From: Donald Ball Date: Mon, 17 Nov 2025 20:21:42 -0500 Subject: [PATCH] fix atan issue the love2d that I'm using doesn't have the new atan arity lol --- asteroids/main.lua | 39 +++++++++++++++++++++------------------ asteroids/phys.lua | 6 ++---- asteroids/test_phys.lua | 18 +++++++++++++++++- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/asteroids/main.lua b/asteroids/main.lua index 0bc5df9..cb45bbf 100644 --- a/asteroids/main.lua +++ b/asteroids/main.lua @@ -1,5 +1,6 @@ local love = require("love") local phys = require("phys") +local inspect = require("inspect") local function initGame() return { @@ -51,7 +52,7 @@ local function initState() } } for _, asteroid in ipairs(asteroids) do - asteroid.angle = love.math.random() * (2 * math.pi) + asteroid.angle = love.math.random() * phys.tau asteroid.stage = #game.asteroids.stages local stage = game.asteroids.stages[asteroid.stage] asteroid.speed = stage.speed @@ -65,11 +66,9 @@ local function initState() ship = { x = game.width / 2, y = game.height / 2, - angle = 3 * math.pi / 2, - speed = { - x = 0, - y = 0, - }, + angle = 0, + facing = phys.tau * 3 / 4, + speed = 0, radius = game.ship.radius, }, bullets = {}, @@ -100,14 +99,14 @@ end function love.update(dt) if state.controls.joystick ~= nil then local thrust = state.controls.joystick:getGamepadAxis('triggerright') * game.controls.ship.maxThrust - state.ship.speed.x = state.ship.speed.x + math.cos(state.ship.angle) * thrust * dt - state.ship.speed.y = state.ship.speed.y + math.sin(state.ship.angle) * thrust * dt + if thrust > 0 then + phys.apply(state.ship, { speed = thrust * dt, angle = state.ship.facing }) + end local spin = state.controls.joystick:getGamepadAxis('leftx') * game.controls.ship.turnRate - state.ship.angle = (state.ship.angle + spin * dt) % (2 * math.pi) + state.ship.facing = (state.ship.facing + spin * dt) % phys.tau end - state.ship.x = (state.ship.x + state.ship.speed.x * dt) % game.width - state.ship.y = (state.ship.y + state.ship.speed.y * dt) % game.height + update(state.ship, dt) for bulletIndex = #state.bullets, 1, -1 do local bullet = state.bullets[bulletIndex] @@ -159,7 +158,7 @@ function love.update(dt) end if #state.asteroids == 0 then - reset() + --reset() end end @@ -169,10 +168,11 @@ function love.gamepadpressed(joystick, button) end if button == "b" then -- debounce, perhaps + -- include ship velocity, perhaps local bullet = { - x = state.ship.x + math.cos(state.ship.angle) * game.ship.radius, - y = state.ship.y + math.sin(state.ship.angle) * game.ship.radius, - angle = state.ship.angle, + x = state.ship.x + math.cos(state.ship.facing) * game.ship.radius, + y = state.ship.y + math.sin(state.ship.facing) * game.ship.radius, + angle = state.ship.facing, speed = game.ship.bullet.speed, radius = game.ship.bullet.radius, timeLeft = game.ship.bullet.time, @@ -180,8 +180,11 @@ 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 + --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 + end + if button == "x" then + print(inspect(state.ship)) end end @@ -195,7 +198,7 @@ function love.draw() love.graphics.draw(game.ship.sprite, state.ship.x, state.ship.y, - (state.ship.angle + math.pi / 2) % (math.pi * 2), + (state.ship.facing + math.pi / 2) % (math.pi * 2), scale, scale, game.ship.sprite:getWidth() / 2, diff --git a/asteroids/phys.lua b/asteroids/phys.lua index e337f8a..b850788 100644 --- a/asteroids/phys.lua +++ b/asteroids/phys.lua @@ -19,12 +19,10 @@ function phys.update(actor, dt) end function phys.rotate(actor, angle) - actor.angle = (actor.angle + angle) % phys.tau + actor.facing = (actor.facing + angle) % phys.tau end function phys.apply(actor, force) - assert(actor.speed >= 0) - assert(force.speed >= 0) local ax = math.cos(actor.angle) * actor.speed local ay = math.sin(actor.angle) * actor.speed local fx = math.cos(force.angle) * force.speed @@ -33,7 +31,7 @@ function phys.apply(actor, force) local y = ay + fy local speed = math.sqrt(x ^ 2 + y ^ 2) - local angle = math.atan(y, x) % phys.tau + local angle = math.atan2(y, x) % phys.tau actor.speed = speed actor.angle = angle end diff --git a/asteroids/test_phys.lua b/asteroids/test_phys.lua index 7909584..41891a2 100644 --- a/asteroids/test_phys.lua +++ b/asteroids/test_phys.lua @@ -26,7 +26,23 @@ function TestApply() phys.apply(actor, { speed = 1, angle = math.pi / 2 }) t.assertAlmostEquals( actor, - { x = 0, y = 0, speed = math.sqrt(2), angle = math.pi * 3 / 4 } + { x = 0, y = 0, speed = math.sqrt(2), angle = math.pi * 3 / 4 }) + + actor = { x = 0, y = 0, speed = 0, angle = 0 } + phys.apply(actor, { speed = 1, angle = math.pi / 2 }) + t.assertAlmostEquals( + actor, + { x = 0, y = 0, speed = 1, angle = math.pi / 2 }) + + phys.apply(actor, { speed = 1, angle = math.pi / 2 }) + t.assertAlmostEquals( + actor, + { x = 0, y = 0, speed = 2, angle = math.pi / 2 }) + + phys.apply(actor, { speed = 1, angle = math.pi }) + t.assertAlmostEquals( + actor, + { x = 0, y = 0, speed = 0, angle = 0 } ) end