if(dir == 0) { // Up-Right
ball.x += ballSpeed;
ball.y -= ballSpeed;
}else if(dir == 1) { // Down-Right
ball.x += ballSpeed;
ball.y += ballSpeed;
}else if(dir == 2) { // Up-Left
ball.x -= ballSpeed;
ball.y -= ballSpeed;
}else if(dir == 3) { // Down-Left
ball.x -= ballSpeed;
ball.y += ballSpeed;
}
public void checkCollision(){
if(dir == 0) { // Up-Right
//벽에 충돌됐을때
if(ball.y < 0 ) {
dir = 1;
}
if(ball.x > CANVAS_WIDTH-BALL_WIDTH) { //wall right
dir =2;
}
//Bar none
}else if(dir == 1) { // Down-Right
//벽에 충돌됐을때
if(ball.y > CANVAS_HEIGHT-BALL_HEIGHT - BALL_HEIGHT) { //wall bottom
dir = 0;
}
if(ball.x > CANVAS_WIDTH-BALL_WIDTH) { //wall
dir =3; //방향이 꺾이도록
}
}else if(dir == 2) { // Up-Left
//wall
if(ball.y < 0 ) {
dir = 3;
}
if(ball.x < 0 ) {
dir = 0;
}
//bar
}else if(dir == 3) { // Down-Left
//wall
if(ball.y > CANVAS_HEIGHT-BALL_HEIGHT - BALL_HEIGHT) { //wall bottom
dir = 2;
}
if(ball.x < 0 ) { //wall left
dir = 1;
}
}
};
먼저, Ball 설정부분에 위치값 미리 메서드를 만들어서 불러오면 덜 헷갈리기위해
static class Ball {
//생략
//충돌 체크 미리 만들기
Point getCenter() {
return new Point(x+(BALL_WIDTH/2), y+(BALL_HEIGHT/2));
}
Point getBottomCenter() {
return new Point( x + (BALL_WIDTH/2), y + (BALL_HEIGHT));
}
Point getTopCenter() {
return new Point(x + (BALL_HEIGHT/2), y);
}
Point getLeftCenter() {
return new Point(x , y + (BALL_HEIGHT/2));
}
Point getRightCenter() {
return new Point(x + (BALL_WIDTH), y + (BALL_HEIGHT/2));
}
}
서로 영역이 겹치는지 확인하는 함수
public boolean duplRect(Rectangle rect1, Rectangle rect2) {
return rect1.intersects(rect2);// 서로 영역이 겹치는 체크
}
public void checkCollision(){
if(dir == 0) { // Up-Right
//벽에 충돌됐을때
if(ball.y < 0 ) {
dir = 1;
}
if(ball.x > CANVAS_WIDTH-BALL_WIDTH) { //wall right
dir =2;
}
//Bar none
}else if(dir == 1) { // Down-Right
//벽에 충돌됐을때
if(ball.y > CANVAS_HEIGHT-BALL_HEIGHT - BALL_HEIGHT) { //wall bottom
dir = 0;
}
if(ball.x > CANVAS_WIDTH-BALL_WIDTH) { //wall
dir =3; //방향이 꺾이도록
}
//Bar에 충돌 될 때
if(ball.getBottomCenter().y >= bar.y) {
if(duplRect(new Rectangle(ball.x, ball.y, ball.width, ball.height),
new Rectangle(bar.x , bar.y, bar.width, bar.height))) {
dir = 0;
}
}
}else if(dir == 2) { // Up-Left
//wall
if(ball.y < 0 ) {
dir = 3;
}
if(ball.x < 0 ) {
dir = 0;
}
//Bar none
}else if(dir == 3) { // Down-Left
//wall
if(ball.y > CANVAS_HEIGHT-BALL_HEIGHT - BALL_HEIGHT) { //wall bottom
dir = 2;
}
if(ball.x < 0 ) { //wall left
dir = 1;
}
//Bar에 충돌 될 때
if(ball.getBottomCenter().y >= bar.y) {
if(duplRect(new Rectangle(ball.x, ball.y, ball.width, ball.height),
new Rectangle(bar.x , bar.y, bar.width, bar.height))) {
dir = 2;
}
}
}
};
// 블록에 충돌 됐을 때
public void checkCollisionBlock() {
// 0:Up-Right, 1:Down-Right, 2:Up-Left, 3:Down-Left
for (int i = 0; i < BLOCK_ROWS; i++) {
for (int j = 0; j < BLOCK_COLUMNS; j++) {
Block block = blocks[i][j];
if (block.isHidden == false) {
if (dir == 0) { // Up-Right
if (duplRect(new Rectangle(ball.x, ball.y, ball.width, ball.height),
new Rectangle(block.x, block.y, block.width, block.height))) {
if(ball.x > block.x + 2 && ball.getRightCenter().x <= block.x + block.width -2) {
//block bottom collision
dir =1;
}else {
//block left collision
dir =2;
}
block.isHidden = true;
}
} else if (dir == 1) { // Down-Right
if (duplRect(new Rectangle(ball.x, ball.y, ball.width, ball.height),
new Rectangle(block.x, block.y, block.width, block.height))) {
if(ball.x > block.x + 2 &&
ball.getRightCenter().x <= block.x + block.width -2) {
//block top collision
dir =0;
}else {
//block left collision
dir =3;
}
block.isHidden = true;
}
} else if (dir == 2) { // Up-Left
if (duplRect(new Rectangle(ball.x, ball.y, ball.width, ball.height),
new Rectangle(block.x, block.y, block.width, block.height))) {
if(ball.x > block.x + 2 &&
ball.getRightCenter().x <= block.x + block.width -2) {
//block bottom collision
dir =3;
}else {
//block right collision
dir =0;
}
block.isHidden = true;
}
} else if (dir == 3) { // Down-Left
if (duplRect(new Rectangle(ball.x, ball.y, ball.width, ball.height),
new Rectangle(block.x, block.y, block.width, block.height))) {
if(ball.x > block.x + 2 &&
ball.getRightCenter().x <= block.x + block.width -2) {
//block top collision
dir =3;
}else {
//block right collision
dir =1;
}
block.isHidden = true;
}
}
}
}
}
};
블록에 충돌됐을때 잘 되는걸 볼수 있다.!!!!😄