Advertisement
Guest User

Untitled

a guest
Dec 1st, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. function resetcollisionoptions()
  2. collidingoptions = {}
  3. collidingoptions["turtle"] = {"all"}
  4. collidingoptions["button"] = {"turtle"}
  5. collidingoptions["ladder"] = {"turtle"}
  6. collidingoptions["door"] = {"turtle"}
  7. collidingoptions["coin"] = {"turtle"}
  8. collidingoptions["key"] = {"turtle"}
  9. collidingoptions["gate"] = {"turtle"}
  10. collidingoptions["crate"] = {"block", "permabutton", "floorbutton"}
  11. end
  12.  
  13. local function aabb(ax1,ay1,aw,ah, bx1,by1,bw,bh)
  14. local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
  15. return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
  16. end
  17.  
  18. local function calculatemtd(v1, v2)
  19. --UGHHGHGHGHGH
  20. --LETS DO SOME MATH.
  21. local v1minx = v1.x --Min = top left
  22. local v1miny = v1.y
  23. local v2minx = v2.x
  24. local v2miny = v2.y
  25. local v1maxx = v1.x + v1.width -- Max = bottom right
  26. local v1maxy = v1.y + v1.height
  27. local v2maxx = v2.x + v2.width
  28. local v2maxy = v2.y + v2.height
  29. local abs = math.abs
  30.  
  31. --K, we got our first set of values.
  32.  
  33. local mtd = {} --Make our mtd table.
  34. --I don't like math too much anymore
  35.  
  36. local left = v2minx - v1maxx
  37. local right = v2maxx - v1minx
  38. local up = v2miny - v1maxy
  39. local down = v2maxy - v1miny
  40. if left > 0 or right < 0 or up > 0 or down < 0 then --Not colliding. This is the easy part lol.
  41. return false
  42. end
  43.  
  44. if abs(left) < right then --Determine the collision on both axises? Axis'? I give up. On the x and y axis
  45. mtd.x = left
  46. else
  47. mtd.x = right
  48. end
  49.  
  50. if abs(up) < down then
  51. mtd.y = up
  52. else
  53. mtd.y = down
  54. end
  55. if abs(mtd.x) < abs(mtd.y) then
  56. mtd.y = 0
  57. else
  58. mtd.x = 0
  59. end
  60. return mtd
  61. end
  62.  
  63. local function specialentitycheck(v1, v2, v1name, v2name, dt, horizontalarg, verticalarg)
  64. --Lol nothing yet.
  65. end
  66.  
  67. function collisionCheck(v1, v2, v1name, v2name, dt)
  68. local should = true
  69. local collision
  70. local horizontal, vertical
  71. local cancollide = false
  72. if not collidingoptions[v1name] then
  73. cancollide = false
  74. else
  75. for i, v in pairs(collidingoptions[v1name]) do
  76. if v == v2name or v == "all" then
  77. cancollide = true
  78. break
  79. end
  80. end
  81. end
  82. if v1name == "block" then
  83. if v1.x == v2.x then
  84. return
  85. end
  86. end
  87. if cancollide then
  88. collision = aabb(v1.x, v1.y, v1.width, v1.height, v2.x, v2.y, v2.width, v2.height)
  89. end
  90.  
  91. local mtd
  92.  
  93. if collision then
  94. mtd = calculatemtd(v1, v2)
  95. end
  96.  
  97. if mtd then
  98. if mtd.x ~= 0 then
  99. horizontal = true
  100. end
  101. if mtd.y ~= 0 then
  102. vertical = true
  103. end
  104. end
  105. if horizontal or vertical then
  106. --horizontal, vertical = specialentitycheck(v1, v2, v1name, v2name, dt, horizontal, vertical)
  107. end
  108.  
  109. if horizontal then
  110. local speed = v1.xSpeed
  111. if not v2.passive then
  112. v1.xSpeed = 0 --HOLD UP
  113. if not v1.static then
  114. v1.x = v1.x + mtd.x
  115. end
  116. end
  117. if speed >= 0 then --Moving right
  118. if v1.rightcollide then
  119. v1:rightcollide(v2, v2name)
  120. end
  121. end
  122. if speed <= 0 then
  123. if v1.leftcollide then
  124. v1:leftcollide(v2, v2name)
  125. end
  126. end
  127. end
  128. if vertical then
  129. local speed = v1.ySpeed
  130. if not v2.passive then
  131. v1.ySpeed = 0
  132. if not v1.static then
  133. v1.y = v1.y + mtd.y
  134. end
  135.  
  136. if speed <= 0 then
  137. if v1.upcollide then
  138. v1:upcollide(v2, v2name)
  139. end
  140. end
  141. if speed > 0 then
  142. if v1.downcollide then
  143. v1:downcollide(v2, v2name)
  144. end
  145. end
  146. end
  147. end
  148.  
  149. return should
  150. end
  151.  
  152. function docollision() --Handles loops for physics. Lot less complicated than it seems.
  153. local lgamexscroll = gamexscroll
  154. local width = love.graphics.getWidth()
  155. local lobjects = objects
  156. for j, w in pairs(lobjects) do --
  157. for i, v in pairs(w) do -- Select first objects table
  158. if j ~= "block" then
  159. for name, lol in pairs(lobjects) do --Go through all objects names again
  160. for __, value in pairs(lol) do -- Goes through second objetcs table.
  161. if j ~= name then -- They dont need to collide with themselves.
  162. if value.x > lgamexscroll and value.x < value.x + value.width then
  163. collisionCheck(v, value, j, name, dt)
  164. end
  165. end
  166. end
  167. end
  168. end
  169. end
  170. end
  171. end
  172.  
  173. function loadphysics() --Deltatime Put in game, will load all the physics
  174. docollision() --Yay collision system that does physics for me
  175. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement