Compare commits

...

6 Commits

Author SHA1 Message Date
208204ae8a test bump-3dpd lib 2025-03-03 22:29:51 +03:00
85df2fb243 add ipairs and numeric indexing support to vector3 2025-03-03 13:41:38 +03:00
b25e41bd49 not working FUCK YOU MATH 2025-03-03 02:05:43 +03:00
91c2e1bce1 new aabb constructor 2025-03-03 00:44:54 +03:00
1433aa8e40 aabb drawing for debug 2025-03-03 00:30:34 +03:00
e3f195cf71 dang 2025-03-02 23:44:58 +03:00
6 changed files with 1212 additions and 8 deletions

View File

@ -13,9 +13,20 @@ local Player = {
accel = 0.3,
world = nil,
ThrowPressed = Signal.new(),
}
local CAMERA_OFFSET = Vector3(0, -1, 0)
function Player:init(world)
local x,y,z = ((self.position - Vector3(0.25, 1.0, 0.25)) * UNIT_SIZE):decomposed()
local w,h,d = (Vector3(0.25, 1.1, 0.25) * UNIT_SIZE):decomposed()
world:add(self, x,y,z,w,h,d)
self.world = world
end
function Player:tick(ctx)
input_action{name = "left", control = "A"}
input_action{name = "right", control = "D"}
@ -34,17 +45,22 @@ function Player:tick(ctx)
local direction = ((camera_forward * forward_input) + (camera_right * strafe_input)):normalized()
local target_vel = direction * self.speed
self.velocity = self.velocity:lerp(target_vel, self.accel)
if input_action_just_pressed{name = "throw"} then
self.ThrowPressed:emit(self.position:copy(), camera_forward:copy())
end
self.velocity = self.velocity:lerp(target_vel, self.accel)
local goal = ((self.position + CAMERA_OFFSET) + self.velocity) * UNIT_SIZE
local actual_x, actual_y, actual_z, cols, len = self.world:move(self, goal.x, goal.y, goal.z)
-- for i = 1, len do
-- print(util.printt(cols[i].other))
-- end
self.position = Vector3(actual_x / UNIT_SIZE, actual_y / UNIT_SIZE, actual_z / UNIT_SIZE) - CAMERA_OFFSET
if ctx.mouse_capture then
self.yaw = self.yaw + self.mouse_sensitivity * ctx.mouse_movement.x
end
self.position = self.position + self.velocity
draw_text{font = "fonts/Lunchtype21_Regular.ttf", position = {x = 0, y = 0}, string = table.concat(table.pack(self.world:getCube(self)), ";"), height = 14}
draw_text{font = "fonts/Lunchtype21_Regular.ttf", position = {x = 0, y = 14}, string = tostring(self.position), height = 14}
end
return Player

View File

