# How can I improve this collision detection logic?

I’m trying to make an android game and I’m having a bit of trouble getting the collision detection to work. It works sometimes but my conditions aren’t specific enough and my program gets it wrong. How could I improve the following if conditions?

public boolean checkColisionWithPlayer( Player player )
{
// Top Left             // Top Right                // Bottom Right                                         // Bottom Left                      //
int[][] PP = { { player.x, player.y }, { player.x + player.width, player.y }, {player.x + player.height, player.y + player.width }, { player.x, player.y + player.height } };

// TOP LEFT - PLAYER //

if( ( PP[0][0] > x && PP[0][0] < x + width ) && ( PP[0][1] > y && PP[0][1] < y + height ) && ( (x - player.x) < 0 ) )
{
player.isColided = true;
//player.isSpinning = false;

// Collision On Right
if( PP[0][0] > ( x + width/2 ) && ( PP[0][1] - y < ( x + width ) - PP[0][0] ) )
{
Log.i("Colision", "Top Left - Right Side");

player.x = ( x + width ) + 1;
player.Vh = player.phy.getVelsoityWallColision(player.Vh, player.Cr);
}
// Collision On Bottom
else if( PP[0][1] > ( y + height/2 ) )
{
Log.i("Colision", "Top Left - Bottom Side");

player.y = ( y + height ) + 1;
if( player.Vv > 0 ) player.Vv = 0;
}

return true;
}

// TOP RIGHT - PLAYER //

else if( ( PP[1][0] > x && PP[1][0] < x + width ) && ( PP[1][1] > y && PP[1][1] < y + height ) && ( (x - player.x) > 0 ) )
{
player.isColided = true;
//player.isSpinning = false;

// Collision On Left
if( PP[1][0] < ( x + width/2 )  && ( PP[1][0] - x < PP[1][1] - y ) )
{
Log.i("Colision", "Top Right - Left Side");

player.x = ( x ) + 1;
player.Vh = player.phy.getVelsoityWallColision(player.Vh, player.Cr);
}
// Collision On Bottom
else if( PP[1][1] > ( y + height/2 ) )
{
Log.i("Colision", "Top Right - Bottom Side");

player.y = ( y + height ) + 1;
if( player.Vv > 0 ) player.Vv = 0;
}

return true;
}

// BOTTOM RIGHT - PLAYER //

else if( ( PP[2][0] > x && PP[2][0] < x + width ) && ( PP[2][1] > y && PP[2][1] < y + height ) )
{
player.isColided = true;
//player.isSpinning = false;

// Collision On Left
if( PP[2][0] < ( x + width/2 ) && ( PP[2][0] - x < PP[2][1] - y ) )
{
Log.i("Colision", "Bottom Right - Left Side");

player.x = ( x ) + 1;
player.Vh = player.phy.getVelsoityWallColision(player.Vh, player.Cr);
}
// Collision On Top
else if( PP[2][1] < ( y + height/2 ) )
{
Log.i("Colision", "Bottom Right - Top Side");

player.y = y - player.height;
player.Vv = player.phy.getVelsoityWallColision(player.Vv, player.Cr);
//player.Vh = -1 * ( player.phy.getVelsoityWallColision(player.Vv, player.Cr) );
int rs = x - player.x;
Log.i("RS", String.format("%d", rs));
if( rs > 0 )
{
player.direction = -1;
player.isSpinning = true;
player.Vh = -0.5 * ( player.phy.getVelsoityWallColision(player.Vv, player.Cr) );
}
if( rs < 0 )
{
player.direction = 1;
player.isSpinning = true;
player.Vh = 0.5 * ( player.phy.getVelsoityWallColision(player.Vv, player.Cr) );
}
player.rotateSpeed = 1 * rs;
}

return true;
}

// BOTTOM LEFT - PLAYER //

else if( ( PP[3][0] > x && PP[3][0] < x + width ) && ( PP[3][1] > y && PP[3][1] < y + height ) )//&& ( (x - player.x) > 0 ) )
{
player.isColided = true;
//player.isSpinning = false;

// Collision On Right
if( PP[3][0] > ( x + width/2 ) && ( PP[3][1] - y < ( x + width ) - PP[3][0] ) )
{
Log.i("Colision", "Bottom Left - Right Side");

player.x = ( x + width ) + 1;
player.Vh = player.phy.getVelsoityWallColision(player.Vh, player.Cr);
}
// Collision On Top
else if( PP[3][1] < ( y + height/2 ) )
{
Log.i("Colision", "Bottom Left - Top Side");

player.y = y - player.height;
player.Vv = player.phy.getVelsoityWallColision(player.Vv, player.Cr);
//player.Vh = -1 * ( player.phy.getVelsoityWallColision(player.Vv, player.Cr) );
int rs = x - player.x;
//Log.i("RS", String.format("%d", rs));
//player.direction = -1;
//player.isSpinning = true;
if( rs > 0 )
{
player.direction = -1;
player.isSpinning = true;
player.Vh = -1 * ( player.phy.getVelsoityWallColision(player.Vv, player.Cr) );
}
if( rs < 0 )
{
player.direction = 1;
player.isSpinning = true;
player.Vh = 1 * ( player.phy.getVelsoityWallColision(player.Vv, player.Cr) );
}
player.rotateSpeed = 1 * rs;
}

//catch (InterruptedException e) {}
return true;
}

else
{
player.isColided = false;
player.isSpinning = true;
}

return false;
}


There is a problem with the bottom right:

{player.x + player.height, player.y + player.width }


must be

{player.x + player.width, player.y + player.height }


Instead of reinventing the wheel, I recommend you use a library that will handle collision detection (and possibly physics) for you. JBox2D will handle collision detection and physics for you. If you want to learn to create your own physics / collision engine, take a look at its source code.