Zooming Woes

Started by geork, Tue 05/11/2013 10:06:39

Previous topic - Next topic

geork

Hello Everyone!

I've been trying to come up with a way to zoom in and out of the background slowly (no problem) with any center point: so the user defines a point and the camera slowly zooms in/out to that point, as long as the image does not move out of the bounds of the screen, in which case the center point is adjusted. (again, no problems there) However, what I am struggling with is positioning of characters/objects in relation to the new 'center point'. I'll go through the theory first to see if that bit is wrong, and if not I'll post up what I've done so far.

The basic assumption I made was that, say, if the zoom level was 2x, the character/object would end up being twice as far away from the 'center point'. I'm using zoom levels 100% (normal) going upwards (so 300% is 3x zoomed in). Therefore, the end XY position of the character/object to the center point would be the initial X distance and Y distance (these distances are calculated on the assumption of a zoom level of 100%) multiplied by the zoom level (so 100% is 1, 150% is 1.5, 167.45% is 1.6745 etc.).

I also figured that as the 'center point' does NOT start at the center (usually), then it's trajectory to the center of the screen would be the point at which the XY of the character/object is calculated from.

So the process on each loop is:
1) Get the zoom level for this loop.
2) Create the new background frame from dynamic sprite (this works fine it seems)
3) Get the position of the 'center point' on it's trajectory to the middle of the screen.
4) Calculate the XY position of the character/object by multiplying the X distance and Y distance by the current zoom level and adding that to the XY value of the 'center point's new position.

It may be an implementation problem, it may even be that the background sprites aren't calculated correctly, but I just wanted to check if the theory was correct first before going further and posting a wall of code.

Thankyou very much for your time!

Snarky

#1
It sounds correct to me, except for this bit, which I don't quite follow:

Quote from: geork on Tue 05/11/2013 10:06:39
I also figured that as the 'center point' does NOT start at the center (usually), then it's trajectory to the center of the screen would be the point at which the XY of the character/object is calculated from.

What I believe you need to do is simply to calculate the "normal" (100%) x-difference and y-difference between your zoom point and the character/object coordinates, multiply that by the zoom level, and add the scaled values to the screen coordinates of your zoom point in the zoomed-in view (which presumably will be the center of the screen).

However, I think it's generally cleaner to simply scale all values relative to the regular origin (0,0 in 100% scaling), and apply a uniform offset to all the coordinates in order to put your zoom point at the center. That way you can treat all coordinates the same way, and don't confuse yourself with lots of moving points in different scales.

So...

Code: AGS

  // To calculate the offsets to center your zoom point (ignoring screen borders): 
  float x_offset = orig_zoom_x * scale_factor - System.ViewportWidth/2;  // I'm omitting all the FloatToInt/IntToFloat stuff. Just do everything in floats...
  float y_offset = orig_zoom_y * scale_factor - System.ViewportHeight/2;

  // And to calculate any coordinate:
  int scaled_x = orig_x * scale_factor - x_offset; // ... and only round it to the nearest int at the very end to give you the final coordinate
  int scaled_y = orig_y * scale_factor - y_offset;

SMF spam blocked by CleanTalk