@ -1,7 +1,12 @@
util = require "util"
UNIT_SIZE = 1
local player = require "classes.player"
local Vector3 = require "types.vector3"
local List = require "types.list"
local bump = require "lib.bump-3dpd"
local AABB = require "types.aabb"
local world = bump.newWorld()
local Obj = require "classes.obj"
@ -40,11 +45,19 @@ local function duck_seek_feed(duck)
)
end
local test_aabb = AABB.new(Vector3(0, 0, 2), Vector3(1, 1, 1))
-- called every frame, with constant delta time
function game_tick()
-- ctx.initialization_needed is true first frame and every time dynamic reload is performed
if ctx.initialization_needed then
-- audio_play{audio = "music/bg1.xm", loops = true, channel = "music"}
player:init(world)
local x,y,z = (Vector3(0, 0, 2) * UNIT_SIZE):decomposed()
local w,h,d = (Vector3(1, 1, 1) * UNIT_SIZE):decomposed()
world:add(test_aabb, x,y,z,w,h,d)
print(world:getCube(test_aabb))
print(world:getCube(player))
-- audio_play{audio = "music/bg1.xm", loops = true, channel = "music"}
player.ThrowPressed:connect(create_feed)
-- spawn some ducks
@ -56,7 +69,6 @@ function game_tick()
duck.index = i
end
end
-- ctx.udata persists on reload
if ctx.udata == nil then
ctx.udata = {
@ -85,10 +97,12 @@ function game_tick()
texture = "images/measure001a.png",
texture_region = { x = 0, y = 0, w = 512, h = 512 },
}
draw_quad(util.merge(q, params))
-- draw_quad(util.merge(q, params))
cube.position.y = math.sin(ctx.frame_number * 0.05)
-- cube.position.z = math.cos(ctx.frame_number * 0.01)
cube.rotation.x = cube.rotation.x + 0.01
cube.rotation.z = cube.rotation.z + 0.01
cube:draw()
test_aabb:draw()
end

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,85 @@
local Vector3 = require "types.vector3"
---@class AABB
local AABB = {
min = Vector3(),
size = Vector3(),
}
local RED = {
r = 255,
g = 0,
b = 0,
a = 255,
}
setmetatable(AABB, AABB)
AABB.__index = AABB
---@param position Vector3
---@param size Vector3
---@return AABB
function AABB.new(position, size)
position = position or Vector3()
size = size or Vector3(1, 1, 1)
local aabb = {
min = position,
size = size,
}
return setmetatable(aabb, AABB)
end
function AABB:get_max()
return self.min + self.size
end
function AABB:draw()
local max = self:get_max()
-- bottom rectangle
draw_line_3d{start = self.min, finish = Vector3(max.x, self.min.y, self.min.z)}
draw_line_3d{start = self.min, finish = Vector3(self.min.x, self.min.y, max.z)}
draw_line_3d{start = Vector3(max.x, self.min.y, max.z), finish = Vector3(self.min.x, self.min.y, max.z)}
draw_line_3d{start = Vector3(max.x, self.min.y, max.z), finish = Vector3(max.x, self.min.y, self.min.z)}
-- bottom rectangle diagonal
draw_line_3d{start = self.min, finish = Vector3(max.x, self.min.y, max.z), color = RED}
-- top rectangle
draw_line_3d{start = Vector3(self.min.x, max.y, self.min.z), finish = Vector3(max.x, max.y, self.min.z)}
draw_line_3d{start = Vector3(self.min.x, max.y, self.min.z), finish = Vector3(self.min.x, max.y, max.z)}
draw_line_3d{start = Vector3(max.x, max.y, max.z), finish = Vector3(self.min.x, max.y, max.z)}
draw_line_3d{start = Vector3(max.x, max.y, max.z), finish = Vector3(max.x, max.y, self.min.z)}
-- top rectangle diagonal
draw_line_3d{start = Vector3(max.x, max.y, max.z), finish = Vector3(self.min.x, max.y, self.min.z), color = RED}
-- hull
draw_line_3d{start = self.min, finish = Vector3(self.min.x, max.y, self.min.z)}
draw_line_3d{start = self.min, finish = Vector3(max.x, max.y, self.min.z), color = RED}
draw_line_3d{start = Vector3(max.x, self.min.y, self.min.z), finish = Vector3(max.x, max.y, self.min.z)}
draw_line_3d{start = Vector3(max.x, self.min.y, self.min.z), finish = Vector3(max.x, max.y, max.z), color = RED}
draw_line_3d{start = Vector3(max.x, self.min.y, max.z), finish = Vector3(max.x, max.y, max.z)}
draw_line_3d{start = Vector3(max.x, self.min.y, max.z), finish = Vector3(self.min.x, max.y, max.z), color = RED}
draw_line_3d{start = Vector3(self.min.x, self.min.y, max.z), finish = Vector3(self.min.x, max.y, max.z)}
draw_line_3d{start = Vector3(self.min.x, self.min.y, max.z), finish = Vector3(self.min.x, max.y, self.min.z), color = RED}
end
---returns true if the point is inside this aabb
---@param point Vector3
---@return boolean
function AABB:has_point(point)
local max = self:get_max()
return point > self.min and point < max
end
---returns true if `other` intersects this AABB
---@param other AABB
---@return boolean
function AABB:intersects(other)
local my_max = self:get_max()
local other_max = other:get_max()
return self.min <= other_max and my_max >= other.min
end
return AABB

View File

@ -155,8 +155,39 @@ function Vector3:__tostring()
return "Vector3(" .. tostring(self.x) .. ", " .. tostring(self.y) .. ", " .. tostring(self.z) .. ")"
end
Vector3.__index = Vector3
-- note: the < and <= operators of this class are component-wise rather than lexicographic (that is, htey are useful for bounds checking but are not suitable for sorting.)
function Vector3:__lt(b)
local err, other = coerce(b, true)
if err then return nil end
return self.x < other.x and self.y < other.y and self.z < other.z
end
function Vector3:__le(b)
local err, other = coerce(b, true)
if err then return nil end
return self.x <= other.x and self.y <= other.y and self.z <= other.z
end
local NUMK = {"x", "y", "z"}
function Vector3:__index(key)
-- this allows constructs like `for i, v in ipairs(Vector3(3, 2, 1))` to iterate over components
if type(key) == "number" then
return rawget(self, NUMK[key])
end
return rawget(Vector3, key)
end
function Vector3:__newindex(key, value)
if NUMK[key] then
rawset(self, NUMK[key], value)
return
end
rawset(self, key, value)
end
--------API--------
function Vector3:length_squared()
@ -246,6 +277,24 @@ function Vector3:rotate(axis, angle)
return self
end
---In place set.
---@param x number
---@param y number
---@param z number
---@return Vector3
function Vector3:set(x, y, z)
self.x, self.y, self.z = x, y, z
return self
end
---In place set.
---@param t vectorlike
---@return Vector3
function Vector3:sett(t)
self.x, self.y, self.z = t.x, t.y, t.z
return self
end
---Returns a copy of this vector.
---@return Vector3
function Vector3:copy()
@ -307,6 +356,15 @@ end
function Vector3:horizontal()
return Vector3(self.x, 0.0, self.z)
end
---Returns the components of the vector individually.
---@return number
---@return number
---@return number
function Vector3:decomposed()
return self.x, self.y, self.z
end
---- CONSTANTS
---@type Vector3

21
data/scripts/v3test.lua Normal file
View File

@ -0,0 +1,21 @@
local Vector3 = require "types.vector3"
local function pf(x, y, z)
print(x, y, z)
end
local v1 = Vector3(2, 1, 1)
local v2 = Vector3(3, 4, 4)
pf(v1:decomposed())
-- print(v1)
-- print(v2)
-- v1[1] = 383838
-- v1.y = 8858
-- print(v1)
-- print(v1:normalized())
-- print(v2)
-- for i, v in ipairs(v2) do
-- print(i, v)
-- end