
..mainly how to do sprite ordering & collisions in a situation as depicted above

Code: Select all
function sort(T) table.sort(T, function(a, b) return a.y < b.y end ) end
Code: Select all
function overlap(x1,y1,w1,h1,x2,y2,w2,h2)
local tl, bl, tr, br = false, false, false, false
if (x2 >= x1 and x2 <= (x1 + w1)) and (y2 >= y1 and y2 <= (y1 + h1)) then tl = true end
if (x2+w2 >= x1 and x2+w2 <= (x1 + w1)) and (y2 >= y1 and y2 <= (y1 + h1)) then tr = true end
if (x2 >= x1 and x2 <= (x1 + w1)) and (y2+h2 >= y1 and y2+h2 <= (y1 + h1)) then bl = true end
if (x2+w2 >= x1 and x2+w2 <= (x1 + w1)) and (y2+h2 >= y1 and y2+h2 <= (y1 + h1)) then br = true end
if (x1 >= x2 and x1 <= (x2 + w2)) and (y1 >= y2 and y1 <= (y2 + h2)) then tl = true end
if (x1+w1 >= x2 and x1+w1 <= (x2 + w2)) and (y1 >= y2 and y1 <= (y2 + h2)) then tr = true end
if (x1 >= x2 and x1 <= (x2 + w2)) and (y1+h1 >= y2 and y1+h1 <= (y2 + h2)) then bl = true end
if (x1+w1 >= x2 and x1+w1 <= (x2 + w2)) and (y1+h1 >= y2 and y1+h1 <= (y2 + h2)) then br = true end
if tl or tr or bl or br then return true else return false end
end
Hm. Lemme try.Jasoco wrote:Yes, there might be a more compact way to do it, if so, let me know. As long as it takes the same parameters and returns the same values, I don't care what the function looks like.Code: Select all
function overlap(x1,y1,w1,h1,x2,y2,w2,h2) local tl, bl, tr, br = false, false, false, false if (x2 >= x1 and x2 <= (x1 + w1)) and (y2 >= y1 and y2 <= (y1 + h1)) then tl = true end if (x2+w2 >= x1 and x2+w2 <= (x1 + w1)) and (y2 >= y1 and y2 <= (y1 + h1)) then tr = true end if (x2 >= x1 and x2 <= (x1 + w1)) and (y2+h2 >= y1 and y2+h2 <= (y1 + h1)) then bl = true end if (x2+w2 >= x1 and x2+w2 <= (x1 + w1)) and (y2+h2 >= y1 and y2+h2 <= (y1 + h1)) then br = true end if (x1 >= x2 and x1 <= (x2 + w2)) and (y1 >= y2 and y1 <= (y2 + h2)) then tl = true end if (x1+w1 >= x2 and x1+w1 <= (x2 + w2)) and (y1 >= y2 and y1 <= (y2 + h2)) then tr = true end if (x1 >= x2 and x1 <= (x2 + w2)) and (y1+h1 >= y2 and y1+h1 <= (y2 + h2)) then bl = true end if (x1+w1 >= x2 and x1+w1 <= (x2 + w2)) and (y1+h1 >= y2 and y1+h1 <= (y2 + h2)) then br = true end if tl or tr or bl or br then return true else return false end end
Code: Select all
function overlap(x1,y1,w1,h1,x2,y2,w2,h2)
return (((x2+w2 >= x1 and x2+w2 <= (x1 + w1)) or (x2 >= x1 and x2 <= (x1 + w1)))
and ((y2 >= y1 and y2 <= (y1 + h1)) or (y2+h2 >= y1 and y2+h2 <= (y1 + h1)))
end
Amazing. Aside from a missing closing parenthesis at the end, (Which I added above in the quote just in case someone wants to use the code it'll work right.) it works just as well. Thanks! I can't see how it could get any shorter without taking out all the spaces, replacing the two digit variables with single digit ones, removing unneeded parenthesis and putting it all on one line. Like this...Robin wrote:Hm. Lemme try.Warning: untested, and it can probably be made shorter.Code: Select all
function overlap(x1,y1,w1,h1,x2,y2,w2,h2) return (((x2+w2 >= x1 and x2+w2 <= (x1 + w1)) or (x2 >= x1 and x2 <= (x1 + w1))) and ((y2 >= y1 and y2 <= (y1 + h1)) or (y2+h2 >= y1 and y2+h2 <= (y1 + h1)))) end
Code: Select all
function o(x,y,w,h,a,b,c,d) return (((a+c>=x and a+c<=x+w) or (a>=x and a<=x+w)) and ((b>=y and b<=y+h) or (b+d>=y and b+d<=y+h))) end
Code: Select all
function overlap(x1,y1,w1,h1, x2,y2,w2,h2)
return (x2 >= x1 and x2 <= x1+w1) and (y2 >= y1 and y2 <= y1+h1) or
(x2+w2 >= x1 and x2+w2 <= x1+w1) and (y2 >= y1 and y2 <= y1+h1) or
(x2 >= x1 and x2 <= x1+w1) and (y2+h2 >= y1 and y2+h2 <= y1+h1) or
(x2+w2 >= x1 and x2+w2 <= x1+w1) and (y2+h2 >= y1 and y2+h2 <= y1+h1) or
(x1 >= x2 and x1 <= x2+w2) and (y1 >= y2 and y1 <= y2+h2) or
(x1+w1 >= x2 and x1+w1 <= x2+w2) and (y1 >= y2 and y1 <= y2+h2) or
(x1 >= x2 and x1 <= x2+w2) and (y1+h1 >= y2 and y1+h1 <= y2+h2) or
(x1+w1 >= x2 and x1+w1 <= x2+w2) and (y1+h1 >= y2 and y1+h1 <= y2+h2)
end
At the risk of hijacking paramecij's thread, i'll share my rectangle collision code:Jasoco wrote:Yes, there might be a more compact way to do it, if so, let me know. As long as it takes the same parameters and returns the same values, I don't care what the function looks like.
Code: Select all
function overlap(x1,y1,w1,h1,x2,y2,w2,h2)
if x1+w1 < x2
or x2+w2 < x1
or y1+h1 < y2
or y2+h2 < y1 then
return false
end
return true
end
Oops.ghostwriter wrote:At the risk of hijacking paramecij's thread,
Your version was exactly what I meant, I just couldn't remember what it was exactly, probably because of the Eldritch abomination that was the original (sorry, Jasoco).ghostwriter wrote:i'll share my rectangle collision code:
Code: Select all
function overlap(x1,y1,w1,h1,x2,y2,w2,h2)
return not (x1+w1 < x2 or x2+w2 < x1
or y1+h1 < y2 or y2+h2 < y1)
end
At the risk of being stupid and an idiot because it goes against every 3d game ever made, but I always prefer to refer to X and Y as being the floor and Z being the height of the room. It always seemed weird to have Z be north and south and Y being above and below. Just seems stupid and I blame iD Software even though they probably didn't come up with it, but I need a scapegoat to blame and they were the first to come to mind. i.e., the ground in that game would be X and Y and the Z would be the characters jumping or on stairs position above the ground level.ghostwriter wrote:To reply to paramecij's original question, I would recommend storing the full three dimensional x-y-z coordinates of each sprite. That way you can still use the sorting method suggested by Jasoco, as well as have things like stairs. For example, moving left and right would change the x position, moving towards or away from the screen would change the z position and moving vertically changes the Y position.
This way you could still simply sort by Z position for drawing, no matter how high something is on the Y axis. You'd just have to figure out how to draw everything in the correct position. You can see in the image below, even though the two characters are on the stairs, their z-position is closer than the z-position of the guy on the ground beside them.
Users browsing this forum: No registered users and 3 guests