Open-Terrarium/collision.lua

129 lines
3.5 KiB
Lua
Raw Normal View History

--basic grid based collision detection
2017-07-10 04:41:19 -04:00
--{-x,+x,-y,+y}
player_collision_box = {-0.2,0.2,-0.9,0.8}
2017-07-09 22:23:40 -04:00
--squared collision detection
2017-07-10 00:34:00 -04:00
player.on_block = false
function collision(oldposx,oldposy)
--do stairs
2017-07-12 05:31:02 -04:00
player_in_unloaded_chunk = false
local xer = {player_collision_box[1],player_collision_box[2]}
local yer = {player_collision_box[3],player_collision_box[4]}
2017-07-10 00:34:00 -04:00
local fall = true
--check the corners (y)
player.playery = player.playery + player.inertiay
2017-07-09 22:23:40 -04:00
for q = 1,2 do
for r = 1,2 do
2017-07-12 02:14:30 -04:00
local squarex = math.floor(player.playerx+xer[q])
local squarey = math.floor(player.playery+yer[r])
--use this to detect outside chunk 00
local chunkerx = 0
local chunkery = 0
2017-07-12 02:23:42 -04:00
if squarex < 1 then
chunkerx = -1
squarex = map_max
2017-07-12 04:34:18 -04:00
--print("adjusting x -1")
2017-07-12 02:23:42 -04:00
elseif squarex > map_max then
chunkerx = 1
squarex = 1
2017-07-12 04:34:18 -04:00
--print("adjusting x +1")
2017-07-12 02:14:30 -04:00
end
2017-07-12 02:23:42 -04:00
if squarey < 1 then
chunkery = 1
squarey = map_max
2017-07-12 04:34:18 -04:00
--print("adjusting y +1")
2017-07-12 02:23:42 -04:00
elseif squarey > map_max then
chunkery = -1
squarey = 1
2017-07-12 04:34:18 -04:00
--print("adjusting y -1")
2017-07-12 02:23:42 -04:00
end
2017-07-12 04:34:18 -04:00
--print(chunkerx, chunkery, "|", squarex,squarey)
--print( loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"])
2017-07-19 02:32:24 -04:00
--if (squarex1 > map_max or squarex1 <= 0) or (squarey1 > map_max or squarey1 <= 0) or blocks[loaded_chunks[0][0][squarex1][squarey1]["block"]]["collide"] ~= false then
2017-07-17 04:13:06 -04:00
if loaded_chunks[chunkx+chunkerx] and loaded_chunks[chunkx+chunkerx][chunkery+chunky] and loaded_chunks[chunkx+chunkerx][chunkery+chunky][squarex] and loaded_chunks[chunkx+chunkerx][chunkery+chunky][squarex][squarey] then
2017-07-19 02:32:24 -04:00
if blocks[loaded_chunks[chunkx+chunkerx][chunkery+chunky][squarex][squarey]["block"]]["collide"] ~= false then
2017-07-12 05:25:10 -04:00
player.playery = oldposy
2017-07-12 05:25:10 -04:00
if r == 2 then
player.on_block = true
fall = false
end
if r == 1 then
player.on_block = false
end
2017-07-10 00:34:00 -04:00
end
2017-07-12 05:31:02 -04:00
else
player_in_unloaded_chunk = true
2017-07-10 00:34:00 -04:00
end
end
end
if fall == true then
player.on_block = false
end
--check the corners(x)
player.playerx = player.playerx + player.inertiax
2017-07-10 00:34:00 -04:00
for q = 1,2 do
for r = 1,3 do
2017-07-12 02:14:30 -04:00
local squarex = math.floor(player.playerx+xer[q])
local squarey
if r < 3 then
squarey = math.floor(player.playery+yer[r])
else
squarey = math.floor(player.playery)
end
2017-07-12 02:23:42 -04:00
2017-07-17 04:13:06 -04:00
--print(squarex)
2017-07-12 02:23:42 -04:00
--use this to detect outside chunk 00
local chunkerx = 0
local chunkery = 0
if squarex < 1 then
chunkerx = -1
squarex = map_max
2017-07-12 04:34:18 -04:00
--print("adjusting x -1")
2017-07-12 02:23:42 -04:00
elseif squarex > map_max then
chunkerx = 1
squarex = 1
2017-07-12 04:34:18 -04:00
--print("adjusting x +1")
2017-07-12 02:23:42 -04:00
end
if squarey < 1 then
chunkery = 1
squarey = map_max
2017-07-12 04:34:18 -04:00
--print("adjusting y +1")
2017-07-12 02:23:42 -04:00
elseif squarey > map_max then
chunkery = -1
squarey = 1
2017-07-12 04:34:18 -04:00
--print("adjusting y -1")
2017-07-12 02:23:42 -04:00
end
2017-07-19 02:32:24 -04:00
--if (squarex1 > map_max or squarex1 <= 0) or (squarey1 > map_max or squarey1 <= 0) or blocks[loaded_chunks[0][0][squarex1][squarey1]["block"]]["collide"] ~= false then
2017-07-17 04:13:06 -04:00
if loaded_chunks[chunkx+chunkerx] and loaded_chunks[chunkx+chunkerx][chunkery+chunky] and loaded_chunks[chunkx+chunkerx][chunkery+chunky][squarex] and loaded_chunks[chunkx+chunkerx][chunkery+chunky][squarex][squarey] then
2017-07-19 02:32:24 -04:00
if blocks[loaded_chunks[chunkx+chunkerx][chunkery+chunky][squarex][squarey]["block"]]["collide"] ~= false then
2017-07-12 05:25:10 -04:00
player.inertiax = 0
player.playerx = oldposx
--print("stopping x inertia and pos")
end
2017-07-12 05:31:02 -04:00
else
player_in_unloaded_chunk = true
2017-07-09 22:23:40 -04:00
end
end
end
end
2017-06-20 03:39:40 -04:00