Advertisement
Xmaybeu

Custom Physics System

Apr 15th, 2025
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.94 KB | Source Code | 0 0
  1. --[[ This is the Physics Manager ]]
  2.  
  3. local RunService = game:GetService("RunService")
  4.  
  5. local constants = {
  6.     GRAVITY = 1,
  7.     AIR_RESISTANCE = 0.08
  8. }
  9.  
  10. local Objects = {}
  11.  
  12. function PrepareObject(Object: BasePart)
  13.     if not Object then return end
  14.    
  15.     print("Prepared "..Object.Name)
  16.    
  17.     Object:SetAttribute("Force", Vector3.new(0, 0, 0))
  18.    
  19.     if Object:HasTag("AlwaysIgnore") then
  20.         Object:SetAttribute("IgnorePhysics", true)
  21.         return
  22.     end
  23.    
  24.     Object:SetAttribute("IgnorePhysics", false)
  25. end
  26.  
  27. function Initialize()
  28.     local gameArray = game:GetDescendants()
  29.     if gameArray then
  30.         for _, item in ipairs(gameArray) do
  31.             if item:IsA("BasePart") and not item:IsA("Terrain") then
  32.                 PrepareObject(item)
  33.                 table.insert(Objects, item)
  34.             end
  35.         end
  36.     end
  37. end
  38.  
  39. function CalculatePosition(Object: BasePart, deltaTime: number) : Vector3
  40.     if not Object then return end
  41.    
  42.     local Force: Vector3 = Object:GetAttribute("Force")
  43.     local IgnorePhysics: boolean = Object:GetChildren("IgnorePhysics")
  44.    
  45.     if Force and IgnorePhysics then
  46.         if IgnorePhysics == true then
  47.             return Object.Position
  48.         end
  49.        
  50.         local newPosition = Object.Position + Force
  51.        
  52.         return newPosition
  53.     end
  54. end
  55.  
  56. function CalculateForce(Object: BasePart, deltaTime: number) : Vector3
  57.     if not Object then return end
  58.    
  59.     local Force = Object:GetAttribute("Force")
  60.    
  61.     if Force then
  62.         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))
  63.     end
  64. end
  65.  
  66. function InvertForce(Object: BasePart) : Vector3
  67.     local Force = Object:GetAttribute("Force")
  68.     if Force then
  69.         if Force.Y <= 0 then
  70.             if Force.Y < 1 and Force.Y > -1 then
  71.                 Force = Vector3.new(Force.X, 0, Force.Z)
  72.             end
  73.            
  74.             local newForce = Vector3.new(Force.X, -Force.Y / 2.5, Force.Z)
  75.            
  76.             return newForce
  77.         else
  78.             return Force
  79.         end
  80.     end
  81. end
  82.  
  83. function IsColliding(Object: BasePart) : boolean
  84.     local objectsColliding = Object:GetTouchingParts()
  85.     if #objectsColliding ~= 0 then
  86.         return true
  87.     else
  88.         return false
  89.     end
  90. end
  91.  
  92. function GetObjectsColliding(Object: BasePart): {Instance}
  93.     local objectsColliding = Object:GetTouchingParts()
  94.    
  95.     return objectsColliding
  96. end
  97.  
  98. function SetOutOfPart(Object: BasePart, CollidingObject: BasePart)
  99.     local O_Position = Object.Position
  100.     local CO_Position = CollidingObject.Position
  101.    
  102.     local O_Size = Object.Size
  103.     local CO_Size = CollidingObject.Size
  104.    
  105.    
  106. end
  107.  
  108. function UpdatePhysics(deltaTime: number)
  109.     for _, object in ipairs(Objects) do
  110.         if object:GetAttribute("IgnorePhysics") == false then
  111.             object.Position = CalculatePosition(object, deltaTime)
  112.             object:SetAttribute("Force", CalculateForce(object, deltaTime))
  113.            
  114.             local colliding = IsColliding(object)
  115.             if colliding == true then
  116.                 object:SetAttribute("Force", InvertForce(object))
  117.             end
  118.         end
  119.     end
  120. end
  121.  
  122. Initialize()
  123.  
  124. RunService.Heartbeat:Connect(UpdatePhysics)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement