Normal( 3vPoint1 - 3vPoint2); What now ?

So back in UE3, to get a direction between 2 points, and normalize it to use it for other cases such as momentum and such, you used pretty much, Normal(Vector1 - Vector2), then you obtained, a unit vector direction from 2 3d points.

Now i try for instance, (MycppVectorPoint1 - MyCppVectorPoint2 ).Normalize() , or safe normal wich i don’t really know what’s the difference between " safe normal and Normalize and the tolarance, and i don’t get something Normal ?

What’s Safe normal and normalize difference ?

How do i get this direction vector in UE4 ?

Normalize() normalizes a vector variable in place, it doesn’t return anything:

FVector MyVec = GetVectorFromSomewhere();
// MyVec now normalized

This doesn’t work in your case because (A - B) is a C++ R-value - it’s a temporary expression, not a variable that can be changed.

SafeNormal() and UnsafeNormal() return a normalized copy:

FVector MyVec = GetVectorFromSomewhere().SafeNormal();

UnsafeNormal() is faster, but should only be used if you know for sure your vector cannot have a length of zero. Otherwise, use SafeNormal() and pass in a small value for the tolerance, for example 1.e-4f (0.0001). This just means if your vector had a length very close to or equal to zero, you’ll get a zero vector as the result. It avoids division by zero, or precision errors from dividing by a number close to zero.

Ah alright, i see normalize doesen’t return anything at all… it’s just a non static member function that modifies the instance of the vector.


Normalize() vs SafeNormal()

For other readers, just to be clear

//this returns new vector that is normalized
FVector DirectionVector = (Actor1Location - Actor2Location ).GetSafeNormal(); //in 4.7 ".SafeNormal()" is deprecated

//This is a member var that does not return anything, and modifies original data

In general I’d encourage the use of SafeNormal() for clarity of your own code when reviewing it later.


I truly got stuck because of the new language and it’s features, is there something else that i need to take in mind " really really " in mind other than it’s complicated object oriented structure ?

Take on small tasks / sample projects and keep growing your knowledge base through experience, and you will feel more and more equipped for larger / more ambitious projects as you go!



PS: I am also writing a book on UE4 C++ to introduce core gameplay mechanics and a lot of the vector math that you need for AI and custom game mechanics in a 3D game.

GetSafeNormal is named the way it is because it checks to make sure you aren’t going to divide by zero not because you are returning a copy. GetUnsafeNormal also returns a copy but avoids the additional checks. Normalize() will normalize the vector in place but is also “safe” because it checks for divide by zero first.

Very nice to hear from you Matt!

I updated my above code to reflect the 4.7 deprecation.



Edit nevermind.

Can’t wait to buy it. <3