Search code examples
javaandroidcollision-detection

Collision detection with bitmap Android


Alright, so i cant figure out how to make this rpg game im working for android have collision. I draw the map on the screen in a drawable portion that the character is located in. I move the character by a 64 amount across the screen and when it hits the bounds of the screen it will then move the map and look like the character is moving across the map. It stop when the map hits the end it doesnt move the screen any more. What im trying to do is figure out a away to check if my character is in a certain bitmap and not let it pass through. Here is the code for my player moving, and the map itself. The character and the tiles are bitmaps. Anything else needed to allow you to help me better comment and i will post more and how it works.

Edit: sp.yp and sp.xp is the position of the character on screen.

This Draw the map to the screen:

 public void draw(Canvas canvas){
            //How many tiles are on the screen length times width
    for(int x = 0; x <= 31;x++){
        for(int y = 0; y <= 17;y++){

        switch(Map[Mapx + x][Mapy + y]){
        case 0:
            canvas.drawBitmap(BLOCK_ROCK, x*32,y*32,null);
        break;
        case 1:
            canvas.drawBitmap(BLOCK_OCEAN, x*32,y*32,null);
        break;
        case 2:
            canvas.drawBitmap(BLOCK_GRASS, x*32,y*32,null);
        break;
        case 3:
            canvas.drawBitmap(BLOCK_ROCK, x*32,y*32,null);
        break;
        case 4:
            canvas.drawBitmap(BLOCK_FLOWER, x*32,y*32,null);
        break;
        }
        }
    }
}

This is the movment of the player, these methods are called when person hits the keypad i have drawn to the screen:

public void Down(){
    if(sp.yp == 512){
    if(w.Mapy == w.mapheight - 17 - 1){

    }else{
    w.Mapy +=1;
    }
    }else{
    sp.setYd(64);
    sp.update();
    sp.setYd(0);
    }
}
public void Left(){
    if(sp.xp == box.xMin + 32){
    sp.isRight = false;
    if(w.Mapx == 0){

    }else{
        w.Mapx -=1;
    }
    }else{
    sp.isRight = false;
    sp.setXd(-64);
    sp.update();
    sp.xd = 0;
    }
}
public void Jump(){
    if(sp.yp == 64){
    if(w.Mapy == 0){

    }else{
    w.Mapy -=1;
    }
    }else{
    sp.setYd(-64);
    sp.update();
    sp.setYd(0);
    }
}
public void Right(){
    if(sp.xp == 992){
    sp.isRight = true;
    if(w.Mapx == w.mapwidth - 31 - 1){

    }else{
        w.Mapx +=1;
    }
    }else{
    sp.isRight = true;
    sp.setXd(64);
    sp.update();
    sp.xd = 0;
    }
}

Solution

  • You should look into a game engine to handle this type of thing. There are several that are easy to import into your project and provide a great deal of functionality so you can work on designing the game and media resources more. Trust me, you probably don't want to code the whole engine.

    Check AndEngine - http://www.andengine.org/

    Libgdx - http://code.google.com/p/libgdx/

    I suggest AndEngine because there is a great app of simple examples you can use to experiment with. You can find it on:

    code.google.com/p/ andengineexamples/

    (no space in the address... sorry couldn't post more than two links)