Compare commits
3 Commits
e3f195cf71
...
b25e41bd49
Author | SHA1 | Date | |
---|---|---|---|
b25e41bd49 | |||
91c2e1bce1 | |||
1433aa8e40 |
@ -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),
|
||||||
@ -13,9 +14,81 @@ local Player = {
|
|||||||
|
|
||||||
accel = 0.3,
|
accel = 0.3,
|
||||||
|
|
||||||
|
aabb = nil,
|
||||||
|
test_intersect = nil,
|
||||||
|
|
||||||
ThrowPressed = Signal.new(),
|
ThrowPressed = Signal.new(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local aabb_offset = Vector3(-0.25, -1, -0.25)
|
||||||
|
|
||||||
|
---@param aabb AABB
|
||||||
|
---@param other AABB
|
||||||
|
---@param velocity Vector3
|
||||||
|
---@return Vector3, Vector3
|
||||||
|
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
|
||||||
|
|
||||||
function Player:tick(ctx)
|
function Player:tick(ctx)
|
||||||
input_action{name = "left", control = "A"}
|
input_action{name = "left", control = "A"}
|
||||||
input_action{name = "right", control = "D"}
|
input_action{name = "right", control = "D"}
|
||||||
@ -34,17 +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 = 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.aabb.min = self.position + aabb_offset
|
||||||
|
self.aabb.min:set(
|
||||||
|
self.position.x + aabb_offset.x,
|
||||||
|
self.position.y + aabb_offset.y,
|
||||||
|
self.position.z + aabb_offset.z
|
||||||
|
)
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
self.position = self.position + self.velocity
|
|
||||||
|
self.aabb:draw()
|
||||||
end
|
end
|
||||||
|
|
||||||
return Player
|
return Player
|
@ -2,12 +2,12 @@ util = require "util"
|
|||||||
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 AABB = require "types.aabb"
|
local AABB = require "types.aabb"
|
||||||
|
|
||||||
-- local test_aabb = AABB.new(
|
local test_aabb = AABB.new(
|
||||||
-- Vector3(-1, -1, -1),
|
Vector3(-1, 0, 3),
|
||||||
-- Vector3(1, 1, 1)
|
Vector3(1, 2, 1)
|
||||||
-- )
|
)
|
||||||
|
|
||||||
local Obj = require "classes.obj"
|
local Obj = require "classes.obj"
|
||||||
|
|
||||||
@ -50,6 +50,8 @@ end
|
|||||||
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()
|
||||||
|
player.test_intersect = test_aabb
|
||||||
-- audio_play{audio = "music/bg1.xm", loops = true, channel = "music"}
|
-- audio_play{audio = "music/bg1.xm", loops = true, channel = "music"}
|
||||||
player.ThrowPressed:connect(create_feed)
|
player.ThrowPressed:connect(create_feed)
|
||||||
|
|
||||||
@ -91,12 +93,12 @@ function game_tick()
|
|||||||
texture = "images/measure001a.png",
|
texture = "images/measure001a.png",
|
||||||
texture_region = { x = 0, y = 0, w = 512, h = 512 },
|
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.y = math.sin(ctx.frame_number * 0.05)
|
||||||
-- cube.position.z = math.cos(ctx.frame_number * 0.01)
|
-- cube.position.z = math.cos(ctx.frame_number * 0.01)
|
||||||
cube.rotation.x = cube.rotation.x + 0.01
|
cube.rotation.x = cube.rotation.x + 0.01
|
||||||
cube.rotation.z = cube.rotation.z + 0.01
|
cube.rotation.z = cube.rotation.z + 0.01
|
||||||
cube:draw()
|
cube:draw()
|
||||||
|
|
||||||
-- test_aabb:draw()
|
test_aabb:draw()
|
||||||
end
|
end
|
||||||
|
@ -3,36 +3,83 @@ local Vector3 = require "types.vector3"
|
|||||||
---@class AABB
|
---@class AABB
|
||||||
local AABB = {
|
local AABB = {
|
||||||
min = Vector3(),
|
min = Vector3(),
|
||||||
max = Vector3(),
|
size = Vector3(),
|
||||||
|
}
|
||||||
|
|
||||||
|
local RED = {
|
||||||
|
r = 255,
|
||||||
|
g = 0,
|
||||||
|
b = 0,
|
||||||
|
a = 255,
|
||||||
}
|
}
|
||||||
|
|
||||||
setmetatable(AABB, AABB)
|
setmetatable(AABB, AABB)
|
||||||
|
|
||||||
AABB.__index = AABB
|
AABB.__index = AABB
|
||||||
|
|
||||||
---@param min vectorlike
|
---@param position Vector3
|
||||||
---@param max vectorlike
|
---@param size Vector3
|
||||||
---@return AABB
|
---@return AABB
|
||||||
function AABB.new(min, max)
|
function AABB.new(position, size)
|
||||||
min = min or Vector3()
|
position = position or Vector3()
|
||||||
max = max or Vector3()
|
size = size or Vector3(1, 1, 1)
|
||||||
local aabb = {
|
local aabb = {
|
||||||
min = min,
|
min = position,
|
||||||
max = max,
|
size = size,
|
||||||
}
|
}
|
||||||
|
|
||||||
return setmetatable(aabb, AABB)
|
return setmetatable(aabb, AABB)
|
||||||
end
|
end
|
||||||
|
|
||||||
function AABB:draw()
|
function AABB:get_max()
|
||||||
-- bottom rectangle
|
return self.min + self.size
|
||||||
draw_line_3d{start = self.min, finish = Vector3(self.max.x, self.min.y, self.min.z)}
|
end
|
||||||
draw_line_3d{start = self.min, finish = Vector3(self.min.x, self.min.y, self.max.z)}
|
|
||||||
draw_line_3d{start = Vector3(self.max.x, self.min.y, self.max.z), finish = Vector3(self.min.x, self.min.y, self.max.z)}
|
|
||||||
|
|
||||||
-- draw_line_3d{start = self.min, finish = Vector3(self.max.x, self.min.y, self.min.z)}
|
function AABB:draw()
|
||||||
-- draw_line_3d{start = self.min, finish = Vector3(self.min.x, self.max.y, self.min.z)}
|
local max = self:get_max()
|
||||||
-- draw_line_3d{start = self.min, finish = Vector3(self.min.x, self.min.y, self.max.z)}
|
-- 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
|
end
|
||||||
|
|
||||||
return AABB
|
return AABB
|
||||||
|
@ -155,6 +155,20 @@ function Vector3:__tostring()
|
|||||||
return "Vector3(" .. tostring(self.x) .. ", " .. tostring(self.y) .. ", " .. tostring(self.z) .. ")"
|
return "Vector3(" .. tostring(self.x) .. ", " .. tostring(self.y) .. ", " .. tostring(self.z) .. ")"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- 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
|
||||||
|
|
||||||
Vector3.__index = Vector3
|
Vector3.__index = Vector3
|
||||||
|
|
||||||
--------API--------
|
--------API--------
|
||||||
@ -246,6 +260,24 @@ function Vector3:rotate(axis, angle)
|
|||||||
return self
|
return self
|
||||||
end
|
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.
|
---Returns a copy of this vector.
|
||||||
---@return Vector3
|
---@return Vector3
|
||||||
function Vector3:copy()
|
function Vector3:copy()
|
||||||
|
Loading…
Reference in New Issue
Block a user