diff --git a/core/src/com/me/brickbuster/Utils.java b/core/src/com/me/brickbuster/Utils.java index 0f950b6..6dff173 100644 --- a/core/src/com/me/brickbuster/Utils.java +++ b/core/src/com/me/brickbuster/Utils.java @@ -1,5 +1,6 @@ package com.me.brickbuster; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; public class Utils { @@ -9,4 +10,11 @@ public class Utils { return incoming.sub(normal.scl(normal.dot(incoming) * 2)); } + public static Vector2 nearestPoint(Vector2 linep1, Vector2 lineDir, Vector2 p1) { + float lineLen = lineDir.len(); + lineDir.nor(); + Vector2 ballDir = p1.sub(linep1); + return linep1.add(lineDir.nor().scl(MathUtils.clamp(ballDir.dot(lineDir), 0, lineLen))); + } + } diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index 84713dd..56a0c94 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -6,6 +6,7 @@ import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; import com.me.brickbuster.BrickBuster; import com.me.brickbuster.Utils; +import net.dermetfan.utils.Pair; public class Ball extends Entity { @@ -33,8 +34,8 @@ public class Ball extends Entity { return; } - int newX = getX() + (int) (direction.x * speed); - int newY = getY() + (int) (direction.y * speed); + float newX = getX() + (direction.x * speed); + float newY = getY() + (direction.y * speed); if (newX + RADIUS > BrickBuster.WIDTH || newX - RADIUS < 0) { Utils.reflect(direction, BrickBuster.VERTICAL_EDGE); @@ -44,8 +45,19 @@ public class Ball extends Entity { reset(); } - setX(getX() + (int) (direction.x * speed)); - setY(getY() + (int) (direction.y * speed)); + if (direction.y < 0) { + Pair paddle = getBrickBuster().getPaddle().getTopEdge(); + Vector2 ball = new Vector2(newX, newY); + Vector2 lineDir = paddle.getValue().sub(paddle.getKey()); + Vector2 nearest = Utils.nearestPoint(paddle.getKey().cpy(), lineDir, ball.cpy()); + + if (nearest.dst(newX, newY) < RADIUS) { + Utils.reflect(direction, lineDir.nor()); + } + } + + setX(getX() + (direction.x * speed)); + setY(getY() + (direction.y * speed)); } public void launch() { diff --git a/core/src/com/me/brickbuster/entity/Entity.java b/core/src/com/me/brickbuster/entity/Entity.java index 6791e90..2ce1a76 100644 --- a/core/src/com/me/brickbuster/entity/Entity.java +++ b/core/src/com/me/brickbuster/entity/Entity.java @@ -7,7 +7,7 @@ public abstract class Entity { private ShapeRenderer shapeRenderer; private BrickBuster brickBuster; - private int x, y; + private float x, y; public Entity(BrickBuster brickBuster, int x, int y) { this.shapeRenderer = new ShapeRenderer(); @@ -28,19 +28,19 @@ public abstract class Entity { return shapeRenderer; } - public int getX() { + public float getX() { return x; } - public void setX(int x) { + public void setX(float x) { this.x = x; } - public int getY() { + public float getY() { return y; } - public void setY(int y) { + public void setY(float y) { this.y = y; }