Advertisement
AlewAlow

help

Apr 15th, 2024 (edited)
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  2. local UserInputService = game:GetService("UserInputService")
  3.  
  4. local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
  5. local Components = require(ReplicatedStorage.Shared.Components)
  6. local Constants = require(ReplicatedStorage.Shared.Constants)
  7.  
  8. local GetEntityGlobalScale = require(ReplicatedStorage.Shared.Utils.GetEntityGlobalScale)
  9. local GetEntityGlobalPosition = require(ReplicatedStorage.Shared.Utils.GetEntityGlobalPosition)
  10. local GetEntityDescendants = require(ReplicatedStorage.Shared.Utils.GetEntityDescendants)
  11.  
  12. local function FireShapecast(origin, direction, size, entities, world)
  13.     if type(entities) == "number" then
  14.         entities = GetEntityDescendants(entities, world)
  15.     end
  16.    
  17.     local invDirection = 1 / direction
  18.  
  19.     local closestHit = nil
  20.     local closestTime = math.huge
  21.     local closestNormal = Vector2.new()
  22.     local closestPosition = Vector2.new()
  23.  
  24.     for _, id in pairs(entities) do
  25.         local transform = world:get(id, Components.Transform)
  26.         if not transform then
  27.             continue
  28.         end
  29.  
  30.         local collider = world:get(id, Components.Collider)
  31.         if not collider then
  32.             continue
  33.         end
  34.  
  35.         local globalPosition = GetEntityGlobalPosition(id, world)
  36.         local globalSize = GetEntityGlobalScale(id, world) * collider.Size
  37.  
  38.         local topleft = globalPosition - globalSize / 2
  39.         local botright = globalPosition + globalSize / 2
  40.  
  41.         local tEnter = (topleft - origin) * invDirection
  42.         local tExit = (botright - origin) * invDirection
  43.  
  44.         local tMin = Vector2.new(math.min(tEnter.X, tExit.X), math.min(tEnter.Y, tExit.Y))
  45.         local tMax = Vector2.new(math.max(tEnter.X, tExit.X), math.max(tEnter.Y, tExit.Y))
  46.  
  47.         if tMax.X >= tMin.Y and tMax.Y >= tMin.X then
  48.             local collisionTime = math.max(tMin.X, tMin.Y)
  49.             local collisionPoint = origin + collisionTime * direction
  50.             local collisionNormal = Vector2.new(
  51.                 tMin.X > tMin.Y and -1 or 1,
  52.                 tMin.X > tMin.Y and 0 or 1
  53.             )
  54.  
  55.             if collisionTime < closestTime then
  56.                 closestNormal = collisionNormal
  57.                 closestPosition = collisionPoint
  58.                 closestTime = collisionTime
  59.                 closestHit = id
  60.             end
  61.         end
  62.     end
  63.  
  64.     if closestTime < math.huge then
  65.         return {
  66.             Hit = closestHit,
  67.             Position = closestPosition,
  68.             Normal = closestNormal,
  69.         }
  70.     end
  71.  
  72.     return false
  73. end
  74.  
  75.  
  76.  
  77. return FireShapecast
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement