1

I'm developing a game for android using opengl es 2.0, and I use a height map bitmap to create a terrain. That works ok.

During the game, I have classes which represent entities in the game which store in their instances their (x,y) positions. To get the z value, i use a method that queries the height map bitmap.

This is the method:

public float zAt(float pos[]){
    float xPos = pos[0] + terrainWidth/2;
    float yPos = height-(pos[1] + terrainHeight/2);

    int bitMapWidth = zMap.getWidth();
    int bitMapHeight = zMap.getHeight();

    int pixelX= (int) (((bitMapWidth-1)  * xPos) / terrainWidth);
    int pixelY = (int) (((bitMapHeight-1) * yPos) / terrainHeight);

    if(pixelX < 0 || pixelX >= bitMapWidth || pixelY < 0 || pixelY >= bitMapHeight)
        return 0;
    else{
        System.out.println("DEBUG! >> "+"("+pixelX+","+pixelY+"):"+zMap.getPixel(pixelX, pixelY)+" <<");
        float toReturn = ((zMap.getPixel(pixelX, pixelY) & 0xFF)-(255f/2f))/zDiv;

        return toReturn;
    }
}

When the game begins, everything is fine. But after some game loops, I noticed that my objects in the game always stay in z=0. I went to check the zAt method, throwing that println in there and I verified that in fact, after some time (this time differs each time i run the game), the zMap.getPixel starts returning 0.

This is such a strange problem because I don't change my bitmap in the course of the game.

Sometimes it even flickers for a bit, i.e, getPixel returns 0, then returns the right value, then returns 0 again... most of the times ending up always returning 0.

This is how I load the bitmap:

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;

    Bitmap terrainBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.heightmap, options);

Does somebody have a clue on why this is happening?

jmacedo
  • 773
  • 1
  • 13
  • 24
  • are the xPos and yPos are always (did you make assertions etc.) between (0..1)? – mehmetminanc Sep 14 '12 at 16:32
  • @Mehmet M. I. xPos and yPos aren't between 0 and 1, and they don't need to be. pos[] is in game/opengl coordinates and a series of transformations are applied, as you can see, to get the coordinates of the corresponding pixel to that position. In the log produced, one can see things like: `DEBUG! >> (408,300):453213 <<` ..and some game loops later... `DEBUG! >> (408,300):0 <<` which implies something happens in between to the bitmap, and that the coordinates transformation to pixels are not the problem – jmacedo Sep 14 '12 at 17:20

1 Answers1

0

If either the pixelX or pixelY value is greater then the respective dimension of the bitmap, you will get 0 returned. Verify the pixelX and pixelY values before stepping into this code section:

if(pixelX < 0 || pixelX >= bitMapWidth || pixelY < 0 || pixelY >= bitMapHeight)
    return 0;
Maz
  • 764
  • 5
  • 5
  • That'S 0 in that case, yes, but thats not the problem, since that is the desired behavior . What is happening is possible to observe through the println (see my comment to mehmet's answer) which automatically ensures that it is the else part which is beeing run. In fact,the very same pixel coordinates dont have always the same getPixel value which is the really weird thing. It almost looks as if somebody painted the whole bitmap in black at a random time after the program starts. – jmacedo Sep 17 '12 at 03:34