2D-zooming headache... (math)
in
Programming Questions
•
2 years ago
Hello again, my dear friends!
Today there is a question about 2D zooming. First I explain my settings:
1) I have a 2d space (starmap), where objects(stars) are located arbitrary.
2) The screen only captures (screen.size.x, screen.size.y) area of the space thus allowing us to see just a little part of the map. Same as in all the RTS games.
3) The screens center position on map is called "offset".
4) The screens dimensions are called "size"
All in all, at the beginning, when the offset=0, we see the [-size/2 to +size/2] portion of the map. If the offset moves, we see [offset - size/2 to offset + size/2] region of the map.
Now everything was fine until i started implementing zoom.
5) The zoom is uniform (not differentiated by axis) and called "zoom"
6) The stars position on the map is called "position".
Using pen and paper I have came up with formula for projected star position (the x and y equations are not explicitly described here since they are same as the ones below except for the axis index like
size.x instead of
size):
- projectedPosition = size/2 + (star.position - offset)/zoom;
and the
criterion formula to determine if the star needs to be drawn(is onscreen, giving its present zoom):
- projectedPosition > size * (zoom - 1) / (2 * zoom) &&
- projectedPosition < size * (zoom + 1) / (2 * zoom)
and it all works! At least at zoom factor=1 when it is like there is no zoom. When i start zooming, it works too, but it seems to me that the
criterion has a flaw because stars start disappearing on the left and up edges of the screen before they get off-screen.
I understand this to be a simple math problem, consisting of several overlayed coordinate projections... however, I got stuck here a little. I would like someone to suggest corrections to these equations or some different equations that would do. I also want to be able to un-project mouse screen coords back to the map coords. This now works for zoom factor=1 but due to the mysterious errors in the equations, the mouse projection gets incorrectly altered on different zoom factors... please help!
1