Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local UserInputService = game:GetService("UserInputService")
- local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
- local Components = require(ReplicatedStorage.Shared.Components)
- local Constants = require(ReplicatedStorage.Shared.Constants)
- local GetEntityGlobalScale = require(ReplicatedStorage.Shared.Utils.GetEntityGlobalScale)
- local GetEntityGlobalPosition = require(ReplicatedStorage.Shared.Utils.GetEntityGlobalPosition)
- local GetEntityDescendants = require(ReplicatedStorage.Shared.Utils.GetEntityDescendants)
- local function FireShapecast(origin, direction, size, entities, world)
- if type(entities) == "number" then
- entities = GetEntityDescendants(entities, world)
- end
- local closestHit = nil
- local closestTime = math.huge
- local closestNormal = Vector2.new()
- local closestPosition = Vector2.new()
- local invDirection = 1 / direction
- for _, id in pairs(entities) do
- local transform = world:get(id, Components.Transform)
- if not transform then
- continue
- end
- local collider = world:get(id, Components.Collider)
- if not collider then
- continue
- end
- local globalPosition = GetEntityGlobalPosition(id, world)
- local globalSize = GetEntityGlobalScale(id, world) * collider.Size
- local topleft = globalPosition - globalSize / 2
- local botright = globalPosition + globalSize / 2
- local tEnter = (topleft - origin) * invDirection
- local tExit = (botright - origin) * invDirection
- local tMin = Vector2.new(math.max(math.min(tEnter.X, tExit.X), 0), math.max(math.min(tEnter.Y, tExit.Y), 0))
- local tMax = Vector2.new(math.min(math.max(tEnter.X, tExit.X), 1), math.min(math.max(tEnter.Y, tExit.Y), 1))
- if tMax.X < tMin.Y or tMax.Y < tMin.X then
- continue
- end
- local collisionTime = math.max(tMin.X, tMin.Y)
- if collisionTime >= closestTime then
- continue
- end
- local collisionPoint = origin + collisionTime * direction
- local collisionNormal
- if tMax.X > tMin.Y then
- if tEnter.X < tExit.X then
- collisionNormal = Vector2.new(-1, 0)
- else
- collisionNormal = Vector2.new(1, 0)
- end
- else
- if tEnter.Y < tExit.Y then
- collisionNormal = Vector2.new(0, -1)
- else
- collisionNormal = Vector2.new(0, 1)
- end
- end
- closestNormal = collisionNormal
- closestPosition = collisionPoint
- closestTime = collisionTime
- closestHit = id
- end
- if closestTime < math.huge then
- return {
- Hit = closestHit,
- Position = closestPosition,
- Normal = closestNormal,
- }
- end
- return false
- end
- return FireShapecast
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement