Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
- local Components = require(ReplicatedStorage.Shared.Components)
- local RotateVector2 = require(ReplicatedStorage.Shared.Utils.RotateVector2)
- local GetEntityGlobalPosition = require(ReplicatedStorage.Shared.Utils.GetEntityGlobalPosition)
- local GetEntityGlobalRotation = require(ReplicatedStorage.Shared.Utils.GetEntityGlobalRotation)
- local GetEntityGlobalScale = require(ReplicatedStorage.Shared.Utils.GetEntityGlobalScale)
- local GetEntityCanvas = require(ReplicatedStorage.Shared.Utils.GetEntityCanvas)
- local ApplyVelocity = require(ReplicatedStorage.Shared.Systems.ApplyVelocity)
- local CheckCollisions = require(ReplicatedStorage.Shared.Systems.CheckCollisions)
- local function rectangleCollisionResponse(
- pos1,
- size1,
- pos2,
- size2
- )
- local topLeft1 = pos1 - size1 / 2
- local topLeft2 = pos2 - size2 / 2
- local dx = (topLeft1.X + size1.X / 2) - (topLeft2.X + size2.X / 2)
- local dy = (topLeft1.Y + size1.Y / 2) - (topLeft2.Y + size2.Y / 2)
- local combinedHalfWidths = size1.X / 2 + size2.X / 2
- local combinedHalfHeights = size1.Y / 2 + size2.Y / 2
- local overlapX = combinedHalfWidths - math.abs(dx)
- local overlapY = combinedHalfHeights - math.abs(dy)
- local x1, y1 = topLeft1.X, topLeft1.Y
- if overlapX > 0 and overlapY > 0 then
- if overlapX < overlapY then
- if dx > 0 then
- x1 += overlapX
- else
- x1 -= overlapX
- end
- else
- if dy > 0 then
- y1 += overlapY
- else
- y1 -= overlapY
- end
- end
- end
- return Vector2.new(x1, y1) + size1 / 2
- end
- local function globalToLocal(id, position, world)
- local parentRotation = 0
- local parentPosition = Vector2.zero
- local parentScale = Vector2.one
- local child = world:get(id, Components.Child)
- if child and world:contains(child.Parent) then
- parentPosition = GetEntityGlobalPosition(child.Parent, world)
- parentRotation = GetEntityGlobalRotation(child.Parent, world)
- parentScale = GetEntityGlobalScale(child.Parent, world)
- end
- return RotateVector2(position - parentPosition, -math.rad(parentRotation)) / parentScale
- end
- local function RespondToCollisions(world, state)
- for id1, transform1, collider1, colliding1 in world:query(
- Components.Transform,
- Components.Collider,
- Components.Colliding,
- Components.Physics
- ):without(Components.NonShared)
- do
- local globalPosition1 = GetEntityGlobalPosition(id1, world)
- local globalSize1 = GetEntityGlobalScale(id1, world) * collider1.Size
- for _, id2 in colliding1.CollidingWith do
- local trigger2 = world:get(id2, Components.Trigger)
- if trigger2 then
- continue
- end
- local collider2 = world:get(id2, Components.Collider)
- if not collider2 then
- continue
- end
- local transform2 = world:get(id2, Components.Transform)
- if not transform2 then
- continue
- end
- local globalPosition2 = GetEntityGlobalPosition(id2, world)
- local globalSize2 = GetEntityGlobalScale(id2, world) * collider2.Size
- globalPosition1 = rectangleCollisionResponse(
- globalPosition1,
- globalSize1,
- globalPosition2,
- globalSize2
- )
- end
- world:insert(id1, transform1:patch({
- Position = globalToLocal(id1, globalPosition1, world)
- }))
- end
- end
- return {
- system = RespondToCollisions,
- after = {
- ApplyVelocity,
- CheckCollisions,
- },
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement