# Closest Distance to a complex Line

Hello,
I have a vehicle traveling along a road, and want to constantly monitor its distance from the ideal line, which I have as a mesh-object (Geometry) drawn with a vertex buffer. The way I am doing it right now, is by adding a bounding sphere to the vehicle and constantly checking the collision-results with the line-object. This of course is very precise, but very frame-expensive. What I am looking for is a method, to get the closest distance between the “line” object and the vehicle. If the line consisted of a dense row of vertices, this of course would not be a problem, but the line has sections that go over a long distance just straight, and has only two vertices forming that sections. Also, If I could find out the current line “segment”, I could simply use pythagoras to calculate the distance, but how would I identify the currently counting line segment?

Maybe someone has had a similar problem and has a good (precise and efficient) approach to solve this?

Thanks for any hints!

P

How many line segements have you?

A usuall approach would be to create some kind of acceleration structure, that you feed with vehicle position, and that returns a few possible lines, then brute force those preselected lines with pytagoras, and use best result.

Possibilites would be split the world into X*Y parts, and store for each part a array, of all lines intersecting it. -> Could be done at load or even precompiled.

Hey, thanks, thats the way I actually did it. The most tricky thing was to deal with sharp corners in the path. When simply using phytogoras on the closest line segments, this would give you a close distance if you just kept on riding straight over a turn. But, of course, this could be fixed, by checking, whether the car is actually within the range of the line-segment:

if(linestart.distance(lineend)+lineend.distance(currentPos)-currentPos.distance(linestart)<some_threshold){
// line passed, do not use for calculation
}