Add paddle collision detection
Change tracking of entity positions from int to float
This commit is contained in:
parent
5756c9077c
commit
8f788851b2
@ -1,5 +1,6 @@
|
||||
package com.me.brickbuster;
|
||||
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
public class Utils {
|
||||
@ -9,4 +10,11 @@ public class Utils {
|
||||
return incoming.sub(normal.scl(normal.dot(incoming) * 2));
|
||||
}
|
||||
|
||||
public static Vector2 nearestPoint(Vector2 linep1, Vector2 lineDir, Vector2 p1) {
|
||||
float lineLen = lineDir.len();
|
||||
lineDir.nor();
|
||||
Vector2 ballDir = p1.sub(linep1);
|
||||
return linep1.add(lineDir.nor().scl(MathUtils.clamp(ballDir.dot(lineDir), 0, lineLen)));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.me.brickbuster.BrickBuster;
|
||||
import com.me.brickbuster.Utils;
|
||||
import net.dermetfan.utils.Pair;
|
||||
|
||||
public class Ball extends Entity {
|
||||
|
||||
@ -33,8 +34,8 @@ public class Ball extends Entity {
|
||||
return;
|
||||
}
|
||||
|
||||
int newX = getX() + (int) (direction.x * speed);
|
||||
int newY = getY() + (int) (direction.y * speed);
|
||||
float newX = getX() + (direction.x * speed);
|
||||
float newY = getY() + (direction.y * speed);
|
||||
|
||||
if (newX + RADIUS > BrickBuster.WIDTH || newX - RADIUS < 0) {
|
||||
Utils.reflect(direction, BrickBuster.VERTICAL_EDGE);
|
||||
@ -44,8 +45,19 @@ public class Ball extends Entity {
|
||||
reset();
|
||||
}
|
||||
|
||||
setX(getX() + (int) (direction.x * speed));
|
||||
setY(getY() + (int) (direction.y * speed));
|
||||
if (direction.y < 0) {
|
||||
Pair<Vector2, Vector2> 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());
|
||||
|
||||
if (nearest.dst(newX, newY) < RADIUS) {
|
||||
Utils.reflect(direction, lineDir.nor());
|
||||
}
|
||||
}
|
||||
|
||||
setX(getX() + (direction.x * speed));
|
||||
setY(getY() + (direction.y * speed));
|
||||
}
|
||||
|
||||
public void launch() {
|
||||
|
@ -7,7 +7,7 @@ public abstract class Entity {
|
||||
|
||||
private ShapeRenderer shapeRenderer;
|
||||
private BrickBuster brickBuster;
|
||||
private int x, y;
|
||||
private float x, y;
|
||||
|
||||
public Entity(BrickBuster brickBuster, int x, int y) {
|
||||
this.shapeRenderer = new ShapeRenderer();
|
||||
@ -28,19 +28,19 @@ public abstract class Entity {
|
||||
return shapeRenderer;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
public float getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
public void setX(float x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
public float getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
public void setY(float y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user