Advertisement
AlewAlow

returns a wrong normal

Apr 15th, 2024
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 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 closestHit = nil
  18.     local closestTime = math.huge
  19.     local closestNormal = Vector2.new()
  20.     local closestPosition = Vector2.new()
  21.  
  22.     local invDirection = 1 / direction
  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.max(math.min(tEnter.X, tExit.X), 0), math.max(math.min(tEnter.Y, tExit.Y), 0))
  45.         local tMax = Vector2.new(math.min(math.max(tEnter.X, tExit.X), 1), math.min(math.max(tEnter.Y, tExit.Y), 1))
  46.  
  47.         if tMax.X < tMin.Y or tMax.Y < tMin.X then
  48.             continue
  49.         end
  50.  
  51.         local collisionTime = math.max(tMin.X, tMin.Y)
  52.         if collisionTime >= closestTime then
  53.             continue
  54.         end
  55.  
  56.         local collisionPoint = origin + collisionTime * direction
  57.         local collisionNormal
  58.  
  59.         if tMax.X > tMin.Y then
  60.             if tEnter.X < tExit.X then
  61.                 collisionNormal = Vector2.new(-1, 0)
  62.             else
  63.                 collisionNormal = Vector2.new(1, 0)
  64.             end
  65.         else
  66.             if tEnter.Y < tExit.Y then
  67.                 collisionNormal = Vector2.new(0, -1)
  68.             else
  69.                 collisionNormal = Vector2.new(0, 1)
  70.             end
  71.         end
  72.  
  73.         closestNormal = collisionNormal
  74.         closestPosition = collisionPoint
  75.         closestTime = collisionTime
  76.         closestHit = id
  77.     end
  78.  
  79.     if closestTime < math.huge then
  80.         return {
  81.             Hit = closestHit,
  82.             Position = closestPosition,
  83.             Normal = closestNormal,
  84.         }
  85.     end
  86.  
  87.     return false
  88. end
  89.  
  90.  
  91.  
  92. return FireShapecast
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement