Detect when train needs to start decelerating to stop at a certain point

I have 0 idea of where to start on this. I’m trying to detect when I need to decelerate based on current speed and where I need to stop. The deceleration and acceleration rates are linear, and I’m using AngularBodyVelocity to apply the speed.

Is the deceleration linear? please give more info

Trial and error, the ultimate method.

2 Likes

Is the character inside the train? If so you could check if the humanoid is sitting at the current point of where you want it to stop by using a touched event at that certain point.

1 Like

Have a stop part at the station, have the train line up it’s position with the stop parts position.

We can do this with some physics math.

First we have to get its velocity, which will be Distance divided by Time

Distance/Time = Velocity

Now this will returns us to a velocity in seconds if you used the Time in seconds. That means that as an example, if the object is going 10m (studs in this case) / 1 second, then in 10 seconds it will cover 100m. You can proceed to check with raycasting if this will result in a crash, and if yes, start the slowing down process.

If you’re going 10m in 1 second and in 10 seconds you’ll crash, you’ll have to decrease the velocity by 1 every second until your distance covered is 0. This is when it will reach a full stop.

the time that the train velocity will be 0 is the current velocity divided by acceleration

Your giving us almost no information at all here. How fast do you want it to deaccelerate, is there a distance it must reach before it starts stopping, etc etc.

Right now the answer to the question is completely subjective…

im using the acceleration formula a = (v-u)/t
a being the acceleration
v being the final velocity (0 studs per second)
u being the initial velocity (velocity of the train)
and t the time taken

1 Like

that will give you the time that take to stop the train

2 Likes

Search up suvat equations.

Or, just have something that checks speed through position of the train from the first frame to the next using Runservice.Heartbeat, then dividing that by deltaTime to get velocity.

2 Likes

We can start by determining speed. The conversion formula from meters to studs is

1 stud = 25 / 7 m   ;   1 m = 3.571 s

Now, how do you calculate the distance? You should have deceleration predefined. Deceleration should be constant (ideally), and depend on friction and brakes. So if you have acceleration, formula for which is

a = F / m   ;   F - force, m - mass

You can calculate braking distance for a particular speed:

v = u - a*t

Now you can expose time (t)

t = (v-u) / a

According to the time value, create a tween to reduce the speed. Luckly, there are also very useful easing styles that come with TweenService. If you are using BodyMovers, you can play with forces even more. That would probably also be more performance beneficial.

Perhaps add a Region3 at the station, so stopping is ensured. The train should stop smoothly, but the region is there to minimize any possible miscalculations.

2 Likes

and you can calculate the displacement distance with s = 1/2(u+v)t
s being the distance

1 Like

Yes, you can calculate the distance, but don’t do it too rapidly, because it’s relatively performance heavy. Do you even need to know the distance?

Another formula:

s = u • t - 1/2 • a • t^2   ;   u - starting velocity

*t^2 in your calculations is slightly slower than t • t.

3 Likes

yes you need to know the distance to know when you are going to start decelerating

1 Like

It needs to decellerate, not immediately stop.

1 Like

Yes, it is.

30

1 Like

Yeah, I’ve already developed a system do check when it’s near, so performance shouldn’t be too much of a worry.

1 Like

@Deverium I’m sorry, I have confused you with hiperion14 and though he was the original poster. I think you have all the necessary information now. However, I still don’t know how your train is supposed to be stopping. As I can currently imagine, once your train hits a part or reaches certain distance from station, it should start to decelerate. I’d say you should perhaps determine braking distance depending on train’s top speed. By calculating the longest possible displacemet for a particular train using the above formulas (I summarize them below), you can position your detector or set minimal magnitude a couple of studs outside breaking range, meaning that when the train is closer than

s = (u + v) • t • 0.5 -- Originally hiperion14's idea.
-- or
s = u • t - 1/2 • a • t^2   ;   u - starting 

studs, a tween is created, gradually reducing the speed according to braking time. You can use various easing styles and directions here. Velocity is a vector unit and can be found in properties. Use it to determine current speed.

Calculating time parameter:

t = (v - u) /a

Would you rather use BodyMovers? You can apply a contra force, being equal to

F = m * a

The mass, however, is probably far from equal to the mass of a real train. Adding floor friction would be overcomplicated.