### Author Topic: Need help with scaling algorithms [SELF-SOLVED]  (Read 271 times)

#### Jobert14

• Full Member
• Posts: 232
• BAHHH!!!
##### Need help with scaling algorithms [SELF-SOLVED]
« on: March 06, 2013, 02:07:26 AM »
I'm currently working on a 4-player spacewar like game featuring polygon vector graphics for my upcoming birthday this year...The only problem is that I can't seem to get the image scaling right.

I know how to scale image sizes using multiplication but as the image gets smaller, the shrinking speed increases which is HORRIBLE to look at for a fast paced spacewar clone! I want the ship sprites to shrink at a steady rate which I can't do right for a really long time now.

Please post some code, not math blabber!
« Last Edit: March 10, 2013, 05:46:36 AM by Jobert14 »
The C.E.O guy of Meido-Tek Productions (please warn me if this name is already taken).

#### Mrwhy

• Hero Member
• Posts: 2890
• My Dad called me Mr Why when I was 5.
##### Re: Need help with scaling algorithms
« Reply #1 on: March 06, 2013, 02:19:52 AM »
x=kx ' this gives a size increasing at rate k  .   K<1 gives shrinking
x=x-dx ' this shrinks by dx each time
x=xo/d ' this dwindles size with distance d

After each shrink, erase the image of previous size (unless you want  smearing)

#### fluffrabbit

• Sr. Member
• Posts: 393
##### Re: Need help with scaling algorithms
« Reply #2 on: March 06, 2013, 03:03:57 AM »
Use division to undo the effects of multiplication, or something like that. You need exponential interpolation or something. Honestly, I don't know, but I know what you're talking about. You want to control an object's size, not its scale. If an object is 500 pixels (which is its fixed size), when you reduce its scale by 10%, you are always reducing its size by 50 pixels.

So, simply treat its scale (0.0 - 1.0) as its size and decrease this number by some amount per frame, so:

'main loop
DO

objscale! = objscale! - 0.01

drawobject objsize! * objscale!

LOOP

If objsize! is 10, then each frame the object's size is decreasing by 0.1. This is not exponential scaling, it is a continuous decrease of size. It loses a constant number of countable pixels.

#### Jobert14

• Full Member
• Posts: 232
• BAHHH!!!
##### Re: Need help with scaling algorithms
« Reply #3 on: March 06, 2013, 04:51:10 PM »
Quote from: fluffrabbit on March 06, 2013, 03:03:57 AM
If objsize! is 10, then each frame the object's size is decreasing by 0.1. This is not exponential scaling, it is a continuous decrease of size. It loses a constant number of countable pixels.

Is there a difference between normal and exponential scaling? I just need to be able to scale screen coords (i.e. image size) at a steady rate.
The C.E.O guy of Meido-Tek Productions (please warn me if this name is already taken).

#### DSMan195276

• Hero Member
• Posts: 1978
• Yes
##### Re: Need help with scaling algorithms
« Reply #4 on: March 06, 2013, 05:50:34 PM »
The difference is in the rate at which the scaling happens. Exponential scaling will start off slower (Probably for a very short period of time, depending on your scale), but then get faster (Becoming much faster then linear scaling). In contrast, linear scaling will move at the same rate the entire time.

So, to have some real examples, let's say we're going to make a picture go from 100x100 pixels to 200x200 pixels in 1 second.

