Advertisement
Guest User

Untitled

a guest
Apr 4th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. btk.AABB = {type = "AABB"}
  2. function btk.AABB:new(x, y, w, h)
  3. o = {}
  4. setmetatable(o, self)
  5.  
  6. o.x = x
  7. o.y = y
  8. o.w = w
  9. o.h = h
  10.  
  11. o.sides = {}
  12.  
  13. return o
  14. end
  15.  
  16. function btk.AABB:__index(key)
  17. if key == "left" then
  18. return self.x
  19. elseif key == "top" then
  20. return self.y
  21. elseif key == "bottom" then
  22. return self.y + self.h
  23. elseif key == "right" then
  24. return self.x + self.w
  25. elseif key == "cx" then
  26. return self.x + self.w / 2
  27. elseif key == "cy" then
  28. return self.y + self.h / 2
  29. end
  30.  
  31. return btk.AABB[key]
  32. end
  33.  
  34. function btk.AABB:__newindex(k, v)
  35. if k == "left" then
  36. local diffX = v - self.x
  37. self.x = self.x + diffX
  38. self.w = self.w - diffX
  39. elseif k == "top" then
  40. local diffY = v - self.y
  41. self.y = self.y + diffY
  42. self.h = self.w - diffY
  43. elseif k == "right" then
  44. self.w = v - self.x
  45. elseif k == "bottom" then
  46. self.h = v - self.y
  47. elseif k == "cx" then
  48. self:setCenter(v, self.cy)
  49. elseif k == "cy" then
  50. self:setCenter(self.cx, v)
  51. else
  52. rawset(self, k, v)
  53. end
  54. end
  55.  
  56. function btk.AABB:center()
  57. return btk.Vec2:new(
  58. self.x + self.w / 2,
  59. self.y + self.h / 2
  60. )
  61. end
  62.  
  63. function btk.AABB:grow(...)
  64. local dx, dy = nil, nil
  65. local n = select("#", ...)
  66. local arg1, arg2 = select(1, ...), select(2, ...)
  67.  
  68. if n == 1 then
  69. local argType = getType(arg1)
  70.  
  71. if argType == "number" then
  72. dx = arg1
  73. dy = arg1
  74. elseif argType == "Vec2" then
  75. dx = arg1.x
  76. dy = arg1.y
  77. else
  78. assert(false, "Unexpected type " .. argType)
  79. end
  80. elseif n == 2 then
  81. dx = arg1
  82. dy = arg2
  83. end
  84.  
  85. self.x = self.x - dx
  86. self.y = self.y - dy
  87. self.w = self.w + 2 * dx
  88. self.h = self.h + 2 * dy
  89.  
  90. return self
  91. end
  92.  
  93. function btk.AABB:draw()
  94. love.graphics.rectangle("line", self.x, self.y, self.w, self.h);
  95. end
  96.  
  97. function btk.AABB:halfWidths()
  98. return btk.Vec2:new(self.w / 2, self.h / 2)
  99. end
  100.  
  101. function btk.AABB:containsPoint(...)
  102. local x = nil
  103. local y = nil
  104.  
  105. if select("#", ...) == 1 then
  106. local point = select(1, ...)
  107. assert(getType(point) == "Vec2", "Vec2 expected")
  108.  
  109. x = point.x
  110. y = point.y
  111. elseif select("#", ...) == 2 then
  112. x = select(1, ...)
  113. y = select(2, ...)
  114. end
  115.  
  116. if x > self.left and
  117. y > self.top and
  118. x < self.right and
  119. y < self.bottom then
  120. return {
  121. left = x - self.left,
  122. right = self.right - x,
  123. top = y - self.top,
  124. bottom = self.bottom - y,
  125. }
  126. end
  127.  
  128. return nil
  129. end
  130.  
  131. function btk.AABB:setCenter(...)
  132. local n = select("#", ...)
  133. local arg1 = select(1, ...)
  134. local arg2 = select(2, ...)
  135.  
  136. if n == 1 then
  137. assert(getType(arg1) == "Vec2", "Unexpected type " .. getType(arg1))
  138. self.x = arg1.x - self.w / 2
  139. self.y = arg1.y - self.h / 2
  140. elseif n == 2 then
  141. self.x = arg1 - self.w / 2
  142. self.y = arg2 - self.h / 2
  143. end
  144.  
  145. return self
  146. end
  147.  
  148. function btk.AABB:__tostring()
  149. return "[" ..
  150. self.x .. " "
  151. .. self.y .. " "
  152. .. self.w .. " "
  153. .. self.h .. "]"
  154. end
  155.  
  156. function btk.AABB:move(...)
  157. local n = select("#", ...)
  158. local arg1 = select(1, ...)
  159. local arg2 = select(2, ...)
  160.  
  161. local x, y = 0, 0
  162.  
  163. if n == 1 then
  164. vec = arg1
  165. assert(getType(vec) == "Vec2", "Unexpected type " .. getType(vec))
  166. x = vec.x
  167. y = vec.y
  168. elseif n == 2 then
  169. x = arg1
  170. y = arg2
  171. end
  172.  
  173. self.x = self.x + x
  174. self.y = self.y + y
  175.  
  176. return self
  177. end
  178.  
  179. function btk.AABB.collide(a, b)
  180. local sides = {}
  181. if next(a.sides) == nil then -- If sides is empty
  182. sides = {
  183. top = true,
  184. right = true,
  185. bottom = true,
  186. left = true
  187. }
  188. else
  189. sides = a.sides
  190. end
  191.  
  192. local collisionBox = clone(a):grow(b:halfWidths())
  193. local ds = collisionBox:containsPoint(b:center())
  194. local dVec = nil
  195.  
  196. if ds then
  197. local other = btk.Vec2:new(0, -ds.top)
  198. if sides.top then
  199. dVec = other
  200. end
  201.  
  202. other = btk.Vec2:new(ds.right, 0)
  203. if sides.right and (not dVec or other < dVec) then
  204. dVec = other
  205. end
  206.  
  207. other = btk.Vec2:new(0, ds.bottom)
  208. if sides.bottom and (not dVec or other < dVec) then
  209. dVec = other
  210. end
  211.  
  212. other = btk.Vec2:new(-ds.left, 0)
  213. if sides.left and (not dVec or other < dVec) then
  214. dVec = other
  215. end
  216. end
  217.  
  218. return dVec
  219. end
  220.  
  221. function btk.AABB.sweepCollide(a, b, v)
  222. assert(false, "Not yet implemented")
  223. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement