How to Calculate Stop Distance from part

  1. What do you want to achieve?

Hello. I want to calculate the distance needed for when my train should start decelerating.

  1. 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.

1 Like

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

2 Likes

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

I do appreciate your response though

1 Like

Assuming you know the acceleration value (or really the deceleration value), you can make use of the following formula:

v = u + at

Let v = 0, u be the initial velocity, a be the acceleration value, and t be the time in seconds.
Rearrange for t:

t = (0-u)/a
t = -u/a

(this is a calculation is used in actual physics, so I’m not entirely sure how closely Roblox will match the accuracy)

2 Likes

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?

1 Like

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.

2 Likes

I don’t understand, what should it be then? the velocity isn’t constant once it starts slowing down, Its linear

1 Like

At what rate is it slowing down then?

Is it slowing down from friction?

1 Like

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

just realized- you mentioned what u is. my apologies.

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

oh, okay. I thought I had that. Okay, so how do I convert the speed into acceleration?

Well what is causing it to stop?

1 Like

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.

How many times is heartbeat firing each second?

60 times per second. filler blah

So in your formula, decel should be -0.666*0.13 * 60, as we need t to be in seconds, hence a should be in Studs / Second²

1 Like

I feel like I tried this before but okay, thank you. I’ll let you know if it works!

It’s possible that heartbeat doesn’t fire every 1/60th of a second btw, so it may become inaccurate if you are using that to slow down the train.

yeah, it gave a distance of 50, way too close. I checked the physics summary and it is always either 60 or 59.

I’ll have to continue this on my phone and will not be able to edit my code from here