Skip to main content
pseudo code -> java, but still not working as intended
Source Link
corey
  • 109
  • 3

EDIT- This is my implementation of the pseudo code. In the log, 'hit true' is never thrown but i probably got the vector->separate x/y values and the for for loops wrong.

public boolean gethit(obstacle second){ int ox = second'hit trying' is.obx; int oy = second Its not working as intended.oby; int oh = second.obh; int ow = second.obw;

public boolean gethit(obstacle second){

        int AABBxpx, AABBypy, AABBwph,pw;
 AABBh;       
        Bitmap sprite1 = rotatedbm;
        Bitmap sprite2 = second.getgfx();
        
        px = playerx;
        py = playery;
        ph = sprite1.getHeight();
        pw = sprite1.getWidth();
        

        int alpha1;ox = second.obx;
        int alpha2;oy = second.oby;
        int oh = second.obh;
AABBw        int ow = playerwsecond.obw;
 -       
        int [] AABB1bl = {px, py+ph};
        int [] AABB1tr = {px+pw, py};
        int [] AABB2bl = {ox, oy+oh};
        int [] AABB2tr = {ox+ow, oy};
        
        AABB2bl[0] -= playerx;AABB1bl[0];
AABBh        AABB2bl[1] -= playerhAABB1bl[1]; 
        
        AABB2tr[0] -= oyAABB1bl[0];
        AABB2tr[1] -= playery;AABB1bl[1];
AABBx        
        AABB1tr[0] -= playerxAABB1bl[0];
 + playerw      AABB1tr[1] -= AABBw;AABB1bl[1];
AABBy        
        AABB1bl[0] -= playeryAABB1bl[0];
 + playery      AABB1bl[1] -= AABBh;AABB1bl[1];
        
        int start_x = AABB2bl[0];
        int start_y = AABB2tr[1];
        int end_x = AABB1tr[0];
        int end_y = AABB1bl[1];
        
        int ALPHAMASK = 0xFF<<24; // assuming colors are represented as 0xAARRBBGG
        
Bitmap sprite1 = rotatedbm;     int alpha1;
Bitmap sprite2 = second     int alpha2;
        
        
        Log.getgfxi("hit", "trying");
 
        for(int x = 0;start_x; x < Math.abs(AABBw);end_x; x++) 
            for(int y = 0;start_y; y < Math.abs(AABBh);end_y; y++) {  
              alpha1 = sprite1.getPixel(playerw-AABBw+xx , playery-AABBh+yy*sprite1.getWidth()) & ALPHAMASK; 
  
             alpha2 = sprite2.getPixel(x + AABB2bl[0], y(y+AABB2tr[1])*sprite2.getWidth()) & ALPHAMASK;
 
              if(alpha1 != 0 && alpha2 != 0) {
                Log.i("hit", "true");
                return true; // you'd probably wish to return after a colliding pixel has been found
              }
            }
        return false;
}

}

EDIT- This is my implementation of the pseudo code, but i probably got the vector->separate x/y values and the for for loops wrong.

public boolean gethit(obstacle second){ int ox = second.obx; int oy = second.oby; int oh = second.obh; int ow = second.obw;

int AABBx, AABBy, AABBw, AABBh;
        
int alpha1;
int alpha2;
        
AABBw = playerw - ox - playerx;
AABBh = playerh - oy - playery;
AABBx = playerx + playerw - AABBw;
AABBy = playery + playery - AABBh;

int ALPHAMASK = 0xFF<<24; // assuming colors are represented as 0xAARRBBGG
        
Bitmap sprite1 = rotatedbm;
Bitmap sprite2 = second.getgfx();
 
for(int x = 0; x < Math.abs(AABBw); x++) 
for(int y = 0; y < Math.abs(AABBh); y++) {  
    alpha1 = sprite1.getPixel(playerw-AABBw+x, playery-AABBh+y) & ALPHAMASK;  
     alpha2 = sprite2.getPixel(x, y) & ALPHAMASK;
 
    if(alpha1 != 0 && alpha2 != 0) {
        return true;
    }
}
return false;

}

