Compare commits
No commits in common. "208204ae8ab6d250b21f25f3de7262f4045c2ab8" and "b25e41bd49a6b515f583c2e37827f8fad743c8eb" have entirely different histories.
208204ae8a
...
b25e41bd49
@ -1,5 +1,6 @@
|
|||||||
local Vector3 = require("types.vector3")
|
local Vector3 = require("types.vector3")
|
||||||
local Signal = require("types.signal")
|
local Signal = require("types.signal")
|
||||||
|
local AABB = require("types.aabb")
|
||||||
|
|
||||||
local Player = {
|
local Player = {
|
||||||
position = Vector3(0, 1, 0),
|
position = Vector3(0, 1, 0),
|
||||||
@ -12,19 +13,80 @@ local Player = {
|
|||||||
yaw_speed = 0.05,
|
yaw_speed = 0.05,
|
||||||
|
|
||||||
accel = 0.3,
|
accel = 0.3,
|
||||||
|
|
||||||
world = nil,
|
aabb = nil,
|
||||||
|
test_intersect = nil,
|
||||||
|
|
||||||
ThrowPressed = Signal.new(),
|
ThrowPressed = Signal.new(),
|
||||||
}
|
}
|
||||||
|
|
||||||
local CAMERA_OFFSET = Vector3(0, -1, 0)
|
local aabb_offset = Vector3(-0.25, -1, -0.25)
|
||||||
|
|
||||||
function Player:init(world)
|
---@param aabb AABB
|
||||||
local x,y,z = ((self.position - Vector3(0.25, 1.0, 0.25)) * UNIT_SIZE):decomposed()
|
---@param other AABB
|
||||||
local w,h,d = (Vector3(0.25, 1.1, 0.25) * UNIT_SIZE):decomposed()
|
---@param velocity Vector3
|
||||||
world:add(self, x,y,z,w,h,d)
|
---@return Vector3, Vector3
|
||||||
self.world = world
|
local function resolve_collision(aabb, other, velocity)
|
||||||
|
local my_min = aabb.min
|
||||||
|
local my_max = aabb:get_max()
|
||||||
|
local other_min = other.min
|
||||||
|
local other_max = other:get_max()
|
||||||
|
|
||||||
|
local overlap_x = math.min(my_max.x - other_min.x, other_max.x - my_min.x)
|
||||||
|
local overlap_y = math.min(my_max.y - other_min.y, other_max.y - my_min.y)
|
||||||
|
local overlap_z = math.min(my_max.z - other_min.z, other_max.z - my_min.z)
|
||||||
|
|
||||||
|
local min_overlap = math.min(overlap_x, overlap_y, overlap_z)
|
||||||
|
|
||||||
|
local new_pos = my_min:copy()
|
||||||
|
local new_vel = velocity:copy()
|
||||||
|
|
||||||
|
if min_overlap == overlap_x then
|
||||||
|
if velocity.x > 0 then
|
||||||
|
new_pos.x = other_min.x - (my_max.x - my_min.x)
|
||||||
|
elseif velocity.x < 0 then
|
||||||
|
new_pos.x = other_max.x
|
||||||
|
else
|
||||||
|
if my_min.x < other_min.x then
|
||||||
|
new_pos.x = other_min.x - (my_max.x - my_min.x)
|
||||||
|
else
|
||||||
|
new_pos.x = other_max.x
|
||||||
|
end
|
||||||
|
end
|
||||||
|
new_vel.x = 0
|
||||||
|
elseif min_overlap == overlap_y then
|
||||||
|
if velocity.y > 0 then
|
||||||
|
new_pos.y = other_min.y - (my_max.y - my_min.y)
|
||||||
|
elseif velocity.y < 0 then
|
||||||
|
new_pos.y = other_max.y
|
||||||
|
else
|
||||||
|
if my_min.y < other_min.y then
|
||||||
|
new_pos.y = other_min.y - (my_max.y - my_min.y)
|
||||||
|
else
|
||||||
|
new_pos.y = other_max.y
|
||||||
|
end
|
||||||
|
end
|
||||||
|
new_vel.y = 0
|
||||||
|
elseif min_overlap == overlap_z then
|
||||||
|
if velocity.z > 0 then
|
||||||
|
new_pos.z = other_min.z - (my_max.z - my_min.z)
|
||||||
|
elseif velocity.z < 0 then
|
||||||
|
new_pos.z = other_max.z
|
||||||
|
else
|
||||||
|
if my_min.z < other_min.z then
|
||||||
|
new_pos.z = other_min.z - (my_max.z - my_min.z)
|
||||||
|
else
|
||||||
|
new_pos.z = other_max.z
|
||||||
|
end
|
||||||
|
end
|
||||||
|
new_vel.z = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
return new_pos, new_vel
|
||||||
|
end
|
||||||
|
|
||||||
|
function Player:init()
|
||||||
|
self.aabb = AABB.new(Vector3(-0.25, 0, -0.25), Vector3(0.5, 1.1, 0.5))
|
||||||
end
|
end
|
||||||
|
|
||||||
function Player:tick(ctx)
|
function Player:tick(ctx)
|
||||||
@ -45,22 +107,39 @@ function Player:tick(ctx)
|
|||||||
|
|
||||||
local direction = ((camera_forward * forward_input) + (camera_right * strafe_input)):normalized()
|
local direction = ((camera_forward * forward_input) + (camera_right * strafe_input)):normalized()
|
||||||
local target_vel = direction * self.speed
|
local target_vel = direction * self.speed
|
||||||
|
-- self.velocity = self.velocity:lerp(target_vel, self.accel)
|
||||||
|
self.velocity = target_vel:copy()
|
||||||
|
self.position = self.position + self.velocity
|
||||||
|
if self.aabb:intersects(self.test_intersect) then
|
||||||
|
local new_pos, new_vel = resolve_collision(self.aabb, self.test_intersect, self.velocity)
|
||||||
|
self.position:set(
|
||||||
|
new_pos.x - aabb_offset.x,
|
||||||
|
new_pos.y - aabb_offset.y,
|
||||||
|
new_pos.z - aabb_offset.z
|
||||||
|
)
|
||||||
|
self.aabb.min:set(
|
||||||
|
self.position.x + aabb_offset.x,
|
||||||
|
self.position.y + aabb_offset.y,
|
||||||
|
self.position.z + aabb_offset.z
|
||||||
|
)
|
||||||
|
self.velocity:sett(new_vel)
|
||||||
|
end
|
||||||
if input_action_just_pressed{name = "throw"} then
|
if input_action_just_pressed{name = "throw"} then
|
||||||
self.ThrowPressed:emit(self.position:copy(), camera_forward:copy())
|
self.ThrowPressed:emit(self.position:copy(), camera_forward:copy())
|
||||||
end
|
end
|
||||||
self.velocity = self.velocity:lerp(target_vel, self.accel)
|
-- self.aabb.min = self.position + aabb_offset
|
||||||
local goal = ((self.position + CAMERA_OFFSET) + self.velocity) * UNIT_SIZE
|
self.aabb.min:set(
|
||||||
local actual_x, actual_y, actual_z, cols, len = self.world:move(self, goal.x, goal.y, goal.z)
|
self.position.x + aabb_offset.x,
|
||||||
-- for i = 1, len do
|
self.position.y + aabb_offset.y,
|
||||||
-- print(util.printt(cols[i].other))
|
self.position.z + aabb_offset.z
|
||||||
-- end
|
)
|
||||||
self.position = Vector3(actual_x / UNIT_SIZE, actual_y / UNIT_SIZE, actual_z / UNIT_SIZE) - CAMERA_OFFSET
|
|
||||||
|
|
||||||
if ctx.mouse_capture then
|
if ctx.mouse_capture then
|
||||||
self.yaw = self.yaw + self.mouse_sensitivity * ctx.mouse_movement.x
|
self.yaw = self.yaw + self.mouse_sensitivity * ctx.mouse_movement.x
|
||||||
end
|
end
|
||||||
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}
|
|
||||||
|
self.aabb:draw()
|
||||||
end
|
end
|
||||||
|
|
||||||
return Player
|
return Player
|
@ -1,12 +1,13 @@
|
|||||||
util = require "util"
|
util = require "util"
|
||||||
UNIT_SIZE = 1
|
|
||||||
local player = require "classes.player"
|
local player = require "classes.player"
|
||||||
local Vector3 = require "types.vector3"
|
local Vector3 = require "types.vector3"
|
||||||
local List = require "types.list"
|
local List = require "types.list"
|
||||||
local bump = require "lib.bump-3dpd"
|
|
||||||
local AABB = require "types.aabb"
|
local AABB = require "types.aabb"
|
||||||
|
|
||||||
local world = bump.newWorld()
|
local test_aabb = AABB.new(
|
||||||
|
Vector3(-1, 0, 3),
|
||||||
|
Vector3(1, 2, 1)
|
||||||
|
)
|
||||||
|
|
||||||
local Obj = require "classes.obj"
|
local Obj = require "classes.obj"
|
||||||
|
|
||||||
@ -45,19 +46,13 @@ local function duck_seek_feed(duck)
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
local test_aabb = AABB.new(Vector3(0, 0, 2), Vector3(1, 1, 1))
|
|
||||||
|
|
||||||
-- called every frame, with constant delta time
|
-- called every frame, with constant delta time
|
||||||
function game_tick()
|
function game_tick()
|
||||||
-- ctx.initialization_needed is true first frame and every time dynamic reload is performed
|
-- ctx.initialization_needed is true first frame and every time dynamic reload is performed
|
||||||
if ctx.initialization_needed then
|
if ctx.initialization_needed then
|
||||||
player:init(world)
|
player:init()
|
||||||
local x,y,z = (Vector3(0, 0, 2) * UNIT_SIZE):decomposed()
|
player.test_intersect = test_aabb
|
||||||
local w,h,d = (Vector3(1, 1, 1) * UNIT_SIZE):decomposed()
|
-- audio_play{audio = "music/bg1.xm", loops = true, channel = "music"}
|
||||||
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)
|
player.ThrowPressed:connect(create_feed)
|
||||||
|
|
||||||
-- spawn some ducks
|
-- spawn some ducks
|
||||||
@ -69,6 +64,7 @@ function game_tick()
|
|||||||
duck.index = i
|
duck.index = i
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ctx.udata persists on reload
|
-- ctx.udata persists on reload
|
||||||
if ctx.udata == nil then
|
if ctx.udata == nil then
|
||||||
ctx.udata = {
|
ctx.udata = {
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -169,25 +169,8 @@ function Vector3:__le(b)
|
|||||||
return self.x <= other.x and self.y <= other.y and self.z <= other.z
|
return self.x <= other.x and self.y <= other.y and self.z <= other.z
|
||||||
end
|
end
|
||||||
|
|
||||||
local NUMK = {"x", "y", "z"}
|
Vector3.__index = Vector3
|
||||||
|
|
||||||
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--------
|
--------API--------
|
||||||
|
|
||||||
function Vector3:length_squared()
|
function Vector3:length_squared()
|
||||||
@ -356,15 +339,6 @@ end
|
|||||||
function Vector3:horizontal()
|
function Vector3:horizontal()
|
||||||
return Vector3(self.x, 0.0, self.z)
|
return Vector3(self.x, 0.0, self.z)
|
||||||
end
|
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
|
---- CONSTANTS
|
||||||
|
|
||||||
---@type Vector3
|
---@type Vector3
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
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
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user