Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --general collision detection algorithm where x is posX, y is posY, sx is size x, sy is size y
- function isColliding(body1, body2)
- return (
- body1.x + body1.sx > body2.x and
- body1.y + body1.sy > body2.y and
- body2.x + body2.sx > body1.x and
- body2.y + body2.sy > body1.y
- )
- end
- --example: if isColliding(player, enemy) then ( ... ) end
- --general algorithm to get colliding sides
- function edges(body1, body2)
- local tolerance = 5 --define a tolerance: if the bodies are colliding and side x is within tolerance pixels of side y...
- --return a tuple of booleans: left side, right side, top side, bottom side
- return
- body1.x + body1.sx - body2.x <= tolerance,
- body2.x + body2.sx - body1.x <= tolerance,
- body1.y + body1.sy - body2.y <= tolerance,
- body2.y + body2.sy - body1.y <= tolerance
- end
- --now these put together in an example:
- function love:update(dt)
- if isColliding(player, enemy) then --NOTE: you SHOULD make isColliding a method of player so u can do if player:isColliding(enemy), but it's not needed
- local left, right, top, bottom = edges(player, enemy)
- --left = true if the right side of player is touching left side of enemy, same for the rest
- print(left, right, top, bottom)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement