Shrink ball, increase brick height, improve paddle collision, add brick collision
This commit is contained in:
		@ -8,9 +8,11 @@ import com.me.brickbuster.BrickBuster;
 | 
			
		||||
import com.me.brickbuster.Utils;
 | 
			
		||||
import net.dermetfan.utils.Pair;
 | 
			
		||||
 | 
			
		||||
import java.util.Iterator;
 | 
			
		||||
 | 
			
		||||
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 Vector2 direction;
 | 
			
		||||
@ -36,6 +38,27 @@ public class Ball extends Entity {
 | 
			
		||||
 | 
			
		||||
		float newX = getX() + (direction.x * 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) {
 | 
			
		||||
			Utils.reflect(direction, BrickBuster.VERTICAL_EDGE);
 | 
			
		||||
@ -47,7 +70,6 @@ public class Ball extends Entity {
 | 
			
		||||
 | 
			
		||||
		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());
 | 
			
		||||
 | 
			
		||||
@ -68,7 +90,7 @@ public class Ball extends Entity {
 | 
			
		||||
 | 
			
		||||
	public void paddleCollision() {
 | 
			
		||||
		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));
 | 
			
		||||
		direction = new Vector2(MathUtils.cos(newAngle), MathUtils.sin(newAngle));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -2,15 +2,25 @@ package com.me.brickbuster.entity;
 | 
			
		||||
 | 
			
		||||
import com.badlogic.gdx.graphics.Color;
 | 
			
		||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
 | 
			
		||||
import com.badlogic.gdx.math.Vector2;
 | 
			
		||||
import com.me.brickbuster.BrickBuster;
 | 
			
		||||
 | 
			
		||||
public class Brick extends Entity {
 | 
			
		||||
	public static final Color BLOCK_COLOR = Color.FOREST;
 | 
			
		||||
	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) {
 | 
			
		||||
		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
 | 
			
		||||
@ -24,4 +34,9 @@ public class Brick extends Entity {
 | 
			
		||||
	@Override
 | 
			
		||||
	public void update(float dt) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public Vector2[] getVertices() {
 | 
			
		||||
		return vertices;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user