gut trig and use vector3 lib in game script
This commit is contained in:
parent
034cdcce80
commit
02c4525b87
@ -1,22 +1,13 @@
|
|||||||
-- called every frame, with constant delta time
|
-- called every frame, with constant delta time
|
||||||
local capture = false
|
local capture = false
|
||||||
local trig = require "trig"
|
local Vector3 = require "vector3"
|
||||||
local Vector3l = require "vector3"
|
|
||||||
|
|
||||||
|
|
||||||
local function Vector3(x, y, z)
|
|
||||||
if y == nil then
|
|
||||||
return {x = x, y = x, z = x}
|
|
||||||
end
|
|
||||||
return {x = x, y = y, z = z}
|
|
||||||
end
|
|
||||||
|
|
||||||
local rot = 0
|
local rot = 0
|
||||||
local rot_spd = 0.05
|
local rot_spd = 0.05
|
||||||
|
|
||||||
local spd = 0.07
|
local spd = 0.07
|
||||||
|
|
||||||
local pos = Vector3(0)
|
local pos = Vector3()
|
||||||
|
|
||||||
local function Vector2(x, y)
|
local function Vector2(x, y)
|
||||||
if y == nil then
|
if y == nil then
|
||||||
@ -48,6 +39,7 @@ function game_tick()
|
|||||||
if ctx.initialization_needed then
|
if ctx.initialization_needed then
|
||||||
-- ctx.udata persists on reload
|
-- ctx.udata persists on reload
|
||||||
ctx.udata = {}
|
ctx.udata = {}
|
||||||
|
print(Vector3(5, 0, 0))
|
||||||
end
|
end
|
||||||
ctx.mouse_capture = capture
|
ctx.mouse_capture = capture
|
||||||
input_action{name="quit", control="SPACE"}
|
input_action{name="quit", control="SPACE"}
|
||||||
@ -64,15 +56,16 @@ function game_tick()
|
|||||||
capture = false
|
capture = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local dir = draw_camera_from_principal_axes{position = pos, yaw = rot}.direction
|
local dir = Vector3(draw_camera_from_principal_axes{position = pos, yaw = rot}.direction)
|
||||||
dir.y = 0
|
dir.y = 0
|
||||||
dir = trig.v3_normalized(dir)
|
dir = dir:normalized()
|
||||||
|
|
||||||
local rot_d = b2n(input_action_pressed{name = "right"}) - b2n(input_action_pressed{name = "left"})
|
local rot_d = b2n(input_action_pressed{name = "right"}) - b2n(input_action_pressed{name = "left"})
|
||||||
rot = rot + rot_d * rot_spd
|
rot = rot + rot_d * rot_spd
|
||||||
|
|
||||||
local mov_d = b2n(input_action_pressed{name = "forward"}) - b2n(input_action_pressed{name = "back"})
|
local mov_d = b2n(input_action_pressed{name = "forward"}) - b2n(input_action_pressed{name = "back"})
|
||||||
pos = trig.v3_add(pos, trig.v3_mult(dir, spd * mov_d))
|
-- pos = trig.v3_add(pos, trig.v3_mult(dir, spd * mov_d))
|
||||||
|
pos = pos + dir * spd * mov_d
|
||||||
|
|
||||||
draw_billboard{position = Vector3(5, 0, 0), size = Vector2(1, 1), texture = "images/duck.png"}
|
draw_billboard{position = Vector3(5, 0, 0), size = Vector2(1, 1), texture = "images/duck.png"}
|
||||||
end
|
end
|
||||||
|
@ -1,38 +1,3 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.v3_length_squared(v)
|
|
||||||
local x2 = v.x * v.x
|
|
||||||
local y2 = v.y * v.y
|
|
||||||
local z2 = v.z * v.z
|
|
||||||
|
|
||||||
return x2 + y2 + z2
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.v3_normalized(v)
|
|
||||||
local length = math.sqrt(M.v3_length_squared(v))
|
|
||||||
local new_vec = {
|
|
||||||
x = v.x / length,
|
|
||||||
y = v.y / length,
|
|
||||||
z = v.z / length,
|
|
||||||
}
|
|
||||||
|
|
||||||
return new_vec
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.v3_mult(v, f)
|
|
||||||
return {
|
|
||||||
x = v.x * f,
|
|
||||||
y = v.y * f,
|
|
||||||
z = v.z * f,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function M.v3_add(v1, v2)
|
|
||||||
return {
|
|
||||||
x = v1.x + v2.x,
|
|
||||||
y = v1.y + v2.y,
|
|
||||||
z = v1.z + v2.z,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
return M
|
return M
|
Reference in New Issue
Block a user