Handle zero-length vectors when normalizing

This commit is contained in:
BeaR 2013-04-18 17:25:08 +02:00
parent d91f2c9fd2
commit 9027e5018b

View file

@ -114,10 +114,14 @@ public:
return Vec3(other-(*this)).Length2(); return Vec3(other-(*this)).Length2();
} }
Vec3 Normalized() const { Vec3 Normalized() const {
return (*this) / Length(); float len = Length();
if( len == 0.0f)
return (*this);
return (*this) / len;
} }
float Normalize() { //returns the previous length, is often useful float Normalize() { //returns the previous length, is often useful
float len = Length(); float len = Length();
if( len != 0.0f )
(*this) = (*this)/len; (*this) = (*this)/len;
return len; return len;
} }