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;
}
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<Brick> 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() {

View File

@ -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;
}
}