[Vehicle AI]Calculate how much to brake

Started by
5 comments, last by KieranChandler 9 years, 9 months ago

Im making a police simulator that requires traffic simulation like GTA4.

I currently have the vehicles driving around following nodes. The main problem i have is that when the vehicles detect something infront they slam on the brakes until the obstacle is no longer infront. I have come here to ask how i can work out how much braking needs to be applied to stop just before the obstacle.

Im using Unity3D, so i can get the velocity and the distance to the object.

Here is the code im using to make the vehicle brake:


RaycastHit rayHit = new RaycastHit();
if(Physics.Raycast(rayPos2, transform.forward, out rayHit, reactionDistance))
{
    hitDistance = Vector3.Distance(rayPos2, rayHit.point);
    float brakeAmount = (stoppingAmount * rigidbody.velocity.magnitude) / hitDistance;

    flWheel.brakeInput = Mathf.Clamp(brakeAmount, 0, 1);
    frWheel.brakeInput = Mathf.Clamp(brakeAmount, 0, 1);
    rlWheel.brakeInput = Mathf.Clamp(brakeAmount, 0, 1);
    rrWheel.brakeInput = Mathf.Clamp(brakeAmount, 0, 1);
}

If its easier to contact me via email or skype then here's my details:

Skype: kieranc12

Gmail: kieranchandler15@gmail.com

Any help would be greatly appreciated

Advertisement

First, it might be possible that any of the things I write here are already part of your code - I simply didnt understand it too well.

The car should be aware of its braking distance, the simplified formula is:


(speed in km/h / 10)² = braking distance in meters

The short braking distance (emergency brake) is half of that.

So first, braking is not necessary until the braking distance is not violated by obstacles. Hard brake only when the shorter distance is violated.

Second, if you drive with 50 km/h and another car is infront of you with 10m distance, but same speed, that is of course no reason to brake; you need delta of braking distance here.

So if you drive with 50, a car infront of you with 30, we get: 50km/h -> 25m and 30km/h -> 9m, so the critical distance between cars is 16m in this example.

If it feels wrong, add/substract a few meters to that, because of the reaction time threshold and the driver style.

If you re-evaluate the situation in reasonable short intervals, this should lead to mostly ok-behaviour.

It is a bit unsorted, but I hope you can make sense of it smile.png

Sorry, I dont quite understand what you mean there. I get that the vehicle needs to be aware of the necessary braking distance but i need to work out how much braking needs to be applied at the current update.

to apply the braking to the car i need to give the actual car(the ai is a separate script to the car) a number between 0 and 1. 0 being no brake input, 1 being full brakes.

so how would i work out how much braking needs to be applied and convert it into a number between 0 and 1 to represent that?

EDIT: Also the speed(velocity) is in Metres/s and the distance is in metres

Oh yes, I misread your question before. The rule-of-thumb formula I posted is from a driver license class, and has in first place nothing to do with traffic simulation.

It might still apply within the Unity3D simulation, then you need to figure out which values respond to "brake relaxed, but with the intend to stop the car" and "stop as soon as possible". You could guess values first and measure them with a few different initial velocities and see if the braking distance roughly corresponds to the formula result.

I am wondering if you really need the perfect braking value from beginning? In reality, a driver does just a (more or less educated) guess, and the slower the car gets, the better he can see if more or less braking is needed. I understand that a lot of iterations per car probably have impact on your performance, though. But your results will likely be more precise at lower speeds, so you could get away with a recalc at 50% of distance between the spot where car decides to start braking and the obstacle, and another one at 75% or so.

That is probably not very satisfying, but otherwise I have no idea, sorry. A lot depends on the implementation of whatever does the car physics for you.

Ive been thinking i may have to have it like that where the AI begins the braking with 50% braking(0.5) and then adjusts the amount based on what is thinks is necessary but i dont know how to do that and i think that would be extremely bad for performance.

Also doing the checks at 50% and 75% wouldn't work because what if the vehicle is traveling at say 30mph and needs to stop within 12 metres it needs to apply full braking, which means the ai needs to always start applying full brakes but lets say its traveling 30mph and needs to stop at 20 metres, what will happen is it will slam the brakes on 20 metres away then slowly decrease the amount of braking which will look weird.

Thank you alot for replying and helping out, i dont think that would work though.

just work out the distance between the car and obstacle, work out the braking distance (distance required to stop from current velocity) and calculate the ratio.

if distance is less than braking distance, you are going to hit it so braking is mute, but a good idea

if distance is twice braking distance you need 50% braking

is distance is four times braking distance you need 25% braking

calculating this every frame will compensate for maths accuracy issues as well

Okay that makes a lot of sense now, thanks. but how do i work out the braking distance. i have access to the slip amount and the friction curves of the wheels and everything so i would assume i can have a very accurate calculation in there.

This topic is closed to new replies.

Advertisement