Shrink ball, increase brick height, improve paddle collision, add brick collision

This commit is contained in:
BlueNutterfly 2018-11-11 20:42:25 +04:00
parent a0b0753c5e
commit 3feca300be
2 changed files with 41 additions and 4 deletions

View File

@ -8,9 +8,11 @@ import com.me.brickbuster.BrickBuster;
import com.me.brickbuster.Utils; import com.me.brickbuster.Utils;
import net.dermetfan.utils.Pair; import net.dermetfan.utils.Pair;
import java.util.Iterator;
public class Ball extends Entity { public class Ball extends Entity {
public static final int RADIUS = 18; public static final int RADIUS = 12;
public static final Color BALL_COLOR = Color.CHARTREUSE; public static final Color BALL_COLOR = Color.CHARTREUSE;
public Vector2 direction; public Vector2 direction;
@ -36,6 +38,27 @@ public class Ball extends Entity {
float newX = getX() + (direction.x * speed); float newX = getX() + (direction.x * speed);
float newY = getY() + (direction.y * speed); float newY = getY() + (direction.y * speed);
Vector2 ball = new Vector2(newX, newY);
boolean brick_collision = false;
Iterator<Brick> brickIterator = getBrickBuster().getBricks().iterator();
while (!brick_collision && brickIterator.hasNext()) {
Brick brick = brickIterator.next();
Vector2[] vertices = brick.getVertices();
for(int i = 0; i < vertices.length; i++) {
Vector2 v1 = vertices[i];
Vector2 v2 = vertices[i+1 < vertices.length? i+1 : 0];
Vector2 segment = v2.cpy().sub(v1);
Vector2 nearest = Utils.nearestPoint(v1.cpy(), segment, ball.cpy());
if (nearest.dst(newX, newY) <= RADIUS) {
brickIterator.remove();
Utils.reflect(direction, segment.nor());
brick_collision = true;
break;
}
}
}
if (newX + RADIUS > BrickBuster.WIDTH || newX - RADIUS < 0) { if (newX + RADIUS > BrickBuster.WIDTH || newX - RADIUS < 0) {
Utils.reflect(direction, BrickBuster.VERTICAL_EDGE); Utils.reflect(direction, BrickBuster.VERTICAL_EDGE);
@ -47,7 +70,6 @@ public class Ball extends Entity {
if (direction.y < 0) { if (direction.y < 0) {
Pair<Vector2, Vector2> paddle = getBrickBuster().getPaddle().getTopEdge(); Pair<Vector2, Vector2> paddle = getBrickBuster().getPaddle().getTopEdge();
Vector2 ball = new Vector2(newX, newY);
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());
@ -68,7 +90,7 @@ public class Ball extends Entity {
public void paddleCollision() { public void paddleCollision() {
float paddleCenter = getBrickBuster().getPaddle().getX() + Paddle.PADDLE_WIDTH/2; float paddleCenter = getBrickBuster().getPaddle().getX() + Paddle.PADDLE_WIDTH/2;
float rel = (getX() - paddleCenter) + 50; float rel = MathUtils.clamp((getX() - paddleCenter) + 50, 5, 95);
float newAngle = MathUtils.PI - (MathUtils.PI * (rel/100)); float newAngle = MathUtils.PI - (MathUtils.PI * (rel/100));
direction = new Vector2(MathUtils.cos(newAngle), MathUtils.sin(newAngle)); direction = new Vector2(MathUtils.cos(newAngle), MathUtils.sin(newAngle));
} }

View File

@ -2,15 +2,25 @@ package com.me.brickbuster.entity;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.BrickBuster; import com.me.brickbuster.BrickBuster;
public class Brick extends Entity { public class Brick extends Entity {
public static final Color BLOCK_COLOR = Color.FOREST; public static final Color BLOCK_COLOR = Color.FOREST;
public static final int BLOCK_WIDTH = 50; public static final int BLOCK_WIDTH = 50;
public static final int BLOCK_HEIGHT = 15; public static final int BLOCK_HEIGHT = 20;
private Vector2[] vertices;
public Brick(BrickBuster brickBuster, int x, int y) { public Brick(BrickBuster brickBuster, int x, int y) {
super(brickBuster, x, y); super(brickBuster, x, y);
this.vertices = new Vector2[] {
new Vector2(x, y),
new Vector2(x + BLOCK_WIDTH, y),
new Vector2(x + BLOCK_WIDTH, y + BLOCK_HEIGHT),
new Vector2(x, y + BLOCK_HEIGHT)
};
} }
@Override @Override
@ -24,4 +34,9 @@ public class Brick extends Entity {
@Override @Override
public void update(float dt) { public void update(float dt) {
} }
public Vector2[] getVertices() {
return vertices;
}
} }