Advertisement
killerbrenden

2D Collision Detection

Nov 19th, 2020
1,141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.50 KB | None | 0 0
  1. local world = {}
  2. world.__index = world
  3.  
  4. local runService = game:GetService("RunService")
  5.  
  6. local function getPoints(Obj)
  7.     local top = Obj.Position + UDim2.new(0,0,0,Obj.Size.Y.Offset/2)
  8.     local bottom = Obj.Position - UDim2.new(0,0,0,Obj.Size.Y.Offset/2)
  9.     local left = Obj.Position - UDim2.new(0,Obj.Size.X.Offset/2,0,0)
  10.     local right = Obj.Position + UDim2.new(0,Obj.Size.X.Offset/2,0,0)
  11.    
  12.     return {
  13.         ["Top"] = top,
  14.         ["Bottom"] = bottom,
  15.         ["Left"] = left,
  16.         ["Right"] = right
  17.     }
  18. end
  19.  
  20. function world.new()
  21.     return setmetatable({
  22.         _Thread = nil,
  23.         _World = {},
  24.         _Yield = false,
  25.         Colliding = {}
  26.     },world)
  27. end
  28.  
  29. function world:GetObjects()
  30.     return self._World
  31. end
  32.  
  33. function world:GetCollisions()
  34.     return self.Colliding
  35. end
  36.  
  37. function world:Pause()
  38.     self._Yield = true
  39. end
  40.  
  41. function world:Run()
  42.     self._Yield = false
  43.    
  44.     if not self._Thread then
  45.         self._Thread = coroutine.create(function()
  46.             runService.RenderStepped:Connect(function()
  47.                 if self._Yield then
  48.                     coroutine.yield()
  49.                 end
  50.                
  51.                 for _,obj in pairs(self._World) do
  52.                     if not self.Colliding[obj] then
  53.                         self.Colliding[obj] = {}
  54.                     end
  55.                    
  56.                     for _,anObj in pairs(self._World) do
  57.                         if anObj ~= obj then
  58.                             local oP = getPoints(obj)
  59.                             local aOP = getPoints(anObj)
  60.                            
  61.                             if aOP.Top.Y.Offset >= oP.Bottom.Y.Offset and aOP.Top.Y.Offset <= oP.Top.Y.Offset or aOP.Bottom.Y.Offset <= oP.Bottom.Y.Offset and aOP.Bottom.Y.Offset >= oP.Top.Y.Offset then
  62.                                 if aOP.Right.X.Offset >= oP.Left.X.Offset and aOP.Right.X.Offset <= oP.Right.X.Offset or aOP.Left.X.Offset <= oP.Right.X.Offset and aOP.Left.X.Offset >= oP.Left.X.Offset then
  63.                                     if not table.find(self.Colliding[obj], anObj) then
  64.                                         table.insert(self.Colliding[obj], anObj)
  65.                                     end
  66.                                 end
  67.                             end
  68.                         end
  69.                     end
  70.                 end
  71.             end)
  72.         end)
  73.     end
  74.    
  75.     coroutine.resume(self._Thread)
  76. end
  77.  
  78. function world:RemoveObject(obj)
  79.     local pos = table.find(self._World, obj)
  80.     table.remove(self._World, pos)
  81.     obj:Destroy()
  82. end
  83.  
  84. local object = {}
  85. object.__index = object
  86.  
  87. function world:AddObject(Size)
  88.     local obj = Instance.new("ImageLabel")
  89.     obj.Size = Size
  90.     obj.AnchorPoint = Vector2.new(0.5,0.5)
  91.     obj.Name = "Object"..#self._World+1
  92.    
  93.     self._World[#self._World+1] = obj
  94.    
  95.     return setmetatable({
  96.         Object = obj
  97.     },object)
  98. end
  99.  
  100. function object:Destroy()
  101.     local pos = table.find(self._World, self.Object)
  102.     table.remove(self._World, pos)
  103.     self.Object:Destroy()
  104.     self.Object = nil
  105. end
  106.  
  107. return world
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement