Skip to content

Instantly share code, notes, and snippets.

@f2l2pe
Created September 2, 2024 20:43
Show Gist options
  • Save f2l2pe/d5f7073751b7d19560b592d919371b34 to your computer and use it in GitHub Desktop.
Save f2l2pe/d5f7073751b7d19560b592d919371b34 to your computer and use it in GitHub Desktop.
local function check_collision(self, pos)
local function draw_ray(from, to)
local groups = { hash("level") }
local color = vmath.vector4(0, 1, 0, 1)
return data.draw_ray(from, to, groups, color)
end
self.ray_origins = {
tl = self.old_pos + self.col_pos.tl,
tr = self.old_pos + self.col_pos.tr,
bl = self.old_pos + self.col_pos.bl,
br = self.old_pos + self.col_pos.br,
}
local hit_x = nil
local ceil_check = nil
local ground_check = nil
for _, value in pairs(self.ray_origins) do
data.utils.debugdraw.circle(value.x, value.y, 2)
end
if self.velocity.x > 0 then
hit_x = draw_ray(self.ray_origins.tl, self.ray_origins.tl + data.utils.vector.right * ray_size)
hit_x = draw_ray(self.ray_origins.bl, self.ray_origins.bl + data.utils.vector.right * ray_size)
elseif self.velocity.x < 0 then
hit_x = draw_ray(self.ray_origins.tr, self.ray_origins.tr + data.utils.vector.left * ray_size)
hit_x = draw_ray(self.ray_origins.br, self.ray_origins.br + data.utils.vector.left * ray_size)
end
if self.velocity.y > 0 then
ceil_check = draw_ray(self.ray_origins.bl, self.ray_origins.bl + data.utils.vector.up * ray_size)
if not ceil_check then
ceil_check = draw_ray(self.ray_origins.br, self.ray_origins.br + data.utils.vector.up * ray_size)
end
end
ground_check = draw_ray(self.ray_origins.tl, self.ray_origins.tl + data.utils.vector.down * ray_size)
if not ground_check then
ground_check = draw_ray(self.ray_origins.tr, self.ray_origins.tr + data.utils.vector.down * ray_size)
end
if hit_x then
local hit = hit_x[1]
local hit_pos = hit.position
local hit_pos = data.round_vector3(hit_pos)
data.utils.debugdraw.circle(hit_pos.x, hit_pos.y, 10)
local new_pos_x
local wall_x
if hit.normal.x > 0 then
wall_x = data.tile2world(data.world2tile(hit_pos) + data.utils.vector.left).x + data.TILE_SIZE / 2
new_pos_x = (pos + self.col_pos.bl).x
elseif hit.normal.x < 0 then
wall_x = data.tile2world(data.world2tile(hit_pos)).x - data.TILE_SIZE / 2
new_pos_x = (pos + self.col_pos.br).x
end
local diff_new_pos = math.abs(new_pos_x - wall_x)
if (diff_new_pos <= 0) then
pos.x = self.old_pos.x
self.velocity.x = 0
end
end
if ceil_check then
local hit = ceil_check[1]
local hit_pos = data.round_vector3(hit.position)
data.utils.debugdraw.circle(hit_pos.x, hit_pos.y, 10)
if hit.normal.y < 0 then
local new_top_pos = (pos + self.col_pos.tl).y
local hit_pos_y = hit_pos.y
if new_top_pos >= hit_pos_y then
new_top_pos = hit_pos_y
pos.y = data.round(new_top_pos - self.col_pos.tl.y)
self.velocity.y = 0
end
end
end
if ground_check then
local hit = ground_check[1]
local hit_pos = data.round_vector3(hit.position)
data.utils.debugdraw.circle(hit_pos.x, hit_pos.y, 10)
if hit.normal.y > 0 then
local wall_y
wall_y = data.tile2world(data.world2tile(hit_pos) + data.utils.vector.down).y + data.TILE_SIZE / 2
local new_bot_pos_y = (pos + self.col_pos.bl).y
if new_bot_pos_y <= wall_y then
new_bot_pos_y = wall_y
pos.y = data.round(new_bot_pos_y - self.col_pos.br.y + 1)
self.is_grounded = true
self.velocity.y = 0
end
end
else
self.is_grounded = false
end
return pos
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment