fix atan issue
the love2d that I'm using doesn't have the new atan arity lol
This commit is contained in:
parent
9cdf032c39
commit
f5df911707
3 changed files with 40 additions and 23 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue