From 79f4334cd7b4b3bed94691339e1dc09224c930eb Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sun, 11 Nov 2018 15:07:29 +0400 Subject: [PATCH] Add screen edge reflection. --- core/src/com/me/brickbuster/BrickBuster.java | 6 ++++++ core/src/com/me/brickbuster/Utils.java | 12 ++++++++++++ core/src/com/me/brickbuster/entity/Ball.java | 18 +++++++++++++++--- 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 core/src/com/me/brickbuster/Utils.java diff --git a/core/src/com/me/brickbuster/BrickBuster.java b/core/src/com/me/brickbuster/BrickBuster.java index a438c22..f284d8c 100644 --- a/core/src/com/me/brickbuster/BrickBuster.java +++ b/core/src/com/me/brickbuster/BrickBuster.java @@ -3,6 +3,7 @@ package com.me.brickbuster; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.math.Vector2; import com.me.brickbuster.entity.Ball; import com.me.brickbuster.entity.Block; import com.me.brickbuster.entity.Paddle; @@ -15,6 +16,11 @@ public class BrickBuster extends ApplicationAdapter { public static final int HEIGHT = 600; public static final String TITLE = "Brick Buster"; + public static final Vector2 RIGHT_EDGE = new Vector2(0, 1); + public static final Vector2 TOP_EDGE = new Vector2(-1, 0); + public static final Vector2 LEFT_EDGE = new Vector2(0, -1); + public static final Vector2 BOTTOM_EDGE = new Vector2(1, 0); + Ball ball; Paddle paddle; ArrayList blocks; diff --git a/core/src/com/me/brickbuster/Utils.java b/core/src/com/me/brickbuster/Utils.java new file mode 100644 index 0000000..0f950b6 --- /dev/null +++ b/core/src/com/me/brickbuster/Utils.java @@ -0,0 +1,12 @@ +package com.me.brickbuster; + +import com.badlogic.gdx.math.Vector2; + +public class Utils { + + public static Vector2 reflect(Vector2 incoming, Vector2 surface) { + Vector2 normal = new Vector2(-surface.y, surface.x); + return incoming.sub(normal.scl(normal.dot(incoming) * 2)); + } + +} diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index 74b35d0..c23af4b 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -5,6 +5,7 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; import com.me.brickbuster.BrickBuster; +import com.me.brickbuster.Utils; public class Ball extends Entity { @@ -31,11 +32,22 @@ public class Ball extends Entity { if (!getBrickBuster().isPlaying()) { return; } + + int newX = getX() + (int) (direction.x * speed); + int newY = getY() + (int) (direction.y * speed); + + if (newX + RADIUS > BrickBuster.WIDTH) { + Utils.reflect(direction, BrickBuster.RIGHT_EDGE); + } else if (newY + RADIUS > BrickBuster.HEIGHT) { + Utils.reflect(direction, BrickBuster.TOP_EDGE); + } else if (newX - RADIUS < 0) { + Utils.reflect(direction, BrickBuster.LEFT_EDGE); + } else if (newY - RADIUS < 0) { + Utils.reflect(direction, BrickBuster.BOTTOM_EDGE); + } + setX(getX() + (int) (direction.x * speed)); setY(getY() + (int) (direction.y * speed)); - /*if (this.getX() + RADIUS < BrickBuster.WIDTH) { - this.setX(this.getX() + 1); - }*/ } public void launch() {