It’s my collision script which i’ve been working on for the past few days…I thought I had it correct, but it still seems all messed up. It’s taking away too many bricks. It takes away the brick it hits and takes the last brick left on the board (most bottom right brick)
It’s driving me crazy.
public void checkBrickCollision()
{
int ballLeft = (int)ball.ballRect.x;
int ballRight = (int)ball.ballRect.x + (int)ball.ballRect.width;
int ballTop = (int)ball.ballRect.y + (int)ball.ballRect.height;
int ballBottom = (int)ball.ballRect.y;
for (int i=0;i<level.getBrickCount();i++) {
if (level.bricks[i].GetVisible() == true) {
Vector2 Top = new Vector2(ballLeft + (ball.ballRect.width / 2),ballTop );
Vector2 TopLeft = new Vector2(ballLeft,ballBottom+ball.ballRect.height);
Vector2 TopRight = new Vector2(ballLeft + ball.ballRect.width,ballBottom+ball.ballRect.height);
Vector2 Bottom = new Vector2(ballLeft + (ball.ballRect.width/2),ballBottom);
Vector2 BottomLeft = new Vector2(ballLeft,ballBottom);
Vector2 BottomRight = new Vector2(ballLeft + ball.ballRect.width,ballBottom);
Vector2 Right = new Vector2(ballRight,ballBottom + ball.ballRect.height/2);
Vector2 Left = new Vector2(ballLeft,ballBottom + ball.ballRect.height/2);
//int brickLeft = (int)bricks[i].brickRect.x;
//int brickRight = (int)bricks[i].brickRect.x +(int)bricks[i].brickRect.width;
//int brickTop = (int)bricks[i].brickRect.y + (int)bricks[i].brickRect.height;
//int brickBottom = (int)bricks[i].brickRect.x;
if (ball.getRect().overlaps(level.bricks[i].getRect()))
//System.out.println("Overlapped");
{
if (level.bricks[i].getRect().contains(Bottom.x,Bottom.y)) {
ball.ballSpeedY = -ball.ballSpeedY;
level.bricks[i].isVisible = false;
level.brickCount--;
System.out.println("Top");
return;
}
if (level.bricks[i].brickRect.contains(TopLeft.x,TopLeft.y)) {
ball.ballSpeedX = -ball.ballSpeedX;
ball.ballSpeedY = -ball.ballSpeedY;
level.bricks[i].isVisible = false;
level.brickCount--;
System.out.println("BottomRight");
return;
}
if (level.bricks[i].brickRect.contains(TopRight.x,TopRight.y)) {
ball.ballSpeedX = -ball.ballSpeedX;
ball.ballSpeedY = -ball.ballSpeedY;
level.bricks[i].isVisible = false;
level.brickCount--;
System.out.println("BottomLeft");
return;
}
if (level.bricks[i].brickRect.contains(BottomLeft.x,BottomLeft.y)) {
ball.ballSpeedX = -ball.ballSpeedX;
ball.ballSpeedY = -ball.ballSpeedY;
level.bricks[i].isVisible = false;
level.brickCount--;
System.out.println("TopRight");
return;
}
if (level.bricks[i].brickRect.contains(BottomRight.x,BottomRight.y)) {
ball.ballSpeedX = -ball.ballSpeedX;
ball.ballSpeedY = -ball.ballSpeedY;
level.bricks[i].isVisible = false;
System.out.println("TopLeft");
level.brickCount--;
return;
}
if (level.bricks[i].brickRect.contains(Top.x,Top.y)) {
ball.ballSpeedY = -ball.ballSpeedY;
level.bricks[i].isVisible = false;
level.brickCount--;
System.out.println("Bottom");
return;
}
if (level.bricks[i].brickRect.contains(Left.x,Left.y)) {
ball.ballSpeedX = -ball.ballSpeedX;
level.bricks[i].isVisible = false;
level.brickCount--;
System.out.println("Right");
return;
}
if (level.bricks[i].brickRect.contains(Right.x,Right.y)) {
ball.ballSpeedX = -ball.ballSpeedX;
level.bricks[i].isVisible = false;
level.brickCount--;
System.out.println("Left");
return;
}
System.out.println(level.getBrickCount());
}
}
}
}
EDIT
This is my Level class:
package com.psillicoder.brickbreaker;
import java.util.*;
import com.psillicoder.brickbreaker.Brick;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Level {
final int BLANK = 0;
final int BLUE = 1;
public int brickCount;
int ROW = 12;
int COL = 12;
public Brick[] bricks = new Brick[(ROW)* (COL)];
int board[][] = new int[ROW][COL];
public Level() {
board = new int[][] {
{2,2,2,2,2,2,2,2,2,2,2,2,},
{3,3,3,3,3,3,3,3,3,3,3,3,},
{1,1,1,1,1,1,1,1,1,1,1,1,},
{1,3,1,3,1,1,3,3,3,3,3,1,},
{1,3,1,3,1,1,1,1,3,1,1,1,},
{1,3,3,1,1,1,1,1,3,1,1,1,},
{1,3,1,3,1,1,1,1,3,1,1,1,},
{1,3,1,3,1,1,1,1,3,1,1,1,},
{1,1,1,1,1,1,1,1,1,1,1,1,},
{3,3,3,3,3,3,3,3,3,3,3,3,},
{2,2,2,2,2,2,2,2,2,2,2,2,},
{1,1,1,1,1,1,1,1,1,1,1,1,},
};
}
public void LoadBricks() {
this.brickCount = 0;
for (int x = 0; x < ROW; x++) {
for (int y = 0; y < COL;y++) {
if (board[x][y] != 0) {
bricks[this.brickCount] = new Brick(board[x][y],y*32 + 20,-x*16 + 575, true);
this.brickCount++;
}
if (board[x][y] == 0) {
//this.brickCount++;
}
}
}
}
public void drawBricks(SpriteBatch batch) {
int intBricks = brickCount;
for (int i=0;i<intBricks;i++) {
if (bricks[i].GetVisible() == true) {
bricks[i].draw(batch);
}
if (bricks[i].GetVisible()==false) {
//this.brickCount--;
}
}
}
public int getBrickCount() {
return bricks.length;
}
}