# How can I keep my car between two lanes or lines?

Attention Topic was automatically imported from the old Question2Answer platform.

How can I keep my car between two lanes or lines?

I have a car. I’m going to install a lane tracking system on this car. I want it to detect 2 lines in the path and automatically stay between that line.
If there is no line, I want it to point to the next line. How can I do it?

Depends on how realistic you want the simulation to be. If you want to simulate the car as only being able to turn its steering wheel by so many degrees per frame, you can learn about proportional-integral-derivative systems. If you know the desired steering angle and your current steering angle, then the “error” is their difference, and you can make a formula using the error, its integral, and its derivative to decide how hard to turn the wheel each frame, and transform the output into a change of the actual steering. On top of that, you can use another PID formula to find your desired steering angle. Basically, while the car moves in the direction it’s steered, it wants to keep its distance from each line the same. Your “error” in that system is (distance from line 1) - (distance from line 2). You’ll tune each formula manually by tweaking the proportional coefficients for each part of the formula, until the behavior looks reasonable.

It’s explained really well this video

Look at this answer for how to get the closest point on each line to take the distance from.

To keep track of the derivative and integral of error, just update them during the `_physics_process` function:
`derivative = (error - prev_error) / delta`
`integral += error * delta`

Choosing a new line to follow can be a little more complicated, but you’ll need some control logic that decides what lines you’re following. Take things one step at a time.

I’m pretty confused by what you said.

Gangster53 YT | 2022-10-17 14:22