EDIT- This is my implementation of the pseudo code. In the log, 'hit true' is never thrown but 'hit trying' is. Its not working as intended...

public boolean gethit(obstacle second){

        int px,py,ph,pw;
        
        Bitmap sprite1 = rotatedbm;
        Bitmap sprite2 = second.getgfx();
        
        px = playerx;
        py = playery;
        ph = sprite1.getHeight();
        pw = sprite1.getWidth();
        

        int ox = second.obx;
        int oy = second.oby;
        int oh = second.obh;
        int ow = second.obw;
        
        int [] AABB1bl = {px, py+ph};
        int [] AABB1tr = {px+pw, py};
        int [] AABB2bl = {ox, oy+oh};
        int [] AABB2tr = {ox+ow, oy};
        
        AABB2bl[0] -= AABB1bl[0];
        AABB2bl[1] -= AABB1bl[1]; 
        
        AABB2tr[0] -= AABB1bl[0];
        AABB2tr[1] -= AABB1bl[1];
        
        AABB1tr[0] -= AABB1bl[0];
        AABB1tr[1] -= AABB1bl[1];
        
        AABB1bl[0] -= AABB1bl[0];
        AABB1bl[1] -= AABB1bl[1];
        
        int start_x = AABB2bl[0];
        int start_y = AABB2tr[1];
        int end_x = AABB1tr[0];
        int end_y = AABB1bl[1];
        
        int ALPHAMASK = 0xFF<<24; // assuming colors are represented as 0xAARRBBGG
        
        int alpha1;
        int alpha2;
        
        
        Log.i("hit", "trying");
        for(int x = start_x; x < end_x; x++) 
            for(int y = start_y; y < end_y; y++) {  
              alpha1 = sprite1.getPixel(x , y*sprite1.getWidth()) & ALPHAMASK; 
              alpha2 = sprite2.getPixel(x + AABB2bl[0], (y+AABB2tr[1])*sprite2.getWidth()) & ALPHAMASK;
              if(alpha1 != 0 && alpha2 != 0) {
                Log.i("hit", "true");
                return true; // you'd probably wish to return after a colliding pixel has been found
              }
            }
        return false;
}
added 941 characters in body
Source Link
corey
  • 109
  • 3

I am making a game for android which I need hit detection between two objects. The first bitmap I have is a flying superhero, and the second bitmap is anything that he collides with.

The superhero bitmap is created on the fly by rotating the original bitmap between -90 and 90 degrees. As a result of the rotation, the bounding box is much, much larger than the non-rotated version. I have code to detect if the bounding box of the player hits any object, but visually this creates a problem when the object comes in contact with the skewed bounding box.

I need a solution that computes pixel-perfect hit detection between the player and the object, not just the bounding boxes.

I have been searching for the past 5 days for examples, tutorials, code snippets, or anything that could help me, but I haven't found a solution. The solution I'm looking for would be simply one function call similar to public boolean(obstacle theobject), where the obstacle's bitmap. coordinates, width, and height are returned with obstacle.getbitmap, obstacle.getx, obstacle.gety, obstacle.getwidth, and obstacle.getheight. The bitmap of the player is named rotatedbm and has global variables playerx, playery, playerh, and playerw.

I do not wish to implement external game engines or libraries.

I have read much on using int arrays with Bitmap.getPixels, and then comparing the arrays, but I cannot grasp the math behind comparing the arrays nor sorting out alpha values. I cannot afford to stay stuck on this one problem much long for the sake of development.

Thank you

EDIT- This is my implementation of the pseudo code, but i probably got the vector->separate x/y values and the for for loops wrong.

public boolean gethit(obstacle second){ int ox = second.obx; int oy = second.oby; int oh = second.obh; int ow = second.obw;

int AABBx, AABBy, AABBw, AABBh;
        
int alpha1;
int alpha2;
        
AABBw = playerw - ox - playerx;
AABBh = playerh - oy - playery;
AABBx = playerx + playerw - AABBw;
AABBy = playery + playery - AABBh;

int ALPHAMASK = 0xFF<<24; // assuming colors are represented as 0xAARRBBGG
        
Bitmap sprite1 = rotatedbm;
Bitmap sprite2 = second.getgfx();

for(int x = 0; x < Math.abs(AABBw); x++) 
for(int y = 0; y < Math.abs(AABBh); y++) {  
    alpha1 = sprite1.getPixel(playerw-AABBw+x, playery-AABBh+y) & ALPHAMASK;  
    alpha2 = sprite2.getPixel(x, y) & ALPHAMASK;

    if(alpha1 != 0 && alpha2 != 0) {
        return true;
    }
}
return false;

}

