Advertisement
JupiterSky

Collision

Dec 17th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.90 KB | None | 0 0
  1. -- title:  game title
  2. -- author: game developer
  3. -- desc:   short description
  4. -- script: lua
  5.  
  6.  
  7. box1 = {}
  8. box2 = {}
  9.  
  10.  
  11. box1.x = 20
  12. box1.y = 40
  13. box1.width = 8
  14.  
  15. box2.x = 50
  16. box2.y = 40
  17. box2.width = 2
  18.  
  19.  
  20. function TIC()
  21.  
  22. cls()
  23.  
  24. if btn(2) then box2.x = box2.x - 1 end
  25. if btn(3) then box2.x = box2.x + 1 end
  26.  
  27. if btn(0) then box2.y = box2.y - 1 end
  28. if btn(1) then box2.y = box2.y + 1 end
  29.  
  30. local hw1 = box1.width*0.5
  31. local hw2 = box2.width*0.5
  32. local invjmpx = false
  33. local invjmpy = false
  34. local xjump = 0
  35. local yjump = 0
  36. local colx = false
  37. local coly = false
  38.  
  39. if box2.x >= box1.x and box2.y > box1.y - (hw1 + hw2) and box2.y < box1.y + (hw1 + hw2) then
  40.  
  41. local length = box2.x - box1.x
  42. local gap = length - hw1 - hw2
  43. if gap < 0 then
  44.  xjump = gap
  45.  invjmpx = true
  46.     colx = true
  47. end
  48.  
  49. elseif box2.x < box1.x and box2.y > box1.y - (hw1 + hw2) and box2.y < box1.y + (hw1 + hw2) then
  50.  
  51. local length = box1.x - box2.x
  52. local gap = length - hw1 - hw2
  53.  
  54. if gap < 0 then
  55.     xjump = gap
  56.     invjmpx = false
  57.     colx = true
  58. end
  59.  
  60. end
  61.  
  62.  
  63.  
  64.  
  65. if box2.y >= box1.y and box2.x > box1.x - (hw1 + hw2) and box2.x < box1.x + (hw1 + hw2) then
  66.  
  67. local length = box2.y - box1.y
  68. local gap = length - hw1 - hw2
  69.  
  70. if gap < 0 then
  71.     yjump = gap
  72.     invjmpy = true
  73.     coly = true
  74. end
  75.  
  76. elseif box2.y < box1.y and box2.x > box1.x - (hw1 + hw2) and box2.x < box1.x + (hw1 + hw2) then
  77.  
  78. local length = box1.y - box2.y
  79. local gap = length - hw1 - hw2
  80.  
  81. if gap < 0 then
  82.     yjump = gap
  83.     invjmpy = false
  84.     coly = true
  85. end
  86.  
  87. end
  88.  
  89.  
  90.  
  91.  
  92. if yjump >= xjump and coly then
  93.     if invjmpy then
  94.      box2.y = box2.y - yjump
  95.     elseif not invjmpy then
  96.      box2.y = box2.y + yjump
  97.     end
  98. elseif xjump >= yjump and colx then
  99.     if invjmpx then
  100.      box2.x = box2.x - xjump
  101.     elseif not invjmpx then
  102.      box2.x = box2.x + xjump
  103.     end
  104. end
  105.  
  106.  
  107. rect(box1.x-hw1, box1.y-hw1, box1.width, box1.width, 11)
  108. rect(box2.x-hw2, box2.y-hw2, box2.width, box2.width, 11)
  109.  
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement