[SOLVED] Problem displaying an isometric map portion

Hello

Im having touble trying to display an isometric map portion (The diamond that covers all the visible screen) on the screen. I dont really know why is wrong with my code.

Can somebody help, please.

This is the code im using for the map builder:


public pruebaGM(int ancho, int alto, int mosx, int mosy, int a[][], Applet p){
		
		//Applet padre
		padre = p;
		
		//Medidas del padre
		pantancho = ancho;
		pantalto = alto;
		
		//Creacion de mosaico modelo
		prototipo = new Mosaico(0, p);
		
		//Medidas de mosaico modelo
		mosancho = prototipo.ancho;
		mosalto = prototipo.alto;
		
		//Mitad de las medidas para puntos de referencia
		mosanchoMT = (mosancho/2)-1;
		mosaltoMT = mosalto/2;
		
		//Mosaicos visibles segun tamano de la pantalla
		mosavis = Math.round(pantancho*2/mosancho);
		//mosavisy = Math.round((pantalto*2)/mosaltoMT);
		
		//Referencia para centralizar mapa
		offsetH = Math.round(pantancho/2-mosanchoMT);
		//offsetV = Math.round((pantalto-(mosavisy*mosalto))/2);
		
		//Mitad de los mosaicos visibles
		mosavisMT = Math.round(mosavis/2);
		//mosavisyMT = Math.round(mosavisy/2);
		
		//Mosaicos de definicion de boundaries
		mosaX1 = mosx-mosavisMT;
		mosaX2 = mosx+mosavisMT;
		mosaY1 = mosy-mosavisMT;
		mosaY2 = mosy+mosavisMT;
		
		//Lienzo donde sera plasmado el mapa
		grid = new BufferedImage(ancho, alto, BufferedImage.TRANSLUCENT);
		
		//Creando interfaz grafica
		pintor = grid.createGraphics();
		
		//Buffer para registro de mosaico visible
		mosanum = 0;
		
		System.out.println("Eje de X: "+mosaX1+"-"+mosaX2);
		System.out.println("Eje de Y: "+mosaY1+"-"+mosaY2);
		//Bucle de construccion
		for(int i=mosaX1; i<mosaX2; i++){
			for(int j=mosaY1; j<mosaY2; j++){
				try{
					System.out.println(i);
					mosagrid = new Mosaico[mosavis*2];
					mosagrid[mosanum] = new Mosaico(a[i][j], p);
					mosagrid[mosanum].posX = ((j*mosanchoMT)-(i*mosanchoMT))+offsetH;
					mosagrid[mosanum].posY = ((j*mosaltoMT)+(i*mosaltoMT));//+offsetV;
					mosagrid[mosanum].paint(pintor);
					mosanum++;
				}catch(Exception ex){}
			}
		}
	}

First, change:


catch(Exception ex){}

to


catch(Exception ex) { exception.printStackTrace(); }

I’m assuming that you’re probably hitting something like a null pointer exception, but you’re catching it without knowing it. Run your program with that and see whether you are actually throwing an exception or not. If you are, tell us which and what line (With the code snippet that surrounds that line). If not, tell us what the program’s doing and what you’re expecting it to do. :3

Hi and thanks for the answer.

the exception is: java.lang.ArrayIndexOutOfBoundsException: -12

what im hoping to achieve is measure the diamond shape of the map that sorrounds the screen and display it taking as the center the “mosx” and “mosy” tile. The bidimentional iteration is passing the array of the map negative values, because i set my starting tile as (0,0).

Ah, that’s the exception I meant. Not Null Pointer.

I think that there are several issues with your loop, mostly coming from how you’re declaring/using various variables. This is some guess work, because a lot of understanding of code comes from understanding the intent, and I can only just decipher what your variables mean. :3

Ahem!

Issue one is happening every iteration (Meaning that you’re tossing out the previous array on every iteration, which might not be your intent).
mosagrid = new Mosaico[mosavis*2];
Issue two is that mosagrid is being indexed by mosanum.
Let’s say that mosavis = 6.
Then, let’s say that (mosaX2 - mosaX1) == 6, and (mosaY2 - mosaY1) ==6.
You have:


mosanum = 0;
for(int i = 0; i < 6; i++) {
    for(int j = 0; j < 6; j++) {
        mosagrid = new Mosaic[6*2]; // 12!
        mosanum++;
    }
}

If we follow the iterations:
i = 0, j = 0: mosanum = 0
i = 0, j = 1: mosanum = 1
i = 0, j = 2: mosanum = 2

i = 0, j = 5: mosanum = 5
i = 1, j = 0: mosanum = 6
i = 1, j = 1: mosanum = 7

i = 1, j = 5: mosanum = 11
i = 2, j = 0: mosanum = 12 (EXEPTION!)

You are right my friend. i decided to get “mosagrid = new Mosaico[mosavis*2];” out of the “for” loops (it had to be that way from the begining).

For the error number two maybe “mosagrid” is not the problem, cause it only counts the iterations ocurred. But thanks of you pointing that out, i think i discovered what went wrong with my code.

If i declare the “mosagrid” length to be "mosavis2", im saying that mosagrid´s length is 24 (122). But it doesnt. The total number of iterations will be (12*12).

Im applying those changes, and see if i can fix it. Ill let you know.

Now it is perfect!!!

Thanks a lot.

Glad to have helped, goodluck! :smiley:

Would you mind posting a screenshot? I would love to see it working :slight_smile:

Sure,
Sorry for the late response. Here is the pic:

Thanks! That looks nice!

Please don’t forget to keep posting your progress :slight_smile:

By the way, I kind of feel like the map itself looks weird, why don’t you just make it a diamond? So the visible area is even in every axis. I hope you can understand what I mean :slight_smile: Keep it up!

Correct me if I am wrong but there is diamond in that screenshot, but its too big so only top is shown

Indeed a nice cell-shaded type of look to it. But… a yellow background. Why!?

Hehehe, the yellow background and the green tiles are just placeholders :smiley:

Just like Fokusas said, the map used to be a diamon shapped thing, but then, i realized that the tiles that go offscreen were adding some innecesary cpu cicles to the whole composition process. i decided just to show the tiles that fit on the screen, based on a current character position (in this case, the gray tile (10,10)).

What you can see now (looks like a little house indeed :p) is the top roof of the map, because the starting position tile (10, 10) is nearer (0,0).

What im working right now, is the movement. Im developing some kind of engine that draws new tiles in position as they appear, and erase old tiles as they go offscreen.