A Calculation Question

FallacyStudiosFallacyStudios Member Posts: 970
edited April 2012 in Working with GS (Mac)
I was wondering if anyone knows a formula so that when something interpolates down I have another object that is Constrained attribute to grow as it comes down.

So for my boss he is flying up above the screen. I know his X and Y. I have a shadow that is down on the floor that is Constrained to the Boss's X. I want the Y to be Constrained so that as the boss interpolates down that the shadow will grow to a max height of 75 from a starting 3.

Thanks.

Best Answer

  • SinequanonSinequanon Posts: 35
    edited April 2012 Accepted Answer
    Let's say the boss's max height is 400 (I just chose a number) and his minimum height is 25 (I just chose a number, again).

    You have a situation where the shadow would grow linearly (to make things simple), so the size of the shadow is a function of the height of the boss, where when:

    Height = 400, Shadow Width = 3, and
    Height = 25, Shadow Width = 75.

    To find the (linear) equation between two points, you need to use this really simple formula:

    Equation of a line = y = mx + b, where m is the slope between the two points, and

    m = (y1-y2)/(x1-x2)

    Without getting too far into the math here (I'll give you a good link that explains it all), the final equation would be:

    y=-24/125x+399/5

    Which in GameSalad speak would mean using a Constrain Attribute:

    Self.Size.Width -> ((-24/125)*game.Boss Height) + 80, which would give you sample values varying on the boss's height from:

    game.Boss Height = 25, Self.Size.Width = 75.2
    game.Boss Height = 100, Self.Size.Width = 60.8
    game.Boss Height = 250, Self.Size.Width = 32
    game.Boss Height = 400, Self.Size.Width = 3

    If you really wanted to make sure that the height stayed between 3 and 75, you could also wrap the equation above in a max/min statement:

    max(3,min(((-24/125)*game.Boss Height) + 80),75))

    My one concern is that GS is sometimes wonky with reporting when using interpolate, so put a display text on the boss to make sure it's properly reporting the self.position.Y to the game engine, but otherwise this should suit you well.

    Here's the site I used: http://www.webmath.com/equline1.html

Answers

Sign In or Register to comment.