0
\$\begingroup\$

I am currently developing a tile based game in Java and trying to add the option of zooming. I do the zoom by reducing and enlarging the tiles, but the problem is that it creates spaces between the tiles.

The default form, in which I created all of these tiles from the beginning:

enter image description here

When zooming out - width -= 2 and height -= 2:

enter image description here

Bigger zooming out:

enter image description here

My question is very simple, how do I zoom in and out on a tile based world?

\$\endgroup\$
  • \$\begingroup\$ To be clear, do you want your method fixed, or do you want to know how to zoom in java swing? \$\endgroup\$ – Weckar E. Aug 16 at 12:18
  • \$\begingroup\$ @WeckarE. Fixed. It's just that swing will be more understandable for me. \$\endgroup\$ – RonDev Aug 16 at 12:22
0
\$\begingroup\$

Your idea of zooming is wrong. I believe your problem is to distinguish between real coordinates and sizes to scaled coordinates and sizes. In other words, you need to respect the position and size of the tiles, while scaling them down. The Tiles themselfs only have the information, what position and width they have in world space.
enter image description here

This is without the zoom. In this example, every tile is 1 wide and 1 tall, this is obviously not the size in pixel, as a tile is more like 64 pixels in size. Lets just say, you paint a cube by giving it the top left corner as base position and the size for width and height. so public drawCube(int posx, int posy, int size) for example. Now your cube object has pos (0, 0) with width 1 in world space. Your object would be drawn with the top left corner being (0,0), but since you draw pixels, the bottom right corner would be at (63, 63). The cube with pos (1,1) would then be painted at top right (64, 64) to bottom left (127, 127). This is only, if you have no zoom.

Now lets zoom out by 0.5, so everything has only half the size. Your topleft cube Object still has pos (0,0) and size 1, but if you call public drawCube(int posx, int posy, int size) on it. now you have to calculate position and size in pixel. In pixels, the top left is still (0,o), but your cube is only 32 pixels wide and tall, so the bottom right corner is (31, 31). The cube at (1,1) will then be drawn at top left (32, 32) to bottom right (63, 63). Basically you have to half the position values and the size values.

Obviously the cube does not need to know, what zoom factor your game currently has. For that you normally have a camera object that knows the the camera position (in world space). Your renderer then uses those informations to draw the tiles at the right position with the right size. But that is way more complicated to explain and needs alot more informations on your current code.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.