Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ This is the Physics Manager ]]
- local RunService = game:GetService("RunService")
- local constants = {
- GRAVITY = 1,
- AIR_RESISTANCE = 0.08
- }
- local Objects = {}
- function PrepareObject(Object: BasePart)
- if not Object then return end
- print("Prepared "..Object.Name)
- Object:SetAttribute("Force", Vector3.new(0, 0, 0))
- if Object:HasTag("AlwaysIgnore") then
- Object:SetAttribute("IgnorePhysics", true)
- return
- end
- Object:SetAttribute("IgnorePhysics", false)
- end
- function Initialize()
- local gameArray = game:GetDescendants()
- if gameArray then
- for _, item in ipairs(gameArray) do
- if item:IsA("BasePart") and not item:IsA("Terrain") then
- PrepareObject(item)
- table.insert(Objects, item)
- end
- end
- end
- end
- function CalculatePosition(Object: BasePart, deltaTime: number) : Vector3
- if not Object then return end
- local Force: Vector3 = Object:GetAttribute("Force")
- local IgnorePhysics: boolean = Object:GetChildren("IgnorePhysics")
- if Force and IgnorePhysics then
- if IgnorePhysics == true then
- return Object.Position
- end
- local newPosition = Object.Position + Force
- return newPosition
- end
- end
- function CalculateForce(Object: BasePart, deltaTime: number) : Vector3
- if not Object then return end
- local Force = Object:GetAttribute("Force")
- if Force then
- return Force + Vector3.new(constants.AIR_RESISTANCE * (math.pow(Force.X, 2) * deltaTime), (Force.Y - constants.GRAVITY) * deltaTime, constants.AIR_RESISTANCE * (math.pow(Force.Z, 2) * deltaTime))
- end
- end
- function InvertForce(Object: BasePart) : Vector3
- local Force = Object:GetAttribute("Force")
- if Force then
- if Force.Y <= 0 then
- if Force.Y < 1 and Force.Y > -1 then
- Force = Vector3.new(Force.X, 0, Force.Z)
- end
- local newForce = Vector3.new(Force.X, -Force.Y / 2.5, Force.Z)
- return newForce
- else
- return Force
- end
- end
- end
- function IsColliding(Object: BasePart) : boolean
- local objectsColliding = Object:GetTouchingParts()
- if #objectsColliding ~= 0 then
- return true
- else
- return false
- end
- end
- function GetObjectsColliding(Object: BasePart): {Instance}
- local objectsColliding = Object:GetTouchingParts()
- return objectsColliding
- end
- function SetOutOfPart(Object: BasePart, CollidingObject: BasePart)
- local O_Position = Object.Position
- local CO_Position = CollidingObject.Position
- local O_Size = Object.Size
- local CO_Size = CollidingObject.Size
- end
- function UpdatePhysics(deltaTime: number)
- for _, object in ipairs(Objects) do
- if object:GetAttribute("IgnorePhysics") == false then
- object.Position = CalculatePosition(object, deltaTime)
- object:SetAttribute("Force", CalculateForce(object, deltaTime))
- local colliding = IsColliding(object)
- if colliding == true then
- object:SetAttribute("Force", InvertForce(object))
- end
- end
- end
- end
- Initialize()
- RunService.Heartbeat:Connect(UpdatePhysics)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement