Custom ball physics clipping through ground

  1. What do you want to achieve?
    I’m trying to make a basic custom physics engine with balls that collide with parts

  2. What is the issue?
    The ball clips through the floor

  3. What solutions have you tried so far?
    I’ve tried decreasing gravity

image

-- Ball collider physics
-- ToldFable 2023

local physicsParts = workspace.Folder:GetChildren()

for t=1,200 do wait()
	for i,v in ipairs(physicsParts) do
		if not v:GetAttribute("ToldFable_Velocity") then
			v:SetAttribute("ToldFable_Velocity",Vector3.zero)
		end
		if not v:GetAttribute("ToldFable_Gravity") then
			v:SetAttribute("ToldFable_Gravity",Vector3.new(0,-0.0001,0))
		end
		local CustomGravity = v:GetAttribute("ToldFable_Gravity")
		local CustomVelocity = v:GetAttribute("ToldFable_Velocity")
		local Params = OverlapParams.new()
		Params.FilterType = Enum.RaycastFilterType.Blacklist
		Params.FilterDescendantsInstances = {v}
		local touchingParts = workspace:GetPartBoundsInRadius(v.Position,v.Size.Y,Params)
		local okToMove = true
		for i,v in ipairs(touchingParts) do
			if v.CanCollide then
				okToMove = false
			end
		end
		if okToMove then
			CustomVelocity += CustomGravity
			v.CFrame += CustomVelocity
		else
			CustomVelocity = Vector3.zero
		end
		v:SetAttribute("ToldFable_Velocity",CustomVelocity)
		--v:SetAttribute("ToldFable_Gravity",CustomGravity)
	end
end

This is for a studio plugin, so I’ve written the code to be easily executable via the command bar
I’m trying to keep this physics as bare-bones/small as possible

1 Like

The velocity of the part will still allow it to clip through the ground. It is technically okay to move as it is not colliding, but if you are 2 studs from the ground going -5 studs per physics update you will still go 3 studs into the ground. I suggest using some sort of raycast to limit yourself, or update the position if the part is heavily colliding.

2 Likes

oh yeah, I guess it’s moving by velocity and not gravity.
that makes sense

thanks hoofer

1 Like

22 posts were split to a new topic: Custom ball physics clipping through ground

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.