From 659fde462254524e02e1d162ab7086179d9150dc Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sun, 11 Nov 2018 20:56:58 +0400 Subject: [PATCH] Use a Vector2 to track entity position. --- core/src/com/me/brickbuster/entity/Ball.java | 17 ++++++-------- .../src/com/me/brickbuster/entity/Entity.java | 22 +++++++++++++------ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index 971a333..b616fad 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -36,9 +36,7 @@ public class Ball extends Entity { return; } - float newX = getX() + (direction.x * speed); - float newY = getY() + (direction.y * speed); - Vector2 ball = new Vector2(newX, newY); + Vector2 ball = getPos().cpy().add(direction.cpy().scl(speed)); boolean brick_collision = false; Iterator brickIterator = getBrickBuster().getBricks().iterator(); @@ -51,7 +49,7 @@ public class Ball extends Entity { Vector2 segment = v2.cpy().sub(v1); Vector2 nearest = Utils.nearestPoint(v1.cpy(), segment, ball.cpy()); - if (nearest.dst(newX, newY) <= RADIUS) { + if (nearest.dst(ball.x, ball.y) <= RADIUS) { brickIterator.remove(); Utils.reflect(direction, segment.nor()); brick_collision = true; @@ -60,11 +58,11 @@ public class Ball extends Entity { } } - if (newX + RADIUS > BrickBuster.WIDTH || newX - RADIUS < 0) { + if (ball.x + RADIUS > BrickBuster.WIDTH || ball.x - RADIUS < 0) { Utils.reflect(direction, BrickBuster.VERTICAL_EDGE); - } else if (newY + RADIUS > BrickBuster.HEIGHT) { + } else if (ball.y + RADIUS > BrickBuster.HEIGHT) { Utils.reflect(direction, BrickBuster.HORIZONTAL_EDGE); - } else if (newY - RADIUS < 0) { + } else if (ball.y - RADIUS < 0) { reset(); } @@ -73,13 +71,12 @@ public class Ball extends Entity { Vector2 lineDir = paddle.getValue().sub(paddle.getKey()); Vector2 nearest = Utils.nearestPoint(paddle.getKey().cpy(), lineDir, ball.cpy()); - if (nearest.dst(newX, newY) < RADIUS) { + if (nearest.dst(ball.x, ball.y) <= RADIUS) { paddleCollision(); } } - setX(getX() + (direction.x * speed)); - setY(getY() + (direction.y * speed)); + setPos(ball); } public void launch() { diff --git a/core/src/com/me/brickbuster/entity/Entity.java b/core/src/com/me/brickbuster/entity/Entity.java index 2ce1a76..2966a22 100644 --- a/core/src/com/me/brickbuster/entity/Entity.java +++ b/core/src/com/me/brickbuster/entity/Entity.java @@ -1,19 +1,19 @@ package com.me.brickbuster.entity; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Vector2; import com.me.brickbuster.BrickBuster; public abstract class Entity { private ShapeRenderer shapeRenderer; private BrickBuster brickBuster; - private float x, y; + private Vector2 pos; public Entity(BrickBuster brickBuster, int x, int y) { this.shapeRenderer = new ShapeRenderer(); this.brickBuster = brickBuster; - this.x = x; - this.y = y; + this.pos = new Vector2(x, y); } public abstract void render(); @@ -28,20 +28,28 @@ public abstract class Entity { return shapeRenderer; } + public Vector2 getPos() { + return pos; + } + + public void setPos(Vector2 pos) { + this.pos = pos; + } + public float getX() { - return x; + return pos.x; } public void setX(float x) { - this.x = x; + pos.x = x; } public float getY() { - return y; + return pos.y; } public void setY(float y) { - this.y = y; + pos.y = y; } }