Fixed Vector[2|3]::Normalize returning NaN for zero vectors

Former-commit-id: e849a14189ff178134358d4ceed87955bc9f8eae
This commit is contained in:
Lynix 2014-08-05 17:38:49 +02:00
parent b413727a21
commit 4acd61cf30
2 changed files with 14 additions and 10 deletions

View File

@ -150,11 +150,13 @@ NzVector2<T>& NzVector2<T>::Minimize(const NzVector2& vec)
template<typename T>
NzVector2<T>& NzVector2<T>::Normalize(T* length)
{
T norm = std::sqrt(GetSquaredLength());
T invNorm = F(1.0) / norm;
x *= invNorm;
y *= invNorm;
T norm = GetLength();
if (norm > F(0.0))
{
T invNorm = F(1.0) / norm;
x *= invNorm;
y *= invNorm;
}
if (length)
*length = norm;

View File

@ -231,11 +231,13 @@ template<typename T>
NzVector3<T>& NzVector3<T>::Normalize(T* length)
{
T norm = GetLength();
T invNorm = F(1.0) / norm;
x *= invNorm;
y *= invNorm;
z *= invNorm;
if (norm > F(0.0))
{
T invNorm = F(1.0) / norm;
x *= invNorm;
y *= invNorm;
z *= invNorm;
}
if (length)
*length = norm;