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,11 +114,15 @@ 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();
(*this) = (*this)/len; if( len != 0.0f )
(*this) = (*this)/len;
return len; return len;
} }
float &operator [] (int i) //allow vector[2] = 3 (vector.z=3) float &operator [] (int i) //allow vector[2] = 3 (vector.z=3)