0
\$\begingroup\$

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;
        }

        //try { Thread.sleep(1000, 0); } 
        //catch (InterruptedException e) {}
        return true;
    }

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


    return false;
}
\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

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 }
\$\endgroup\$
0
1
\$\begingroup\$

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.

I'm sorry if this didn't answer your question fully.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks, looking at the source code will be good but i don’t think i will use the library because i want to be able to understand what the engine is doing and that will be easier it i write it my self \$\endgroup\$
    – Dan
    Jul 10, 2012 at 23:40
  • \$\begingroup\$ are there any text books you know that would help \$\endgroup\$
    – Dan
    Jul 11, 2012 at 1:13
  • \$\begingroup\$ I haven't bought any good books on collision detection myself. Sorry. \$\endgroup\$
    – Oskar
    Jul 12, 2012 at 9:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .