I am sure eveyone is tired of hearing about collision detection, but I have a pickle of a situation. I am making a breakout clone, and am having issues with the collision dection of the blocks. I use the same collision checks as I do for the paddle, but everything goes crazy when I enable the block collision detection.
Here is my code:
Map.cpp (collision Detection code)
int Map::collision(Ball cBall)
{
int rType;
for (int r = 0;r < MAPSIZER; r++)
{
for (int c =0;c < MAPSIZEC;c++)
{
if(level[r][c].isAlive()==1)
{
rType = level[r][c].collision(cBall);
return rType;
}
}
}
return 0;
}
Player.cpp (Collision Detection code)
int Player::collision(Ball cBall)
{
if((position.y+height)>(cBall.getPositionY()-cBall.getHeight()))
{
if((position.x+width) > (cBall.getPositionX()-cBall.getWidth()) &&
(position.x-width) < (cBall.getPositionX()+cBall.getWidth()))
{return 3;}
}
return 0;
}
Ball.cpp (Collision Detection code)
int Ball::collision(int D)
{
if(position.x >= 5.40f-width) {xdirection = -1.0f; return true;}
if(position.x <= -5.45f+width) {xdirection = 1.0f; return true;}
if(position.y >= 7.50f-height){ydirection = -1.0f; return true;}
if(position.y <= -8.00f+height){ydirection = 1.0f; return true;}
switch(D)
{
case 0:
return false;
break;
case 1:
ydirection = -1;
return true;
break;
case 2:
xdirection = 1;
return true;
break;
case 3:
ydirection = 1;
return true;
break;
case 4:
xdirection = -1;
return true;
default:
return false;
break;
}
}
game loop code
ball.collision(map.collision(ball));
ball.collision(player.collision(ball));
ball.update();
map.drawMap();
player.drawPlayer();
ball.drawBall();
Right now I just have the code for TOP detection for the blacks. (one step at a time :))
If I disable the block collision, it works fine with the paddle, but when I enable it, the ball does erratic things.
Thanks,
dimebag