Zooming Maths

Started by geork, Sun 26/05/2013 21:03:39

Previous topic - Next topic

geork

Hello everyone!

I'm very sorry if this is a simple question to answer - my brain just seems a bit fuzzed today (I'm sure that's a verb)

I'm writing a zooming out function (from a zoomed in image of 50%) where the character is scaled and placed separately from the image. More precisely, I have 64 Dynamic Sprites where each contains a more "zoomed in" version of the background than the last, and a character who is animating separately with the proper scaling. I'm trying to have the character appear as if he is staying in the same place, but scale correctly. I tried, therefore, implementing this code:
Code: AGS

//somewhere above:
DynamicSprite Background[64];
//these then get assigned to the backgrounds at progressive level of "zoom" (63 = max zoom)
int i = 63;
float xFactor = IntToFloat(cChar.x-EndCharXPlace)/64.0; //where EndCharXPlace is where the character's final x coordinate should be
float yFactor = IntToFloat(cChar.y-EndCharYPlace)/64.0; //and so on...
float sFactor = IntToFloat(cChar.Scaling-EndCharScaling)/64.0; 
float j;
while(i >= 0){
  object[0].Graphic = Background[i].Graphic; //there are reasons why it's an object...I think...
  j = IntToFloat(i);
  cChar.Scaling = EndCharScaling+FloatToInt(j*sFactor, eRoundNearest);
  cChar.x = EndCharXPlace+FloatToInt(j*xFactor, eRoundNearest);
  cChar.y = EndCharYPlace+FloatToInt(j*yFactor, eRoundNearest);
  Wait(3);
  i --;
}

Whilst the character (cChar) starts and stops at the right X/Y coordinates and scaling, the character appears to move down and left on the screen (relative to where he should be) for the first 32 frames, before slowly returning back to end up at the correct position for the last 32. How can I correct the formula so the character moves the correct amount of X and Y to seemingly stay in the same position?

Apologies again if this is a waste of time - I feel like I should know the answer :\

Thanks again!

Khris

#1
As far as I can see the character's movement is perfectly linear.
Which means the sprites are wrong, plain and simple. I guess you aren't zooming out in a linear fashion, which is why the character shifts position, then moves back into place. Depending on the character's initial position, you are either zooming out too fast or to slow, then make up for the error with the last 32 sprites.

It's really difficult to tell without looking at the sprites though.

Edit: Note that zooming an image from 50% to 55% is NOT the same as zooming from 60% to 65% (since 55/50 > 65/60)

geork

Hello Khris!

Ahh, I understand. I've updated the code on assigning the sprites to reflect a linear scaling path, and it works fine! Now the only issue (apart from a slight character offset, which is easily fixed) is that the rate of zoom increases the further out it gets... But I guess this can be worked around by either adjusting the timings of the rate at which new frames are displayed (which seems a bit rough) or to keep to the original zoom I had and update the character movement math with this linear formula in mind.

If anyone is interested, the code to assign progressively more zoomed in sprites used to be: (for CENTER-RIGHT corner at 50% zoom)
Code: AGS

int i;
while(i<64){
  Background[i] = DynamicSprite.CreateFromBackground(0, (i*8), (i*3), (i*8), (i*6));
  Background[i].Resize(1024, 768);
  i ++;
}

and is now:
Code: AGS
int i;
float c = 1.0/64.0;
float d;
while(i<64){
  if(i!= 0) d = c + (IntToFloat(i)*c);
  else d = c;
  d += 1.0;
  Background[i] = DynamicSprite.CreateFromBackground(0, (1024-FloatToInt(1024.0/d)), (384-FloatToInt(384.0/d)), FloatToInt(1024.0/d), FloatToInt(768.0/d));
  Background[i].Resize(1024, 768);
  i ++;
}


Thanks again!

SMF spam blocked by CleanTalk