I have the car's current position Vector3
and an API that gets me the closest point on the middle of the road to any Vector3
parameter I send it like
Vector3 returnCenterOfRoad(Vector3 positionToCalculateFrom)
so I thought calculate two relative vectors, one from car's position to a point returned from that method a little ways down the road called pointC
and then call that method using the car's current position called pointB
(the car's current position would be pointA
). Now I can get relative vectors from A to C and B to C and I know when vectors start from the same point but end up away from each other, I can do something like AC - BC but what about in this case when they start away but go to the same point?
Is this even the right way to calculate the current distance to the middle of the road?
Also, how do I know if it's on the right half or left half of the road if the track snakes around so the car's orientation changes, the left half could be right half or vice versa depending on where on the track the car currently is.
Edit: I also have another method
Vector3 returnLineTangentToCenterOfRoad(Vector3 myPoint)
So if I send in my current car position as myPoint
, that method will return a unit vector, lets call it tanUnitVec
that is tangent to the closest "center of the road" point from myPoint
which I can draw using debug.drawline as a vector (myPoint + tanUnitVec) * scalar
. Visuzalized with scalar of 2f, I can see that this tangent line is pointing to the origin in the XZ plane and the track is procedurally generated to be in anywhere of the 4 quadrants.
I also have the car's transform
in WCS, Velocity
, Angular velocity
, speed
.
Everything mentioned the beginning is also in WCS.
Here is an image of a yellow line I drew with myPoint
as my car's current position and redline with myPoint
offset forwards a bit from my car. You can see the faint white line that is the origin so the car's current position is in quadrant 1 and moving forwards to quadrant 4 with z-axis up and down and x-axis left to right.
returnCenterOfRoad
works? We'll need a bit more information from the road to detect whether we're on the left or right side of it. How do you use it to get a point "a little ways down the road" - do you have some information about the road's direction, or a progress parameter around the track? \$\endgroup\$(car location - center of road location)
. Left/right will need some more information from the road (When I did something similar, I defined the road as a spline of points, and thus I could get point n+1 or n-1 to get the next/previous road center point) \$\endgroup\$