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?