Advertisement
AlewAlow

uwu

Apr 18th, 2024 (edited)
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.40 KB | None | 0 0
  1. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  2.  
  3. local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
  4. local Components = require(ReplicatedStorage.Shared.Components)
  5.  
  6. local RotateVector2 = require(ReplicatedStorage.Shared.Utils.RotateVector2)
  7. local GetEntityGlobalPosition = require(ReplicatedStorage.Shared.Utils.GetEntityGlobalPosition)
  8. local GetEntityGlobalRotation = require(ReplicatedStorage.Shared.Utils.GetEntityGlobalRotation)
  9. local GetEntityGlobalScale = require(ReplicatedStorage.Shared.Utils.GetEntityGlobalScale)
  10. local GetEntityCanvas = require(ReplicatedStorage.Shared.Utils.GetEntityCanvas)
  11.  
  12. local ApplyVelocity = require(ReplicatedStorage.Shared.Systems.ApplyVelocity)
  13. local CheckCollisions = require(ReplicatedStorage.Shared.Systems.CheckCollisions)
  14.  
  15. local function rectangleCollisionResponse(
  16.     pos1,
  17.     size1,
  18.     pos2,
  19.     size2
  20. )
  21.     local topLeft1 = pos1 - size1 / 2
  22.     local topLeft2 = pos2 - size2 / 2
  23.    
  24.     local dx = (topLeft1.X + size1.X / 2) - (topLeft2.X + size2.X / 2)
  25.     local dy = (topLeft1.Y + size1.Y / 2) - (topLeft2.Y + size2.Y / 2)
  26.    
  27.     local combinedHalfWidths = size1.X / 2 + size2.X / 2
  28.     local combinedHalfHeights = size1.Y / 2 + size2.Y / 2
  29.    
  30.     local overlapX = combinedHalfWidths - math.abs(dx)
  31.     local overlapY = combinedHalfHeights - math.abs(dy)
  32.  
  33.     local x1, y1 = topLeft1.X, topLeft1.Y
  34.  
  35.     if overlapX > 0 and overlapY > 0 then
  36.         if overlapX < overlapY then
  37.             if dx > 0 then
  38.                 x1 += overlapX
  39.             else
  40.                 x1 -= overlapX
  41.             end
  42.         else
  43.             if dy > 0 then
  44.                 y1 += overlapY
  45.             else
  46.                 y1 -= overlapY
  47.             end
  48.         end
  49.     end
  50.    
  51.     return Vector2.new(x1, y1) + size1 / 2
  52. end
  53.  
  54. local function globalToLocal(id, position, world)
  55.     local parentRotation = 0
  56.     local parentPosition = Vector2.zero
  57.     local parentScale = Vector2.one
  58.    
  59.     local child = world:get(id, Components.Child)
  60.     if child and world:contains(child.Parent) then
  61.         parentPosition = GetEntityGlobalPosition(child.Parent, world)
  62.         parentRotation = GetEntityGlobalRotation(child.Parent, world)
  63.         parentScale = GetEntityGlobalScale(child.Parent, world)
  64.     end
  65.    
  66.     return RotateVector2(position - parentPosition, -math.rad(parentRotation)) / parentScale
  67. end
  68.  
  69. local function RespondToCollisions(world, state)
  70.     for id1, transform1, collider1, colliding1 in world:query(
  71.             Components.Transform,
  72.             Components.Collider,
  73.             Components.Colliding,
  74.             Components.Physics
  75.         ):without(Components.NonShared)
  76.     do
  77.         local globalPosition1 = GetEntityGlobalPosition(id1, world)
  78.         local globalSize1 = GetEntityGlobalScale(id1, world) * collider1.Size
  79.        
  80.         for _, id2 in colliding1.CollidingWith do
  81.             local trigger2 = world:get(id2, Components.Trigger)
  82.             if trigger2 then
  83.                 continue
  84.             end
  85.            
  86.             local collider2 = world:get(id2, Components.Collider)
  87.             if not collider2 then
  88.                 continue
  89.             end
  90.            
  91.             local transform2 = world:get(id2, Components.Transform)
  92.             if not transform2 then
  93.                 continue
  94.             end
  95.            
  96.             local globalPosition2 = GetEntityGlobalPosition(id2, world)
  97.             local globalSize2 = GetEntityGlobalScale(id2, world) * collider2.Size
  98.            
  99.             globalPosition1 = rectangleCollisionResponse(
  100.                 globalPosition1,
  101.                 globalSize1,
  102.                
  103.                 globalPosition2,
  104.                 globalSize2
  105.             )
  106.         end
  107.        
  108.         world:insert(id1, transform1:patch({
  109.             Position = globalToLocal(id1, globalPosition1, world)
  110.         }))
  111.     end
  112. end
  113.  
  114. return {
  115.     system = RespondToCollisions,
  116.     after = {
  117.         ApplyVelocity,
  118.         CheckCollisions,
  119.     },
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement