From 6ef7924e0122155c390698bdd66e9757f4da5fa0 Mon Sep 17 00:00:00 2001
From: aap <aap@papnet.eu>
Date: Fri, 17 Apr 2020 15:15:42 +0200
Subject: [PATCH] implemented CVector2D::NormaliseSafe for SkidMarks

---
 src/math/Vector2D.h      | 12 +++++++-----
 src/math/math.cpp        | 13 +++++++++++++
 src/render/Skidmarks.cpp |  3 ++-
 3 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/src/math/Vector2D.h b/src/math/Vector2D.h
index 705ad763..0885a5d2 100644
--- a/src/math/Vector2D.h
+++ b/src/math/Vector2D.h
@@ -11,16 +11,18 @@ public:
 	float Magnitude(void) const { return Sqrt(x*x + y*y); }
 	float MagnitudeSqr(void) const { return x*x + y*y; }
 
-	void Normalise(void){
+	void Normalise(void);
+
+	void NormaliseSafe(void) {
 		float sq = MagnitudeSqr();
-		//if(sq > 0.0f){
+		if(sq > 0.0f){
 			float invsqrt = RecipSqrt(sq);
 			x *= invsqrt;
 			y *= invsqrt;
-		//}else
-		//	x = 1.0f;
+		}else
+			y = 1.0f;
 	}
-	
+
 	const CVector2D &operator+=(CVector2D const &right) {
 		x += right.x;
 		y += right.y;
diff --git a/src/math/math.cpp b/src/math/math.cpp
index 4f74fac9..04fab797 100644
--- a/src/math/math.cpp
+++ b/src/math/math.cpp
@@ -4,6 +4,19 @@
 
 // TODO: move more stuff into here
 
+void
+CVector2D::Normalise(void)
+{
+	float sq = MagnitudeSqr();
+	assert(sq != 0.0f);	// just be safe here
+	//if(sq > 0.0f){
+		float invsqrt = RecipSqrt(sq);
+		x *= invsqrt;
+		y *= invsqrt;
+	//}else
+	//	x = 1.0f;
+}
+
 void
 CMatrix::SetRotate(float xAngle, float yAngle, float zAngle)
 {
diff --git a/src/render/Skidmarks.cpp b/src/render/Skidmarks.cpp
index 41ee5d1d..e003079e 100644
--- a/src/render/Skidmarks.cpp
+++ b/src/render/Skidmarks.cpp
@@ -214,7 +214,8 @@ CSkidmarks::RegisterOne(uintptr id, CVector pos, float fwdX, float fwdY, bool *i
 		aSkidmarks[i].m_pos[aSkidmarks[i].m_last] = pos;
 
 		CVector2D dist = aSkidmarks[i].m_pos[aSkidmarks[i].m_last] - aSkidmarks[i].m_pos[aSkidmarks[i].m_last-1];
-		dist.Normalise();
+		dist.NormaliseSafe();
+		fwd.NormaliseSafe();
 		CVector2D right(dist.y, -dist.x);
 		float turn = DotProduct2D(fwd, right);
 		turn = Abs(turn) + 1.0f;