### Godot Version

4.3.beta3

### Question

I am trying to determine if my vehicle is to the left or right of a path3d. I have been able to work out the distance to the closest point on the path3d but it is positive no matter which side you are on and so logic is difficult to implement.

Any advice or thoughts?

Well distance is always positive. Do you use a built in distance-method? Try your own distance method and donst use the abs()

Use the dot product of two vectors.

Cant you just get the closest point and then check if its left/right?

@herrspaten Yeah bro, just do it. xD

In order to compute what side of your path your car is on, you have to define what constitutes *left* and *right*.

### Determining the car’s side relative to the path (left/right)

As eluded to by @soapspangledgames, you should use the dot product of two vectors. The two vectors in question are: the right-vector of the path, and your car’s position vector relative to the path (i.e. the vector between your closest path point and your car position).

The dot-product is used here because it informs us on how aligned two vectors are. Here is what the dot product describes between two normalized vectors:

Dot Product Result |
Vector relationship |

1.0 |
The vectors are completely aligned (on top of each other). |

0.0 |
The vectors are orthogonal (they form a 90 deg angle). |

-1.0 |
The vectors oppose one another. |

As you may have deduced from the above table, we can use the sign of the dot product (+/-) to determine what side our car is on relative to the road. A positive dot product means that our car is on the right side, and a negative dot product places our car on the left side.

### Computing the right-vector

I’m not yet familiar with `Path3D`

or `Curve3D`

so I don’t know if there is an easy way to compute a curve’s right vector. However, here’s one way of computing the right-vector of the path.

- Compute the forward vector of the path

1.1. Travel a small distance along the path from your “closest point”

1.2. Compute the vector between these two points

1.3. Normalize the vector
- Take the cross product of the forward- and up-vector.

I hope you understand the recommended approach. If not, please describe what we can do to help.

1 Like

Yeah sorry, i underrestimated the math here. I believe the pathfollow3d in conjunction with the remotetransform3d can give you the position and rotation of a point on a path. This should help calculating the right-vector, shouldnt it?

EDIT: Curve3D has a method float get_point_tilt ( int idx ), which also gives the rotation i guess?

Thank you for your suggestions and answers. I believe @Sweatix answer is correct however I have managed to solve this issue another way.

I have a vehicle which has a node(A) placed out in front of it. I then find the closest point (C) on the path to this leading node(A). There is then a node (B) placed in the centre of the vehicle which uses the look at function to look at the closest point (C). Then find the current rotation degrees of (B) and using a bit of maths it gets converted into a steering float between 0-1.0. This works pretty well.

From what I know get_point_tilt gets the tilt information for a certain point on a curve3d.