3
\$\begingroup\$

I have an 'AI' bus in my game that needs to pull over occasionally at a bus stop. What I want to do is change the acceleration and brake force based on the stopping distance of the bus. I don't want to simply use the distance between the bus stop and the bus because each time the bus pulls over, it could be traveling at a different speed.

Right now it uses the distance between the bus stop and the bus. It doesn't really work well though because if a bus is traveling faster, it will take a longer distance to stop and will likely go past the bus stop. Same thing if it were going slower.

How do I calculate the stopping distance my bus will take to come to a complete stop?

\$\endgroup\$
2
\$\begingroup\$

Let's say that at some point in time you have the current speed v0 of your bus, which is at some position x0, and it needs to stop at position xs (s stands for stop).

TL;DR: You have to exert the following force f on the bus:

f = 0.5 * m * v0^2 / (x0 - xs)

where m is the mass of the bus.

Details: The easiest way is to exert a constant force that will drive the bus to a parabolic (in time) curve ending exactly at the bus stop. The equation for the position x at time t, and for the velocity v are

x = x0 + v0 * t + 0.5 * a * t^2
v = v0 + a* t

where a is the acceleration, which is what we want to calculate. From the second equation, get t as a function of v, and plug into the first equation. In this way, you eliminate t and find the equation for x as function of v:

x = x0 + 0.5 * (v^2 - v0^2) / a

Now impose that at v = 0 you want x = xs, and solve for a. You get

a = 0.5 * v0^2 / (x0 - xs)

from which you get the force by multiplying with the mass m.

\$\endgroup\$
  • \$\begingroup\$ That looks really good, it's late where I am but I will try this in the morning. Thank you very much! \$\endgroup\$ – mr-matt May 1 '17 at 9:34
  • \$\begingroup\$ It's not really working that well. I tried adding force with a rigid body but it just slowed the bus, it didn't actually stop. Then I tried adding force with ForceMode.Impulse and that stopped the bus almost immediately. I then tried adding that as the brake torque, but again, it stops the bus almost immediately \$\endgroup\$ – mr-matt May 1 '17 at 19:27
  • \$\begingroup\$ If the bus does not stop it means that other forces are acting on it. Try to switch off all other forces while you are exerting the one I discussed in the answer. I am not familiar with the wheel collider of Unity, so I am not sure if that is possible. My answer is somewhat a simplified solution for a simple point particle, that you may adapt to your needs. \$\endgroup\$ – Turms May 1 '17 at 19:33
0
\$\begingroup\$

I ended up using a simple speed-distance-time calculation, and a velocity calculation. This is the code I used:

float time = ((distance / currentSpeed) / timeFactor);

float acceleration = (-currentSpeed / time) / vFactor;

v = acceleration;

timeFactor and vFactor are just arbitrary values, which happened to be 1 and 15 respectively.

This may not work for most other people however as this was quite specific to my code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.