Math formula needed (relatively simple)

Welcome to our brand new Clickteam Community Hub! We hope you will enjoy using the new features, which we will be further expanding in the coming months.

A few features including Passport are unavailable initially whilst we monitor stability of the new platform, we hope to bring these online very soon. Small issues will crop up following the import from our old system, including some message formatting, translation accuracy and other things.

Thank you for your patience whilst we've worked on this and we look forward to more exciting community developments soon!

Clickteam.
  • Hi peeps, I'm using a formula to change the semi-transparency of a warning icon which appears over your unit as you play, and as your health deteriorates I want it to become more visible (i.e. less transparent).

    As you can upgrade your max-health from a wide range 100, 150, 250, 500, 800, 1500 & 2500 I'm using the current formula:

    always set alpha-blending of the icon to:
    ((value(health))*300/maxvalue(health))+value(health)

    This does a moderately good job, but I'm looking for an improved formula as there are some flaws:

    1 - At low max health settings (i.e. early game), the warning icon comes in a touch too early

    2 - At high max health settings (i.e. later game), the icon fades in nicely as you lose health, but doesn't really get very visible until health is literally depleted.


    Does anyone have any ideas for a better formula? The alpha-levels are 0-255 (0 being fully visible), which is why I use 300 in my formula (I started with 255, but found that increasing the multiplication a bit helped to change when the fading in started.

    Many thanks :)

    IndieDB Page: Please login to see this link.
    Development Blog: Please login to see this link.

  • You can limit the range of health that is supposed to make a difference.
    i.e. min(100, max(10, health)) will always be in a range of [10, 100]. Maybe this would help in getting the desired effect.

    As a more simple solution you could just have a constant negative value added to the formula, e.g.
    ((value(health))*300/maxvalue(health))+value(health) - 50

    This way, the formula on the left needs to be at least 50 in order for the alpha coefficient to be >= 0. This is basically the opposite of the *300 approach that you used, which obviously stops doing anything after 255.

    Please login to see this link.

  • Thanks Looki. Subtracting makes it fade in much earlier, but even adding a fixed value like 50 just stops it from being linear as you upgrade your hull. I.e. when you're at 2500 max health, changing by 50 has no meaningful effect given that I'm multiplying and diving much larger numbers.

    I'm just playing with your min/max idea to see what crops up.

    Mathemagicians - show yourself!! :)

    IndieDB Page: Please login to see this link.
    Development Blog: Please login to see this link.

  • I've made a quick graph of what I am aiming for in the hope that it helps:

    Please login to see this attachment.

    It just demonstrates for having a max health of 100, but ideally the graph shape should remain whatever the max health is, i.e. by working from percentages of your health&maxhealth.

    IndieDB Page: Please login to see this link.
    Development Blog: Please login to see this link.

  • Asholay - Have you tried using the Easing object? It has lots of different options for the kind of curve you're wanting and is not just for moving objects around, you can use it to interpolate between the start and end values of your curve and apply quadratic, cubic, exponential, etc

    Kevin Ayre Please login to see this link.
    My iOS Apps: Please login to see this link., Please login to see this link. and Please login to see this link.

  • Thanks for the idea Kev but I'm writing for the XNA exporter so cannot use that extension - I'll have a look though as it sounds like a good extension for my other bits.

    IndieDB Page: Please login to see this link.
    Development Blog: Please login to see this link.

  • Oh, sorry for misunderstanding. The graph you want looks somewhat like an exponential function.

    This is the graph of e^(ln(255)*x):
    Please login to see this picture.

    ln is the inverse function of e^x - that means: e^(ln 255) = 255.
    So when x reaches 1, the value of e^((ln 255)*x) will be 255.

    Using your health, it would look something like this:
    e^(ln(255)*health/maxHealth)

    the function will now be 255 if the health is 100% full.

    e^(ln(255)*2*health/maxHealth)

    Now the function surpasses 255 at 50% health.

    Something you should know: e^0 = 1. Therefore, instead of using ln(255) you should use ln(256) and subtract one from the result so that a value of 0 can be reached:

    e^(ln(256)*health*2/maxHealth) - 1

    The MMF syntax for this function is:

    Exp(Ln(256)*Health*2.0/MaxHealth) - 1

    Here's a quick plot of this function (scale 1:1) that I made with the Surface object (max health = 300, so the graph has a width of 300):
    Please login to see this picture.

    The red line is the 255 limit. I think it looks very similar to your graph...

    Please login to see this link.

    Edited 2 times, last by Looki (February 18, 2013 at 6:46 AM).

  • You Sir, are a genius! That's exactly the sort of answer I was hoping someone would provide - credit worthy stuff.

    Works like an absolute charm and kicks in a touch earlier than my current formula so gives a touch more warning when your health is down (although just in case anyone is learning/using the formula - you slipped an extra * in the MMF syntax by accident Looki)

    IndieDB Page: Please login to see this link.
    Development Blog: Please login to see this link.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!