Hello. I want to calculate the distance needed for when my train should start decelerating.
What is the issue?
I’ve tried looking online or using AI, but the solutions do not work properly and either overshoot or underrun and its usually unpredictable. Most solutions only work in 2 dimensions, not 3D.
My current code is a mix of my work and AI, which does not work. The value given is much more than the distance every time.
function getStopDistance(stop:Vector3)
local currentSpeed = trig.AssemblyLinearVelocity.Magnitude
RunService.Heartbeat:Wait()
RunService.Heartbeat:Wait()
print(currentSpeed)
local distance = (trig.Position-stop).Magnitude
print("dist: "..tostring(distance))
local decel
if direction.Value == -1 then
decel = ((0.066)* 0.13)
end
if direction.Value == 1 then
decel = ((0.66)* 0.11)
end
local distanceToStop = ((currentSpeed^2) / (2 * decel))
return distanceToStop --math.abs(distance-distanceToStop)
end
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
Use the same script, but make the deceleration faster as it gets to its destination. Things like this are almost impossible to find due to Roblox’s physics
I do not own the asset as I work for someone else, so I can’t nor wouldn’t ask to do this cause it would ruin things. I have the linearvelocity instance’s force set to infinity so there should be no difference
uh, my value is 0.666 studs per frame. It uses a linearvelocity instance to move. would this work fine, or would changes have to happen since it uses heartbeat, as in per frame?
If you are having a constant velocity, then there is no acceleration, so the formula will error. There is also no stopping distance with a constant velocity.
it decelerates at 0.666 studs per frame. No friction is involved. the train uses a LinearVelocity(similar to bodyvelocity) to move it, does not use hinges, so I have the exact deceleration. also, what is “u” in your formula?
also, thanks for the help
0.666 studs per frame is a unit of speed, not acelerration…
I realised now that you want the stopping distance, not stopping time. I’ll try to derive a solution for it, although it looks like the ai’s solution of s = (-u^2)/2a is accurate
every heartbeat, a script decreases the linevelocity of a LinearVelocity instance by -0.666*0.13, which its force is infinity, so there is virtually no friction involved.