I am making a game for android which I need hit detection between two objects. The first bitmap I have is a flying superhero, and the second bitmap is anything that he collides with.

The superhero bitmap is created on the fly by rotating the original bitmap between -90 and 90 degrees. As a result of the rotation, the bounding box is much, much larger than the non-rotated version. I have code to detect if the bounding box of the player hits any object, but visually this creates a problem when the object comes in contact with the skewed bounding box.

I need a solution that computes pixel-perfect hit detection between the player and the object, not just the bounding boxes.

I have been searching for the past 5 days for examples, tutorials, code snippets, or anything that could help me, but I haven't found a solution. The solution I'm looking for would be simply one function call similar to public boolean(obstacle theobject), where the obstacle's bitmap. coordinates, width, and height are returned with obstacle.getbitmap, obstacle.getx, obstacle.gety, obstacle.getwidth, and obstacle.getheight. The bitmap of the player is named rotatedbm and has global variables playerx, playery, playerh, and playerw.

I do not wish to implement external game engines or libraries.

I have read much on using int arrays with Bitmap.getPixels, and then comparing the arrays, but I cannot grasp the math behind comparing the arrays nor sorting out alpha values. I cannot afford to stay stuck on this one problem much long for the sake of development.

Thank you

I am making a game for android which I need hit detection between two objects. The first bitmap I have is a flying superhero, and the second bitmap is anything that he collides with.

The superhero bitmap is created on the fly by rotating the original bitmap between -90 and 90 degrees. As a result of the rotation, the bounding box is much, much larger than the non-rotated version. I have code to detect if the bounding box of the player hits any object, but visually this creates a problem when the object comes in contact with the skewed bounding box.

I need a solution that computes pixel-perfect hit detection between the player and the object, not just the bounding boxes.

I have been searching for the past 5 days for examples, tutorials, code snippets, or anything that could help me, but I haven't found a solution. The solution I'm looking for would be simply one function call similar to public boolean(obstacle theobject), where the obstacle's bitmap. coordinates, width, and height are returned with obstacle.getbitmap, obstacle.getx, obstacle.gety, obstacle.getwidth, and obstacle.getheight. The bitmap of the player is named rotatedbm and has global variables playerx, playery, playerh, and playerw.

I do not wish to implement external game engines or libraries.

I have read much on using int arrays with Bitmap.getPixels, and then comparing the arrays, but I cannot grasp the math behind comparing the arrays nor sorting out alpha values. I cannot afford to stay stuck on this one problem much long for the sake of development.

Thank you

EDIT- This is my implementation of the pseudo code, but i probably got the vector->separate x/y values and the for for loops wrong.

public boolean gethit(obstacle second){ int ox = second.obx; int oy = second.oby; int oh = second.obh; int ow = second.obw;

int AABBx, AABBy, AABBw, AABBh;
        
int alpha1;
int alpha2;
        
AABBw = playerw - ox - playerx;
AABBh = playerh - oy - playery;
AABBx = playerx + playerw - AABBw;
AABBy = playery + playery - AABBh;

int ALPHAMASK = 0xFF<<24; // assuming colors are represented as 0xAARRBBGG
        
Bitmap sprite1 = rotatedbm;
Bitmap sprite2 = second.getgfx();

for(int x = 0; x < Math.abs(AABBw); x++) 
for(int y = 0; y < Math.abs(AABBh); y++) {  
    alpha1 = sprite1.getPixel(playerw-AABBw+x, playery-AABBh+y) & ALPHAMASK;  
    alpha2 = sprite2.getPixel(x, y) & ALPHAMASK;

    if(alpha1 != 0 && alpha2 != 0) {
        return true;
    }
}
return false;

}

