Hello, so I am in the process of creating my own physics-esk thing if you can eve call it that
Anyways, it’s going great… only problem is well I have gravity and I make the ball bounce when it gets in contact with the ground but It just keeps bouncing…
You don’t have any force being lost on the bounce, so it will always have the same amount of energy. Normally, some energy would be lost after each bounce, but you just have it be a Constant amount of 20.
to add to what @dragonvale5555 said, look up coefficient of restitution. its the property of an object which determines the ratio of how much force will be lost on contact from 0-1. basically if it has 0, none of its momentum is preserved, it’ll just drop and stay like a rock, if its at 1, it’s perfectly elastic and will preserve all of its momentum
With the way that you currently have it set up, you will just need to multiply the bounceForce by a number that gets lower and lower each bounce until it gets to 0. If you were to change it up a little, you could have something to manage the total energy of the ball, and each time that it hits the ground, multiply it by the “coefficient of restitution” as @hotpocket285 sugessted.
Alright so I implemented a maxBounceHeight and I slowly decreased that and it worked… problem is that once I go to bounce it again it falls through the floor im guessing cause it has no more energy?
is that you are actually subtracting the maxBounceHeight every frame that it is not hitting something. What I meant was every time it does bounce, you will multiply the maxBounceHeight by some arbitrary value that is 1 or less. Something like
if raycast then
...
maxBounceHeight *= someArbitraryValue
...
There is never a time when the maxBounceHeight is reset. You will need to reset it every time it reaches 0, and the ball gets moved. IDK how your code is set up, so I don’t know the best way to implement it.
If you would like, I can try and make my own system using real physics values and it will be easily changeable. I am pretty good at physics, so it shouldn’t be too hard.
what me and @dragonvale5555 are trying to say is, your current code has no dependant variables, its all independent. no matter how many times the ball bounces, its bounceForce will always be V3.new(0,20,0). make that a variable, and every time it bounces, multiply it by a coefficient of restitution (CoR). so say its .95, it has a 95% ability to retain velocity on impact. take bounceForce and multiply it by your CoR so it lowers. as it continues bouncing and loses more and more of the bounce force it’ll naturally stop bouncing.
so on the line in the image
bounceForce = Vector3.new(0,20,0)
change the 20 to a variable like “retainedForce” or something, and do something like
retainedForce *= .95 -- .95 is the hypothetical CoR
bounceForce = Vector3.new(0,retainedForce,0)
aight’, so i’ve been trying to visualize a lot of how this would work, so i coded it myself to get the hands on approach xD
this is my code if you wanna see how i managed to do it, mine takes directions into account so it can bounce off of slopes too (didn’t take friction into account so it’ll literally just bounce into the direction of the slope instead of sliding down it a little)
local rs = game["Run Service"]
local plr = game.Players.LocalPlayer
local ball = workspace.Part
local running = false
local ballVel = Vector3.zero
local coeffRest = .75
local grav = Vector3.new(0,9.807,0)
local rayPar = RaycastParams.new()
rayPar.FilterType = Enum.RaycastFilterType.Whitelist
rayPar.FilterDescendantsInstances = {workspace.Baseplate}
plr.PlayerGui.ScreenGui.TextButton.MouseButton1Click:Connect(function()
running = not running
end)
rs.RenderStepped:Connect(function(delta)
if running then
local rayRes = workspace:Raycast(ball.Position, (ballVel/ballVel.Magnitude)*ball.Size.Y/2, rayPar)
if rayRes then
ballVel = rayRes.Normal*(ballVel.Magnitude*coeffRest)
else
ballVel -= grav * delta
end
ball.CFrame = ball.CFrame+(ballVel*delta)
end
end)
as a final note on this, if you wish to get more nerdy with the physics, u can alter the rate of which your ball falls using the second law of motion where u multiply the force of gravity by it’s mass if you define it anywhere.
F (gravity in this case is the force) = Mass * Acceleration (the acceleration is the force of gravity in Vector3 terms)
(also the script i wrote doesn’t preserve momentum, so if you hit a slope and you have forward moment then bounce off the ground it’ll just bounce upwards losing all momentum on the X and Z axis)