Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Trouble with scrolling a map via SetViewOffset() while using SetViewZoom()

Author
Message
Eqqman
4
Years of Service
User Offline
Joined: 15th Jul 2015
Location:
Posted: 30th Jul 2015 09:53
Hello-

I'm having trouble generating code that can correctly scroll a map while supporting multiple zoom modes. I've worked out many examples on paper, but the results I get in-game don't match. I'm sure that part of the problem relates to misunderstanding when I should use values that are scaled to match the zoom level.

The idea is that if the screen size is larger than the zoomed map size in the x- or y-axis, then the map should be centered in the screen on that axis. Otherwise I should be able to move the map back and forth along that axis, while preventing the screen from scrolling past the map. My scrolling code for the y-axis is this:

At zoom level 1.0, I can correctly scale within the bounds of the map. However, if I'm at a higher zoom level, I can scroll past the map just slightly. How much I can go past the map appears to be scaling with the zoom level. I'm correctly blocked from scrolling if the zoomed size of the map is already less than the screen size. It's not clear to me why this code doesn't block scrolling at the correct distance when I zoom in.

Goo Goo G'Joob!
Eqqman
4
Years of Service
User Offline
Joined: 15th Jul 2015
Location:
Posted: 2nd Aug 2015 09:39 Edited at: 6th Aug 2015 21:32
Here's a picture to illustrate the problem. The leftmost snippet is at zoom level 100% and the screen offset is zero, so you can see how much black space is at the top of the map (my hex tiles are surrounded by black). When I zoom to 150% and 200%, you can see the black border has increased far more than that, but I'm not sure why my calculations would be in error.



Goo Goo G'Joob!
Eqqman
4
Years of Service
User Offline
Joined: 15th Jul 2015
Location:
Posted: 5th Aug 2015 07:57 Edited at: 5th Aug 2015 08:18
I'm very surprised there is no commentary here. It seems either people don't allow their users to adjust zoom levels on the fly, or they directly manage the magnification on their own without the use of SetViewZoom().

I'm positive my code is mathematically sound, but it doesn't produce the expected results. So, I go back and question my first principles. I removed the restrictions on how much I could scroll the viewport so that I could manually move it around and see what values it should have had in order to correctly stop at the edges of the expanded image. Here's a summary of some of the data:


I had assumed that an expanded world would move out in all directions, moving the top left corner from (0, 0) to some (-x, -y) location. It does, but the amount of expansion is not as simple as taking your original world size and multiplying it by the zoom factor. The magnification is limited by a factor more complicated than basic scale you pass along. For example, it turns out that with my 1024 x 768 screen, magnification by 1.5 stretches the world out by exactly 256 pixels, no matter the original size of the world. If I go from 1.5 to 2.0, then the world stretches by another 128 pixels. I haven't been able to figure out yet where these values are coming from. The implication would also be that going from 1.0 down to 0.5 zoom would contract the world by 512 pixels, but this doesn't seem to be the case. I'll have to collect more data on that.

Goo Goo G'Joob!
Eqqman
4
Years of Service
User Offline
Joined: 15th Jul 2015
Location:
Posted: 5th Aug 2015 09:46 Edited at: 6th Aug 2015 21:33
Okay, I finally have it worked out after taking notes and running over several screen resolutions. This is what happens at the magnifications I am currently working with, so I haven't expanded this out to the general case. Note that as the world size increases or decreases, the center of the world doesn't change. SCREEN_SIZE applies to the dimension in either the x- or y-axis, so the amount of world expansion/contraction will vary along each axis:

At 50% zoom, world contracts by SCREEN_SIZE pixels (from 100%), not WORLD_SIZE * 0.5, as expected
At 150% zoom, world expands by (SCREEN_SIZE / 3) pixels (from 100%), not WORLD_SIZE * 0.5, as expected
At 200% zoom, world expands by (SCREEN_SIZE / 2) pixels (from 100%), not WORLD_SIZE, as expected

Goo Goo G'Joob!

Login to post a reply

Server time is: 2019-11-21 05:44:20
Your offset time is: 2019-11-21 05:44:20