Bounded scaling

0 favourites
  • 3 posts
From the Asset Store
Fly Bound
$2.49 USD
Design for game, asset for game, game kit, UI design, completed design for game FLY BOUND
  • I know we have some number wizards on here

    I have a scrollto behaviour attached to a camera sprite and I want to move the camera sprite around the layer and have the the layout zoom in on the camera sprite accordingly based on its distance from the layer center. It kind of acts as a bounded scale that never displays the outside of a layer.

    I can do:

    set layout scale -> <layer middle x> / <camera sprite x>  [/code:2xxb67lw] which works great, but only for values less than the center point on the x axis. It wont work for the opposite direction or y directions. 
    Ive tried using distance and using a scale thats an average of x and y which kind of works but isnt accurate.
  • This seems to work well:

    Set scale to 0.5*OriginalWindowWidth/min(min(camera.X, layoutwidth-camera.X), min(camera.y, layoutheight-camera.y)*OriginalWindowWidth/OriginalWindowHeight)

    min(camera.X, layoutwidth-camera.X)

    and

    min(camera.y, layoutheight-camera.y)

    are the closest x and y distances to the edge of the layout

    using the aspect ratio I can then convert the y distance to a x distance:

    y*OriginalWindowWidth/OriginalWindowHeight

    So now there are two x distances, and we can get the smaller one with another min.

    min(x, y*aspectratio)

    and to convert the x distance to a scale we multiply by 2 and divide by the window width:

    2*xdist/originalWindowWidth

    As an additional idea you can also limit the zoom if it gets much too close near edges.

    min(scale, 4)

    or whatever max scale you want.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks R0J0hound that works like a charm! Thank you for the additional explanation!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)