3D: How determine if car is driving backwards?

Imagine driving a car based on physics in a 3D world.

I can get the forward direction as vector3 of the car in worldspace
and with the physic system the linear velocity as vector3 in worldspace.

But what I fail to understand is, how I can determine if the car is driving backwards?
Because the length of the linear velocity vector will always be positive or 0.

What I understand is, that I can get the opposite direction of a vector with multiplying it with -1. So I tried to check if all coordinates are pointing into a different direction, but that didn’t work well:
Code

bool isDrivingBackwards = false;
               
if (
    (
          (carVelocity.x < 0f && carForwardDirection.x > 0f)
            || (carVelocity.x > 0f && carForwardDirection.x < 0f)
     )
     &&
     (
          (carVelocity.y < 0f && carForwardDirection.y > 0f)
            || (carVelocity.y > 0f && carForwardDirection.y < 0f)
       )
       &&
       (
            (carVelocity.z < 0f && carForwardDirection.z > 0f)
              || (carVelocity.z > 0f && carForwardDirection.z < 0f)
        )
    ) {
       isDrivingBackwards = true;
}

You can use the dot product of the 2 vectors. If the value is negative, it’s safe to assume it’s going in the opposite direction of its forward vector

1 Like

@ADNCG That worked, thank you!! :slight_smile: