Help with keeping ray casted ball at resting point over floor

Hello, so I am trying to create my own " basic"physics system in order to be able to do many other things with it. However, I am struggling to solve this puzzle on how I can maintain the ball on top of the floor once it starts reaching its resting state.

Theory:
So I was initially thinking to create velocity, accelerating, and bounce physics through the help of raycasting. Where I would use a part as a model as the raycasting points are invisible. Where I completed the part of starting the movement and reflecting the rays. However I am stuck in solving how I can keep the ball on top of the floor once it is close to a velocity speed of 0.
I’m wondering why it keeps falling through it.

Here is a video to showcase the problem:

Here is the code that got is started:

local RaycastParameters = RaycastParams.new()
RaycastParameters.FilterType = Enum.RaycastFilterType.Exclude
RaycastParameters.FilterDescendantsInstances = {part}

function reflect(d:Vector3, n:Vector3) --currentNormal, objectNormal)
	return d - ( 2 * ( d:Dot(n) ) * n )
end

local Origin = part.CFrame
local InitialVelocity = Vector3.new(0,0,0)
local Gravity = Vector3.new(0, -workspace.Gravity, 0)  --> -196.2
local coeffRest = .75

local CurrentPosition = Origin.Position
local CurrentVelocity = InitialVelocity
local CurrentNormal = InitialVelocity.Unit

local Connection;
Connection = RunService.Heartbeat:Connect(function(delta)
	local PredictedDirection = CurrentPosition + ( CurrentVelocity * delta + 0.5 * Gravity * delta * delta)
	local direction = PredictedDirection - CurrentPosition
	local raycast = workspace:Raycast(CurrentPosition,direction,RaycastParameters)
	
	local Intersection = raycast and raycast.Position or PredictedDirection
	local Distance = (CurrentPosition - Intersection).Magnitude
	
	CurrentPosition = Intersection
	
	if raycast then
		local RayNormal = raycast.Normal
		local Reflection = reflect(CurrentNormal,RayNormal)
		CurrentNormal = Reflection
		CurrentVelocity = Reflection * (CurrentVelocity.Magnitude * coeffRest)
		 part.CFrame = CFrame.lookAlong(Intersection, CurrentNormal)
	else
		CurrentNormal = direction.Unit
		CurrentVelocity = CurrentVelocity + Gravity * delta
		 part.CFrame = CFrame.lookAlong(CurrentPosition, direction)
	end
		
end)

1 Like

I am not sure if this changes anything but I am not using any change in mass acceleration for the equation and also the ball is being dropped at a point of Vector3.new(25,25,25)

Are you trying to make it just bounce and control it’s movement on the x and z? You don’t have to use all of that math, just using some constraints you can get the job done.

If you want to keep the ball from moving when not desired, you can either just tween it and anchor it, or using a constraint like AlignPosition you can keep it at the place you want.

Thanks for answering but I am trying to refrain from using any sort of constraints as I am I trying to create my own ball physics while only using ray-casting. As my only problem is trying to get the ball to stay from falling through the floor once it gets closer to a velocity of 0 studs.

I can’t seem to get the code you sent to work so I can’t give any verified solution, but maybe it’s that you’re raycasting from a point: so when that point’s height is parallel to the surface it’s attempting to land on, the raycast can no longer intersect with the surface, thusly leading to it passing through?

If its velocity is near zero, you could also put it to sleep to help with performance & possibly mitigate the parallel-raycasting issue, assuming that is the problem of course.

edit: perhaps using sphere casting or spatial query could help

The script should be working I made a slight edit just in case you copied and pasted it so it wouldn’t have any errors. However, I believe it may be a problem with the delta time once you test it, as it makes the ball fall incredibly fast due to the high delta time. So I suggest you add a wait or yield time on the script as the first line or so.

Also, I am not sure how I can make it go to sleep and to mitigate the parallel-ray casting issue like you said if it’s the problem. As I’ve been stuck on it for a while.

– Also I have done some tests and I have found that it starts to fall through the floor once the velocity gets to less than around 0.2