Download

Using FVector::Normalize...but it returns bool??

I tried using this without actually checking the definition, assuming it would return a unit vector… but no it returns a bool…?


FORCEINLINE bool FVector::Normalize(float Tolerance)
{
	const float SquareSum = X*X + Y*Y + Z*Z;
	if( SquareSum > Tolerance )
	{
		const float Scale = FMath::InvSqrt(SquareSum);
		X *= Scale; Y *= Scale; Z *= Scale;
		return true;
	}
	return false;
}

Obviously it’s working on the data inside the FVector struct…but shouldn’t this function be called IsNormalized()?

It appears that FVector::ToDirectionAndLength is what I want instead to return a normalized vector…


FORCEINLINE void FVector::ToDirectionAndLength(FVector &OutDir, float &OutLength) const
{
	OutLength = Size();
	if (OutLength > SMALL_NUMBER)
	{
		float OneOverLength = 1.0f/OutLength;
		OutDir = FVector(X*OneOverLength, Y*OneOverLength,
			Z*OneOverLength);
	}
	else
	{
		OutDir = FVector::ZeroVector;
	}
}

Is there something I’m missing or any valuable insights anyone can offer about this? Thanks!

Ahh I think I got it, by using an existing fvector in the class I can simply have it change itself by calling this on it.

You can call this on a FVector and it will work in place on that vector. Alternatively you can call GetSafeNormal() to do it to return a new FVector by value. That way you could use it in an expression and leave the original vector untouched.

Aha, thanks very much for that!