This system is not my own original work
Tis a tweak of a tutorial's system
Following a tutorial by/on NerdyTeachers.com, I was able to whip up this simple collision demo with flag collision, camera collision, movement, and a terrible "music" track.
The tutorial link(s) are below for anyone interested!
https://nerdyteachers.com/Explain/Platformer/
Collision system:
function colcheck(plr,flag,aim) -- player is a table -- player must have x,y in it's table -- flag must be an integer, in our case "0" symbolizes a solid wall -- aim: 0=left,1=right,2=up,3=down -- I don't need width or height in my player table because I know (in my case) it will always be 8 on both sides if pl.iscol==false then -- if the player can't collide, then return "false" return false -- In our case, true = yes, there is something blocking you from moving in that direction -- whereas false = no, there is nothing in that direction end local x=plr.x local y=plr.y -- Make referring to the current x/y simple local x1=0 local x2=0 -- Declare our x1/x2/y1/y2 variables for further modification local y1=0 local y2=0 if aim == 0 then -- What we do here is create a 1 pixel wide and imaginary rectangle using 2 coordinates so we can -- check for collisions, we then use our already defined 2 coords. to find the other coordinates -- in the rectangle to form it completely. This is pretty complicated to be explained in text, -- so I recommend you watch, read the tutorial for a visual explanation! x1=x x2=x-1 y1=y y2=y+7 elseif aim == 1 then x1=x+7 x2=x+8 y1=y y2=y+7 elseif aim == 2 then x1=x x2=x+7 y1=y y2=y-1 elseif aim == 3 then x1=x x2=x+7 y1=y+7 y2=y+8 end x1/=8 x2/=8 y1/=8 y2/=8 -- Divide our imaginary box coordinates by 8, for tile conversion if fget(mget(x1,y1),flag) -- Check if any of our coordinates have a sprite on the same tile coord. or fget(mget(x2,y2),flag) -- "Or" for simplicity, if ANY of our values are colliding we return true or fget(mget(x2,y1),flag) or fget(mget(x1,y2),flag) then return true -- Yep, we found a collision else -- If we haven't found a collision, then return false -- Nope, you are good to move further end -- End our check end -- End the function |
[Please log in to post a comment]