Linear scaling: ('seconds' is the amount of time that's gone by)
Our scale is 2 (Because at the end, our picture will be twice as big as it was. or in other terms 200 / 100 = 2) -- values of scale less then 1 will make our picture size decrease
'max_seconds' is the number of seconds the scaling will go on for, so 1 second in this case
Equation: new = old * (scale  * (seconds / max_seconds))
0 seconds: 100x100 pixels
.25 seconds: 125x125 pixels
.50 seconds: 150x150 pixels
.75 seconds: 175x175 pixels
1 second: 200x200 pixels

Exponential scaling: ('seconds' is same as before)
scale is still 2
max_seconds is same as before
Equation: new = old * (scale ^ (seconds / max_seconds))
0 seconds: 100x100 pixels
.25 seconds: 119x119 pixels
.50 seconds: 141x141 pixels
.75 seconds: 168x168 pixels
1 seconds: 200x200 pixels

The difference between the two is that in the first, the change between each snapshot of time is 25 pixels. In contrast, with exponential, the change varies, changing slower at first (At .25 seconds, we're only gone 19 pixels from the start, vs 25 pixels for linear), but then increase (From .25 to .50, we change 22, from .50 to .75 we change 27, and from .75 to 1 we change 32). Notice how after .5 our changes are actually bigger then linear, this gives the appearance that it's moving faster at the end .

And a note if you know any differential Calculus (If not feel free to ignore this): The reason for the difference is that the derivative of a linear is a constant (And thus a constant change). The derivative of an polynomial (Such as x ^ (n) where n is a constant, Ex. our second case) is a line, or in other words a constant increase in our change.

-- Sorry, I realize this seemed like a lot of math blabber but it does apply. If you stick to linear scaling them it should be smooth. The 'smooth-ness' really depends a lot on what number you pick for your intervals for the 'seconds' variable though (Or Ex. your FPS). A high FPS (And thus 'seconds' variable) results in a very smooth motion, where as the example I gave (With only 4 FPS) would be very jerky.

Matt
« Last Edit: March 07, 2013, 01:31:54 PM by DSMan195276 »
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

#### SkyCharger001

• Hero Member
• Posts: 1594
##### Re: Need help with scaling algorithms
« Reply #5 on: March 07, 2013, 09:28:04 AM »
A.
Quote from: DSMan195276 on March 06, 2013, 05:50:34 PM
Equation: new = old * (scale ^ (seconds - max_seconds))

that should be max_seconds-seconds (or you'd get negative values)

B. Jobert, are you looking for flat scaling or depth scaling?
linear scaling is suitable for flat scaling but not depth scaling.

the simplest form of depth scaling is "size/distance"
Code: [Select]
`SCREEN 12FOR h = 50 TO -50 STEP -.5    CLS    FOR g = 1 TO 50        b = h + g        IF b < 1 THEN b = b - 1: b = 1 / -b        a = 180 / b        LINE (320 - a, 200 - a)-(320 + a, 200 + a), (g MOD 15) + 1, B    NEXT g    _DISPLAY    _LIMIT 10NEXT h`

#### OlDosLover

• Hero Member
• Posts: 3859
• OlDosLover
##### Re: Need help with scaling algorithms
« Reply #6 on: March 07, 2013, 01:19:30 PM »
Hi all,
Although i dont understand Maths much thats a really good example SkyCharger001 that illustrates depth over time. Thanks for sharing!
OlDosLover.

#### DSMan195276

• Hero Member
• Posts: 1978
• Yes
##### Re: Need help with scaling algorithms
« Reply #7 on: March 07, 2013, 01:31:32 PM »
SkyCharger:

Actually, it should be division, same as the first one, so: new = old * (scale ^ (seconds / max_seconds)) -- My numbers are still correct though, I think from a different error I made in doing my calculator work (Since max_seconds is 1, if you leave it out in this case you get correct numbers). Sorry about that, I really should have caught that, so thanks for realizing. I'm going to go back and modify my original post to make more sense.

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

#### Jobert14

• Full Member
• Posts: 232
• BAHHH!!!
##### Re: Need help with scaling algorithms
« Reply #8 on: March 07, 2013, 05:10:24 PM »
Well, this might be a big bummer but I managed to fix the stupid slow-to-fast scale down problem by myself a few days ago. I didn't change the scaling algos in my code but I changed the way how the scale value is calculated.

Camera.Scale = 100 * (ViewWidth / 30)

I used this:
Camera.Scale = 100 * (319 / ViewWidth)

So, if ViewWidth is equal to 319 (when the ships in the game are within the screen), the scale is 100%. When the ships fly farther away from each other, the ViewWidth increases to a size that all the ships fit within the screen and the scale value from 100 decreases to a desired value.

At least I've learned some things from this topic...
The C.E.O guy of Meido-Tek Productions (please warn me if this name is already taken).

#### codeguy

• Hero Member
• Posts: 3552
• what the h3ll did i name that code?
##### Re: Need help with scaling algorithms
« Reply #9 on: March 08, 2013, 08:15:39 AM »
if in 3d, i scale as linear ratio of current depth to maximum depth. collider worms demo and NSpace 3d demonstrate this principle. beware, however, in reorganization of qb64 site, i am no longer able to find the former demo but ya might get lucky if someone can post it for me as this is the simpler of the 2 demos.
http://denteddisk.forums-free.com/make-an-appointment-with-the-resident-code-guru-f34.html

#### Mrwhy

• Hero Member
• Posts: 2890
• My Dad called me Mr Why when I was 5.
##### Re: Need help with scaling algorithms
« Reply #10 on: March 08, 2013, 09:38:34 AM »
Linear scale is fine but for close-up apparent size = atan(x/d) is better.
And closer than that depends on the lens aberrations of your eyes.

Your comment on "relative size", codeguy, is very much to the point.
To gently land a plane or space ship there is NO NEED to know the size nor even the nature of the landing place.
Over gravel works splendidly - for the closer we get the more rapid (percent-wise) is the growth of each stone we see. No need even to bother with atan.