I am making a game for android which I need hit detection between two objects. The first bitmap I have is a flying superhero, and the second bitmap is anything that he collides with.

The superhero bitmap is created on the fly by rotating the original bitmap between -90 and 90 degrees. As a result of the rotation, the bounding box is much, much larger than the non-rotated version. I have code to detect if the bounding box of the player hits any object, but visually this creates a problem when the object comes in contact with the skewed bounding box.

I need a solution that computes pixel-perfect hit detection between the player and the object, not just the bounding boxes.

I have been searching for the past 5 days for examples, tutorials, code snippets, or anything that could help me, but I haven't found a solution. The solution I'm looking for would be simply one function call similar to public boolean(obstacle theobject), where the obstacle's bitmap. coordinates, width, and height are returned with obstacle.getbitmap, obstacle.getx, obstacle.gety, obstacle.getwidth, and obstacle.getheight. The bitmap of the player is named rotatedbm and has global variables playerx, playery, playerh, and playerw.

I do not wish to implement external game engines or libraries.

I have read much on using int arrays with Bitmap.getPixels, and then comparing the arrays, but I cannot grasp the math behind comparing the arrays nor sorting out alpha values. I cannot afford to stay stuck on this one problem much long for the sake of development.

I will pay $50 paypal or money order to anyone who can come up with an implementable solution. Please contact me at [email protected] for any further information or questions.

Thank you

I am making a game for android which I need hit detection between two objects. The first bitmap I have is a flying superhero, and the second bitmap is anything that he collides with.

The superhero bitmap is created on the fly by rotating the original bitmap between -90 and 90 degrees. As a result of the rotation, the bounding box is much, much larger than the non-rotated version. I have code to detect if the bounding box of the player hits any object, but visually this creates a problem when the object comes in contact with the skewed bounding box.

I need a solution that computes pixel-perfect hit detection between the player and the object, not just the bounding boxes.

I have been searching for the past 5 days for examples, tutorials, code snippets, or anything that could help me, but I haven't found a solution. The solution I'm looking for would be simply one function call similar to public boolean(obstacle theobject), where the obstacle's bitmap. coordinates, width, and height are returned with obstacle.getbitmap, obstacle.getx, obstacle.gety, obstacle.getwidth, and obstacle.getheight. The bitmap of the player is named rotatedbm and has global variables playerx, playery, playerh, and playerw.

I do not wish to implement external game engines or libraries.

I have read much on using int arrays with Bitmap.getPixels, and then comparing the arrays, but I cannot grasp the math behind comparing the arrays nor sorting out alpha values. I cannot afford to stay stuck on this one problem much long for the sake of development.

I will pay $50 paypal or money order to anyone who can come up with an implementable solution. Please contact me at [email protected] for any further information or questions.

Thank you

I am making a game for android which I need hit detection between two objects. The first bitmap I have is a flying superhero, and the second bitmap is anything that he collides with.

The superhero bitmap is created on the fly by rotating the original bitmap between -90 and 90 degrees. As a result of the rotation, the bounding box is much, much larger than the non-rotated version. I have code to detect if the bounding box of the player hits any object, but visually this creates a problem when the object comes in contact with the skewed bounding box.

I need a solution that computes pixel-perfect hit detection between the player and the object, not just the bounding boxes.

I have been searching for the past 5 days for examples, tutorials, code snippets, or anything that could help me, but I haven't found a solution. The solution I'm looking for would be simply one function call similar to public boolean(obstacle theobject), where the obstacle's bitmap. coordinates, width, and height are returned with obstacle.getbitmap, obstacle.getx, obstacle.gety, obstacle.getwidth, and obstacle.getheight. The bitmap of the player is named rotatedbm and has global variables playerx, playery, playerh, and playerw.

I do not wish to implement external game engines or libraries.

I have read much on using int arrays with Bitmap.getPixels, and then comparing the arrays, but I cannot grasp the math behind comparing the arrays nor sorting out alpha values. I cannot afford to stay stuck on this one problem much long for the sake of development.

Thank you

Tweeted twitter.com/#!/StackGameDev/status/76577251411505152
Source Link
corey
  • 109
  • 3
Loading