Issues with GetActorForwardVector

I seem to have encountered and unexpected behavior of the GetActorForwardVector() method.

I’m checking to actors forward vector against one another, and I’m noticing that one actors forward vector looks like this: X=0.000 Y=1.000 Z=0.000, while the others forward vector looks like this: X=-0.000 Y=1.000 Z=0.000. It’s quite clear to see that they’re both facing the same way on the Y axis, however, one seem to be negative on the X axis, whereas the other is not.

Is there a way for me to force the 0’s to be positive, as I’m only interested in the 1’s? :slight_smile:

I don’t need the 0’s I only need the 1’s (getting the forward AXIS). So in this specific scenario, I have no use for the X axis, only the Y. Problem is of course, that the first vector is not equal to the second, since the X is negative/positive. I only need the value of the Y axis, but it might as well could’ve been the X axis. It’s just the Y axis in this example.

The issue of -0 is a complex one. What do you need to use the numbers for? It might not be necessary to change the values at all depending on what you are trying to do. -0 is the same value as 0 when used in Vector operations.

One thing you can do is the following:

  • Analyze Component by Component. Compare the two vectors (X, Y, Z) and get in which ones the Axis > 0 (and/or = 1).
  • Save each configuration in a variable (per say, an int array). In an example, Array of Actor 1 = 0, 0, 1, Actor 2 = 1, 0, 0.
  • Compare values on the items individually. The result, therefore, determines if they’re facing each other. in the example above, Array check loop would return in all of them being inconsistent, so they’re not facing.

To see if they are facing each other, do a dot product of the two vectors instead

This way you get a single value to compare and is more manageable.
Have a look at this table for the values to expect depending on their facing directions

If both the actors face the same way, the value will be close to 1. If it is close to -1, they are facing away from each other. If it is 0, they are perpendicular

So if the dot product is:

  • DotValue > 0.95, they are facing the same direction
  • DotValue < -0.95, they are facing away
  • abs(DotValue) < 0.05, they are perpendicular

An important thing to note is before the dot product is done, the two vectors need to be normalized

Using the dot product actually solved my problem. Thank you for your answer.

In case someone was wondering, it’s possible to get the dot product from an FVector like this: float DotProduct = FVector().DotProduct(FVector A, FVector B). Reference from the docs