Search code examples
javaandroidcanvascollision

Android Rect Collision


i want to make a little game for android devices. All works... except the collision with the player and objects. Did i do something wrong? I have tried many of methods for checking collision. E. g. intersect, intersects, contains and a function with self made collision test.

Edit: My Problem is that nothing happens :)

    DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    private Bitmap player, enemy;
    private int speedrun=1, x= 0, y = height - height / 5, i=1, yg=height - height / 5 + 40, xe= 920, xp =width / 2 - width / 4;
    private Rect p, e;
    public static int speed=0;

//other code

@Override
    protected void onDraw(Canvas c)
    {
        c.drawColor(Color.CYAN);
        handelPlayer();
        p = new Rect(xp, y, 0, 0);
        wayrect = new Rect(x, yg, 0, 0);
        wayrect2 = new Rect(x + width, yg, 0, 0);
        e = new Rect(xe, yg - 250, 0, 0);
        c.drawBitmap(enemy, e.left, e.top, null);
        c.drawBitmap(player, p.left, p.top, null);
    }

    public void handelPlayer()
    {
        x -= speed*speedrun;
        xe -= speed*speedrun;
        if (x + width < 0)
            x = 0;
        if (xe < -100)
            xe = 920;
        if (MainActivity.touch == 1)
        {
            y -= 100;//jump
            MainActivity.touch = 0;
            i = 1;
        }
        if (y <= height - height / 5)
            y += 3 * i / 10; //gravity
        i++;

        if (p.intersect(e)) //collosision
            speedrun = 0;
    }

Solution

  • First of all, your rectangles goes from the top of your player bitmap to (0,0), the top left corner of your device. I image what you meant was: p = new Rect(xp, y, xp + player.getWidth(), y + player.getHeight()); and the same with e, see code below.

    Second, p.intersect(e) changes the rectangle p to the intersection if they intersect, so you should use Rect.intersects(p, e) instead.

    Thirdly, you're checking collision on the old position values, as you have not updated the rectangles after you changed the position.

    A quick fix might be to move the intersection test to the top of handelPlayer (minor note: handlePlayer would be the correct way to spell it), like this:

    protected void onDraw(Canvas c)
    {
        c.drawColor(Color.CYAN);
        handelPlayer();
        p = new Rect(xp, y, xp + player.getWidth(), y + player.getHeight());;
        wayrect = new Rect(x, yg, 0, 0);   // These rectangles also has their right bottom corner at (0,0), which might cause problems
        wayrect2 = new Rect(x + width, yg, 0, 0);
        e = new Rect(xe, yg - 250, xe + enemy.getWidth(), yg - 250 + enemy.getHeight()) ;
        c.drawBitmap(enemy, e.left, e.top, null);
        c.drawBitmap(player, p.left, p.top, null);
    }
    
    
    public void handelPlayer()
    {
        if (Rect.intersects(p, e)){ //collision
            speedrun = 0;
        }
    
        x -= speed*speedrun;
        xe -= speed*speedrun;
        if (x + width < 0)
            x = 0;
        if (xe < -100)
            xe = 920;
        if (MainActivity.touch == 1)
        {
            y -= 100;//jump
            MainActivity.touch = 0;
            i = 1;
        }
        if (y <= height - height / 5)
            y += 3 * i / 10; //gravity
        i++;
    
    
    }
    

    There might be another problem though, as you have not yet described what exactly happens.