Hello, I am trying to create a custom physics system, however, the ball does not slide/roll very far and stops very quickly:
code:
function Main:CalculateShots(playerwhoshot : Player, typeofshot : string, charge : number,typee) : {Vector3}
local settings : NewSettings = self.Settings
if typeofshot ~= "shootend" then
return
end
local ball = game.Workspace:FindFirstChild("Ball")
if not ball and ball.Thrown.Value and ball.Player.Value ~= playerwhoshot.Name then return end
local CurrentPosition = (typee == "first" and playerwhoshot.Character.HumanoidRootPart.Position) or (typee == "10" and lastknownposition)
local class = playerwhoshot.Class.Value
local BaseForce = (class == "Duck" and 15) or (class == "Fat Bird" and 20) or (class == "Bird" and 25)
local Direction = ball.CFrame.RightVector
if typee == "first" then
lastknownposition = nil
lastknownvelocity = nil
end
local Velocity : Vector3 = (typee == "first" and Direction * (charge * BaseForce) / settings.Normalize) or (typee == "10" and lastknownvelocity)
local positions = {}
local MaxBouncesCondition = (settings.MaxBounces == "BounceUntilFinished") and math.huge or settings.MaxBounces -- if bounce till finished then itll bounce till velocity is gone
local SlidingFriction = 0.99
for i = 1, 10 do
local NextPosition = CurrentPosition + Velocity * settings.TimeStep
if Velocity.Magnitude > 0 then
local raycastparams = RaycastParams.new()
raycastparams.FilterType = Enum.RaycastFilterType.Exclude
raycastparams.FilterDescendantsInstances = {ball, playerwhoshot.Character}
table.insert(positions, CurrentPosition)
Velocity = Velocity + Vector3.new(0, -game.Workspace.Gravity*0.28, 0) * settings.TimeStep
local RaycastResult = workspace:Raycast(CurrentPosition, NextPosition - CurrentPosition, raycastparams)
if RaycastResult then
local normal = RaycastResult.Normal
if math.abs(normal:Dot(Velocity)) < 1 then -- Check if the ball is sliding (velocity parallel to normal)
Velocity = Velocity * SlidingFriction -- Apply sliding friction
print("sliding")
else
Velocity = Velocity - 2 * Velocity:Dot(normal) * normal -- this reflects the velocity aka bouncing
Velocity = Velocity * settings.BounceDamping
end
NextPosition = RaycastResult.Position + RaycastResult.Normal*0.0000001
if math.abs(normal:Dot(Velocity)) >= 0.3 then -- Check if the ball bounced
self.Bounces = self.Bounces + 1
end
end
CurrentPosition = NextPosition
self.CurrentTime = self.CurrentTime + settings.TimeStep
end
end
lastknownposition = CurrentPosition
lastknownvelocity = Velocity
return positions
end