'JAVA Scrollable isometric tilemap
I got some problems with a scrollable isometric tile map with a astar on it. On the one hand I got a moveable camera (screenrectangle) on the other I got my map origin. If I define my origin the astar path is correctly shown on the screen according to the selected tile without scrolling. But if i use my camera to scroll the map ( I only render the visible parts of the map, which even works for very huge maps without any performance problems), the path is shown incorrect. So my tile selection gives me the wrong tile. I got two functions to convert from iso to screen and vise versa which works. Here the relevant code:
//Scrolling
if(KeyInput.a)camera.x+=SCROLL_SPEED * elapsedTime;
if(KeyInput.d)camera.x-=SCROLL_SPEED * elapsedTime;
if(KeyInput.w)camera.y+=SCROLL_SPEED * elapsedTime;
if(KeyInput.s)camera.y-=SCROLL_SPEED * elapsedTime;
//rendering and translating camera
g2.translate(camera.x,camera.y);
world.render(g2);
g2.translate(-camera.x,-camera.y);
//render the map and the path
for(int y = -Fantasy.camera.y-Fantasy.TILE_SIZE.y;y<-Fantasy.camera.y+Fantasy.camera.height+Fantasy.TILE_SIZE.y;y+=Fantasy.TILE_SIZE.y/2) {
for(int x = -Fantasy.camera.x-Fantasy.TILE_SIZE.x;x< -Fantasy.camera.x+Fantasy.camera.width+Fantasy.TILE_SIZE.x;x+=Fantasy.TILE_SIZE.x/2) {
Point iso = Fantasy.screenToIso(new Point(x,y));
if(iso.x>=0 && iso.y>=0 && iso.x<Fantasy.WORLD_SIZE.x && iso.y<Fantasy.WORLD_SIZE.y) {
Point screen = Fantasy.isoToScreen(iso);
tiles[iso.y*Fantasy.WORLD_SIZE.x + iso.x].render(g2,screen);
}
}
}
if(path != null) {
for(Tile n : path) {
Point screen = Fantasy.isoToScreen(new Point(n.getX(),n.getY()));
g2.drawImage(Tile.tiles.getSpriteImage(0),screen.x,screen.y,40,20,null);
}
}
//Tile selection and astar
if(MouseInput.rightClick) {
path = findPath(new Point(0,0),Fantasy.getSelectedTile(MouseInput.rightmouseClicked));
System.out.println(Fantasy.getSelectedTile(MouseInput.rightmouseClicked));
}
The problem is I can either have a scrollable map with only the relevant parts of the map rendered on the screen (works) or I can have a scrollable map with working astar but without only drawing the relevant parts of the map, which makes it laggy.
How can i solve this?
public static Point screenToIso(Point screen) {
return new Point((screen.x / (TILE_SIZE.x/2) + (screen.y / (TILE_SIZE.y/2))-2*(origin.x+origin.y))/2,(screen.y / (TILE_SIZE.y/2) - (screen.x / (TILE_SIZE.x/2))+2*(origin.x-origin.y))/2);
}
public static Point isoToScreen(Point iso) {
return new Point((iso.x-iso.y)*(TILE_SIZE.x/2)+(origin.x*TILE_SIZE.x),(iso.x+iso.y)*(TILE_SIZE.y/2)+(origin.y*TILE_SIZE.y));
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
