How do I set the layout scale based on the distance between two objects?

0 favourites
  • 7 posts
From the Asset Store
This is a Dungeon Master tool & the 1st of 12 Combat Engines from the Building Combat Engines for Browser Games workshop
  • Hey there, everyone.

    I'm struggling with the concept of moving a camera around. I have this piece of code so far...

    The concept for the non-disabled code is that I have a sprite with the "scroll-to" behavior on it that constantly sets itself between the Player and the Goal. That code is working fine, but when the two get too far apart, the objects both disappear off of the screen.

    The disabled code represents my attempts so far to figure out how to properly scale the layout so that both objects are always visible on screen via scaling of the layout. I do realize that I basically am looking for a number between roughly 0.4 and 1 (depending on the distance between the objects) and that I also need to stop the camera from zooming in when I reach a scale of 1. But in order to do that, I first need to find the formula that would let my camera zoom in and out. The problem with my current setup is that it actually zooms out when the two objects approach, rather than zooming in, and I seem to be hitting some kind of brain fart that isn't letting me proceed. Maybe I've looked at this code for too long, haha.

  • Is this what you are trying to make?

    https://howtoconstructdemos.com/auto-zoom-and-scroll-the-screen-to-fit-multiple-characters/

    This is literally exactly correct. I sort of understanding how this is working, but I was wondering if I could ask some clarifying questions. I don't fully understand it, and while I could just copy it and walk away, I'd like to understand it more fully so that I can do it myself in the future.

    It says for "min" and "max" expressions that it is calculating the minimum and maximum of things, so I assume that if I put in, say, max(3,5,Player.x), then, unless my player x was bigger than 5, it would always return 5, right?

    So what those four expressions in the "for each" do is, for every player that is on the screen, calculates the farthest point of the relevant bounding box for that object, then compares it to the previously calculated value from the previous cycle (which is why max is set to 99999, where no object would reasonably be). I see it's using a "min(max" or "max(min" at the very start of each expression, but I'm not quite sure why that part is done...

    After all that is done, it finds the center of the box by adding the values together and dividing it by 2, and lerps via delta time x 16 (this number could be adjusted however I want, I'd assume).

    It calculates a value to scale the layout to by dividing the difference of the min and max values by the original size of the viewport, then clamps it so it can't go beneath 1.

    I'm a little shaky on the math for the viewport calculation (because I am bad at math) but I think I understand the concept.

  • You've figured out everything correctly!

    I see it's using a "min(max" or "max(min" at the very start of each expression, but I'm not quite sure why that part is done...

    The second min and max is used to limit the values to layout size. It's not really needed in the demo since there is a solid war around the layout.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I see. Since mine doesn't have solid walls I suppose it's good that I have it!

    One last question. I am not familiar with local variables. Why not just use global variables? Unless the variables aren't needed outside of that particular sub event. Do local variables only exist within that sub event and basically cease to exist outside of that particular event?

  • Yes, exactly. Local variables exist only within the event or group and hold their value for one tick (unless you set them as Static). The biggest advantage is that they don't "contaminate" your list of global variables.

  • Thank you so much for this information! I feel like I have gained a lot of useful tools for my toolbox!

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