java - Isometric Tile Picking Giving Negative Values -



java - Isometric Tile Picking Giving Negative Values -

i need find coordinates isometric tiles when it's clicked. i've got figured out. y values returning correctly, apparently x formula wrong , i'm receiving negative values.

exhibit a:

the grayness tile supposed 0, 0 reason i'm getting -5...

here's code.

here create map (probably not relevant question here show how i'm doing things). objectid enum storing tile types.

objectid tile_map[][] = new objectid[][] { {objectid.ground_concrete, objectid.ground_grass, objectid.ground_grass, objectid.ground_grass}, {objectid.ground_grass, objectid.ground_grass, objectid.ground_grass, objectid.ground_grass}, {objectid.ground_grass, objectid.ground_grass, objectid.ground_grass, objectid.ground_grass}, {objectid.ground_grass, objectid.ground_grass, objectid.ground_grass, objectid.ground_grass}, {objectid.ground_grass, objectid.ground_grass, objectid.ground_grass, objectid.ground_grass}, };

we load map looping through array above , adding them linkedlist (via handler.addobject()) storing x , y values , tile type, widths , heights.

private void loadmap(objectid map[][]) { int x = offsetx, y = offsety; //int tile_width = 128, tile_height = 128; for(int = 0; < map.length; i++) { for(int j = map[i].length-1; j >= 0; j--) { handler.addobejct(new objground(x + (j * map[i][j].getwidth() / 2) + (i * map[i][j].getwidth() / 2), y + (i * map[i][j].getbottomgroundplane() / 2) - (j * map[i][j].getbottomgroundplane() / 2), map[i][j])); } } }

our mouselistener

private class mouselistener extends mouseinputadapter { public void mousepressed(mouseevent e) { if(!dragging) { gettilebyxy(e.getx(), e.gety()); } } public void mousedragged(mouseevent e) { //system.out.println("mosue pressed"); if(swingutilities.isrightmousebutton(e)) { if(e.getx() != mx || e.gety() != my) { dragging = true; if(e.getx() < mx) { offsetx -= (mx - e.getx()); } else { offsetx += (e.getx() - mx); } if(e.gety() < my) { offsety -= (my - e.gety()); } else { offsety += (e.gety() - my); } mx = e.getx(); = e.gety(); //system.out.println("ox: " + offsetx + " oy: " + offsety); } } } ... }

and our tile getter main problem lies @ htx:

private void gettilebyxy(int x, int y) { int tilew = 127; int tileh = 63; x += (tilew * 2); y += (tileh * 2); int htx = (int)(((double)(offsetx - x) / (double)tilew) + ((double)(offsety - y) / (double)tileh) - 0.5); int hty = (int)(((double)(offsety - y) / (double)tileh) - ((double)(offsetx - x) / (double)tilew) + 0.5); system.out.println("htx: " + htx + " hty: " + hty); }

experimenting photoshop , excel , assuming origin top left, offsetx left side of left-most cell , offsety bottom of left-most cell:

int htx = (int)(((double)(x - offsetx) / (double)tilew) - ((double)(offsety - y) / (double)tileh) + 0.5); int hty = (int)(((double)(offsety - y) / (double)tileh) + ((double)(x - offsetx) / (double)tilew) - 0.5);

java game-engine isometric

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -