From cb90c949ff375d358352692d894d02354b02d35c Mon Sep 17 00:00:00 2001 From: Matt Low Date: Thu, 15 Nov 2018 16:17:26 +0400 Subject: [PATCH] New MINIMUM_ANGLE constant in Ball, currently PI/16 Apply impulses to ball when angle relative to horizontal axis is less than MINIMUM_ANGLE. Prevents balls getting stuck going horizontally --- core/src/com/me/brickbuster/entity/Ball.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index 772b869..5a51294 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -20,6 +20,8 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener { public static final float BOOST_SPEED = 55; public static final int BLOCKS_FOR_BOOST = 39; + public static final float MINIMUM_ANGLE = MathUtils.PI/16; + private float speed; private boolean isStuck = true; private boolean touchedPaddle = false; @@ -56,11 +58,18 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener { touchedPaddle = false; } - if (getY() + RADIUS < 0) { + if (pos.y + RADIUS < 0) { deleted = true; + return; } - body.setLinearVelocity(body.getLinearVelocity().nor().scl(speed)); + Vector2 velocity = body.getLinearVelocity(); + float rad = velocity.angleRad(); + if (Math.abs(rad) < MINIMUM_ANGLE || Math.abs(rad) > MathUtils.PI - MINIMUM_ANGLE) { + body.applyLinearImpulse(new Vector2(0, rad > 0? 1 : -1), pos, true); + } + + body.setLinearVelocity(velocity.nor().scl(speed)); } @Override @@ -111,10 +120,9 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener { } public Vector2 paddleReflectAngle() { - float trim = state.paddle.getWidth() * 0.10f; - float rel = MathUtils.clamp((pos.x - state.paddle.getX()) + (state.paddle.getWidth()/2), - trim, state.paddle.getWidth()-trim); + float rel = (pos.x - state.paddle.getX()) + (state.paddle.getWidth()/2); float newAngle = MathUtils.PI - (MathUtils.PI * (rel / state.paddle.getWidth())); + newAngle = MathUtils.clamp(newAngle, MINIMUM_ANGLE, MathUtils.PI-MINIMUM_ANGLE); return new Vector2(MathUtils.cos(newAngle), MathUtils.sin(newAngle)); } @@ -126,7 +134,6 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener { // launch at random angle between 135 and 45 degrees float angle = MathUtils.random(MathUtils.PI/2) + MathUtils.PI/4; direction = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle)); - } body.setLinearVelocity(direction.scl(speed)); isStuck = false;