From 8af4e653b2230a3845729b304f8df568bf75ca87 Mon Sep 17 00:00:00 2001 From: rxbn_ Date: Sat, 7 Dec 2019 17:09:56 +0100 Subject: [PATCH] Vector2D - rescaling fix - zero issue --- src/break_out/model/Vector2D.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/break_out/model/Vector2D.java b/src/break_out/model/Vector2D.java index 2ec28b5..ba64066 100644 --- a/src/break_out/model/Vector2D.java +++ b/src/break_out/model/Vector2D.java @@ -86,7 +86,9 @@ public class Vector2D implements IVector2D { public void rescale() { // calc unit vector and set it double vectorLength = Math.sqrt(Math.pow(getDx(), 2) + Math.pow(getDy(), 2)); //using the square root of x and y - setDx((1 / vectorLength) * getDx() * Constants.BALL_SPEED); - setDy((1 / vectorLength) * getDy() * Constants.BALL_SPEED); + + // rescaling only needed, if value is not zero + if(getDx() != 0) setDx((1 / vectorLength) * getDx() * Constants.BALL_SPEED); + if(getDy() != 0) setDy((1 / vectorLength) * getDy() * Constants.BALL_SPEED); } }