Is there anyway I can fix this roblox physics glitch? (Balls passing through terrain)

I have a golf game which uses roblox physics. After a long time I noticed the game had a rare glitch where the ball just noclips through the terrain and gets stuck there, preventing the player from completing the courses:

I am almost sure that’s not my code’s fault, as I don’t directly change the ball’s trajectory, just its physicalproperties and a bodyforce that simulates wind. My guess is that roblox physics aren’t that good at handling high speed, small projectiles.

I already tried some things, like decreasing gravity, lowering the ball’s velocity and increasing its size, all of this so it doesn’t go that fast. That helped a bit making that glitch happen less frequently, but it still happens and I don’t want to make a golf game where you have to use a bowling ball so it doesn’t pass objects.

Here’s a sample of the code and, without even messing properties, the ball still goes through the terrain. (You will have to put it inside a part named “Cannon”. I aim the cannon about 60° from the ground. Also make a collisongroup named “Ball”, which won’t collide with itself)

local debris = game:GetService("Debris")
local runService = game:GetService("RunService")
local newBall = Instance.new("Part")
newBall.CollisionGroupId = 1
newBall.Shape = "Ball"
newBall.Size = Vector3.new(1,1,1)
newBall.CustomPhysicalProperties = PhysicalProperties.new(5,.4,.17,1,1)
newBall.Name = "CannonBall"
local gyro = Instance.new("BodyGyro")
gyro.MaxTorque=Vector3.new(200,200,200)
gyro.P = 100
gyro.Parent = newBall

while true do
	wait(.1)
	local ball = newBall:Clone()
	ball.Parent = workspace
	ball.CFrame = workspace.Cannon.CFrame
	ball.Velocity = (workspace.Cannon.CFrame.RightVector*-180)+(workspace.Cannon.CFrame.LookVector*math.random(-5,5))+(workspace.Cannon.CFrame.UpVector*math.random(-5,5))
	debris:AddItem(ball,15)
end
1 Like

Maybe u could use raycasting to check if it goes through the terrain and if it does then put it back

1 Like

Increase the size of the ball or reduce the velocity so it has time to properly detect the collision.
Edit: My fault I failed to read all the way through your topic post. I see you’ve tried this.

1 Like

This happens when the cpu actually skips the cycle ahead-

It’s kinda complicated. This usually happens when ur computer “skips” one or two physics steps ahead, and if those steps have the collisions it’ll be missed. Not much we can do about it.

As R41 said that’s probably the only way to do this, roblox isn’t good at handling high speed projectiles. U could reduce the speed down but u can’t do it in ur case. Good luck :+1:.

1 Like

You might also be able to use a hidden part that is much larger. When that part touches the terrain you could reduce its velocity or set the part to non-collide after the first terrain hit. Hacky a bit but may work.

So the server is also prone of doing this cycle skip, and not just the client?
Also if I were to raycast to check and return the ball when it goes through the terrain, wouldnt it overload the server and therefore make it even more likely for the ball to glitch like this?
I’ll probably check at the end of the shot if the ball is inside the terrain. If it is, the strike doesnt count and the ball goes back to where it was before, like an Out of Bounds zone but not punishing the player’s score.
EDIT: I discovered that raycasts dont collide with terrain when it is made from the interior to the exterior, so i just need to raycast downards and It will tell me nothing is below the ball, which means it’s stuck on a terrain.

I’m guessing this is a Local script right?

No, it is a server script. And on the original code it is too

I have a feeling the CollisionGroup is the main suspect in this glitch, however i’m not sure.

I don’t think it is. This is a collision detection issue and there is nothing that can be done by us. The only solution is to increase the collision detection ability by reducing the velocity or increasing size or both or some sort of correction algorithm that detects the object has passed a certain threshold in which case you manually correct the position.

Tbh what you said is kind true, if you were to increase the velocity of an Object there’s a higher chance of the actual objects collision breaking. You can actually test this with your character trying making your WalkSpeed >10k, if your run really hard into something there’s a chance you might just go right through it.

Yeah I have experienced this on a minecraft-like project i was making too. There was a textbar where you can type your speed, and if i put something too high it just phases through the terrain cliffs and walls.

  1. Yes, both servers and clients are prone to this.

  2. No, developers can do upto 10k raycasts every heartbeat with no performance drops, raycasts use an extremely smal amount of resources to such an extent that it does not affect performance at all, big games like jailbreak rely on raycasts every heartbeat for their car suspensions. Raycast is the best method for u rn other than moving to unreal or unity.

1 Like

Oh, then I guess that could be the way. It’s just that I already have some loops for checking if the ball is on the ground (so i can change the ball’s friction depending on the material the raycast touches and also apply wind when nothing collides) and using more on this code i think it might also change the expected trajectory or overload it, cuz it does have a high rate and activity already.
I guess I’ll keep with the method I said for now and start testing yours, which will be way better if it works properly.

1 Like