Use a Vector2 to track entity position.

This commit is contained in:
Matt Low 2018-11-11 20:56:58 +04:00
parent 3feca300be
commit 659fde4622
2 changed files with 22 additions and 17 deletions

View File

@ -36,9 +36,7 @@ public class Ball extends Entity {
return; return;
} }
float newX = getX() + (direction.x * speed); Vector2 ball = getPos().cpy().add(direction.cpy().scl(speed));
float newY = getY() + (direction.y * speed);
Vector2 ball = new Vector2(newX, newY);
boolean brick_collision = false; boolean brick_collision = false;
Iterator<Brick> brickIterator = getBrickBuster().getBricks().iterator(); Iterator<Brick> brickIterator = getBrickBuster().getBricks().iterator();
@ -51,7 +49,7 @@ public class Ball extends Entity {
Vector2 segment = v2.cpy().sub(v1); Vector2 segment = v2.cpy().sub(v1);
Vector2 nearest = Utils.nearestPoint(v1.cpy(), segment, ball.cpy()); 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(); brickIterator.remove();
Utils.reflect(direction, segment.nor()); Utils.reflect(direction, segment.nor());
brick_collision = true; 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); 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); Utils.reflect(direction, BrickBuster.HORIZONTAL_EDGE);
} else if (newY - RADIUS < 0) { } else if (ball.y - RADIUS < 0) {
reset(); reset();
} }
@ -73,13 +71,12 @@ public class Ball extends Entity {
Vector2 lineDir = paddle.getValue().sub(paddle.getKey()); Vector2 lineDir = paddle.getValue().sub(paddle.getKey());
Vector2 nearest = Utils.nearestPoint(paddle.getKey().cpy(), lineDir, ball.cpy()); 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(); paddleCollision();
} }
} }
setX(getX() + (direction.x * speed)); setPos(ball);
setY(getY() + (direction.y * speed));
} }
public void launch() { public void launch() {

View File

@ -1,19 +1,19 @@
package com.me.brickbuster.entity; package com.me.brickbuster.entity;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.BrickBuster; import com.me.brickbuster.BrickBuster;
public abstract class Entity { public abstract class Entity {
private ShapeRenderer shapeRenderer; private ShapeRenderer shapeRenderer;
private BrickBuster brickBuster; private BrickBuster brickBuster;
private float x, y; private Vector2 pos;
public Entity(BrickBuster brickBuster, int x, int y) { public Entity(BrickBuster brickBuster, int x, int y) {
this.shapeRenderer = new ShapeRenderer(); this.shapeRenderer = new ShapeRenderer();
this.brickBuster = brickBuster; this.brickBuster = brickBuster;
this.x = x; this.pos = new Vector2(x, y);
this.y = y;
} }
public abstract void render(); public abstract void render();
@ -28,20 +28,28 @@ public abstract class Entity {
return shapeRenderer; return shapeRenderer;
} }
public Vector2 getPos() {
return pos;
}
public void setPos(Vector2 pos) {
this.pos = pos;
}
public float getX() { public float getX() {
return x; return pos.x;
} }
public void setX(float x) { public void setX(float x) {
this.x = x; pos.x = x;
} }
public float getY() { public float getY() {
return y; return pos.y;
} }
public void setY(float y) { public void setY(float y) {
this.y = y; pos.y = y;
} }
} }