From 9027e5018bd202e10df63e76fb95f71d5ee85fc0 Mon Sep 17 00:00:00 2001 From: BeaR Date: Thu, 18 Apr 2013 17:25:08 +0200 Subject: [PATCH] Handle zero-length vectors when normalizing --- GPU/Math3D.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/GPU/Math3D.h b/GPU/Math3D.h index c1bf12eae3..110bf6a9a0 100644 --- a/GPU/Math3D.h +++ b/GPU/Math3D.h @@ -114,11 +114,15 @@ public: return Vec3(other-(*this)).Length2(); } 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 len = Length(); - (*this) = (*this)/len; + if( len != 0.0f ) + (*this) = (*this)/len; return len; } float &operator [] (int i) //allow vector[2] = 3 (vector.z=3)