From 3feca300be0435e7cf5d5a77f9a132bab54bf381 Mon Sep 17 00:00:00 2001 From: BlueNutterfly Date: Sun, 11 Nov 2018 20:42:25 +0400 Subject: [PATCH] Shrink ball, increase brick height, improve paddle collision, add brick collision --- core/src/com/me/brickbuster/entity/Ball.java | 28 +++++++++++++++++-- core/src/com/me/brickbuster/entity/Brick.java | 17 ++++++++++- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index 7a5f2d7..971a333 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -8,9 +8,11 @@ import com.me.brickbuster.BrickBuster; import com.me.brickbuster.Utils; import net.dermetfan.utils.Pair; +import java.util.Iterator; + public class Ball extends Entity { - public static final int RADIUS = 18; + public static final int RADIUS = 12; public static final Color BALL_COLOR = Color.CHARTREUSE; public Vector2 direction; @@ -36,6 +38,27 @@ public class Ball extends Entity { float newX = getX() + (direction.x * speed); float newY = getY() + (direction.y * speed); + Vector2 ball = new Vector2(newX, newY); + + boolean brick_collision = false; + Iterator brickIterator = getBrickBuster().getBricks().iterator(); + while (!brick_collision && brickIterator.hasNext()) { + Brick brick = brickIterator.next(); + Vector2[] vertices = brick.getVertices(); + for(int i = 0; i < vertices.length; i++) { + Vector2 v1 = vertices[i]; + Vector2 v2 = vertices[i+1 < vertices.length? i+1 : 0]; + Vector2 segment = v2.cpy().sub(v1); + Vector2 nearest = Utils.nearestPoint(v1.cpy(), segment, ball.cpy()); + + if (nearest.dst(newX, newY) <= RADIUS) { + brickIterator.remove(); + Utils.reflect(direction, segment.nor()); + brick_collision = true; + break; + } + } + } if (newX + RADIUS > BrickBuster.WIDTH || newX - RADIUS < 0) { Utils.reflect(direction, BrickBuster.VERTICAL_EDGE); @@ -47,7 +70,6 @@ public class Ball extends Entity { 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()); @@ -68,7 +90,7 @@ public class Ball extends Entity { public void paddleCollision() { float paddleCenter = getBrickBuster().getPaddle().getX() + Paddle.PADDLE_WIDTH/2; - float rel = (getX() - paddleCenter) + 50; + float rel = MathUtils.clamp((getX() - paddleCenter) + 50, 5, 95); float newAngle = MathUtils.PI - (MathUtils.PI * (rel/100)); direction = new Vector2(MathUtils.cos(newAngle), MathUtils.sin(newAngle)); } diff --git a/core/src/com/me/brickbuster/entity/Brick.java b/core/src/com/me/brickbuster/entity/Brick.java index 4aa87f1..b64a888 100644 --- a/core/src/com/me/brickbuster/entity/Brick.java +++ b/core/src/com/me/brickbuster/entity/Brick.java @@ -2,15 +2,25 @@ package com.me.brickbuster.entity; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; +import com.badlogic.gdx.math.Vector2; import com.me.brickbuster.BrickBuster; public class Brick extends Entity { public static final Color BLOCK_COLOR = Color.FOREST; public static final int BLOCK_WIDTH = 50; - public static final int BLOCK_HEIGHT = 15; + public static final int BLOCK_HEIGHT = 20; + + private Vector2[] vertices; public Brick(BrickBuster brickBuster, int x, int y) { super(brickBuster, x, y); + + this.vertices = new Vector2[] { + new Vector2(x, y), + new Vector2(x + BLOCK_WIDTH, y), + new Vector2(x + BLOCK_WIDTH, y + BLOCK_HEIGHT), + new Vector2(x, y + BLOCK_HEIGHT) + }; } @Override @@ -24,4 +34,9 @@ public class Brick extends Entity { @Override public void update(float dt) { } + + public Vector2[] getVertices() { + return vertices; + } + }