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)