Advertisement
JacKobOriginal

Having problems with an AABB Collision Function

Jun 17th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.08 KB | None | 0 0
  1. function love.update(dt)
  2.     if CheckCollision(player.x, player.y, player.w, player.h, blockx, blocky, blockw, blockh) then
  3.         blockcollision = true
  4.     end
  5.     if love.keyboard.isDown("a") then
  6.         if blockcollision == false then
  7.             textx = textx + player.spd * dt
  8.             blockx = blockx + player.spd * dt
  9.         end
  10.     elseif love.keyboard.isDown("d") then
  11.         if blockcollision == false then
  12.             textx = textx - player.spd * dt
  13.             blockx = blockx - player.spd * dt
  14.         end
  15.     end
  16.     if love.keyboard.isDown("w") then
  17.         if blockcollision == false then
  18.             texty = texty + player.spd * dt
  19.             blocky = blocky + player.spd * dt
  20.         end
  21.     elseif love.keyboard.isDown("s") then
  22.         if blockcollision == false then
  23.             texty = texty - player.spd * dt
  24.             blocky = blocky - player.spd * dt
  25.         end
  26.     end
  27. end
  28.  
  29. function CheckCollision(x1, y1, w1, h1, x2, y2, w2, h2)
  30.   return x1 < x2 + w2 and
  31.          x2 < x1 + w1 and
  32.          y1 < y2 + h2 and
  33.          y2 < y1 + h1
  34. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement