How do I fix this formula so it won't turn out as NaN?

0 favourites
  • So I've been working on this weapon upgrade system for my game, and it has worked perfectly until it came across my minigun weapon.

    The special thing with the minigun is that it can overheat, and when overheating (but not fully overheated) it will do more damage.

    The formula for bullet damage for other weapons with the upgrading is as follows:

    BulletDamage * (WeaponLevel - 100) ^(1.5 * ((WeaponLevel - 100) / 10) /2)

    The "weaponlevel" is always at atleast 100 when usable, and can increase up to 110.

    For the overheated minigun bullets, the previous formula was "Overheat / 6". The overheat would never go above 100, and never shoot overheated bullets when below 40.

    The new formula for the overheated bullets is:

    (Overheat / 6) * (WeaponLevel - 100) ^(1.5 * ((WeaponLevel - 100) / 10) /2)

    However, in a few bullets, the damage comes out as "NaN" and when it hits an enemy, the HP of the enemy also becomes "NaN" and it becomes immortal.

    Why is this, and how do I fix it?

  • One of your variables is probably a string and not a number, which is what NaN stands for.

  • One of your variables is probably a string and not a number, which is what NaN stands for.

    I checked the variables used in the formula, and all of them seem to be numbers, not strings.

    Any other ideas?

  • Add "Browser Log" action with the following string:

    "Overheat:" & Overheat & " WeaponLevel:" & WeaponLevel & " RESULT:" & ((Overheat / 6) * (WeaponLevel - 100) ^(1.5 * ((WeaponLevel - 100) / 10) /2))

    Open browser console in preview (F12), try to find a bullet with NaN damage, you'll be able to see the values of Overheat and WeaponLevel variables, maybe there is something wrong with them.

    If this doesn't help, please post a small project demonstrating the issue.

    .

    Edit: (-10)^1.5 returns NaN, so the most likely reason is that WeaponLevel is less than 100 for some bullets. A simple fix would be adding max(WeaponLevel,100)

  • You could

    clamp((Overheat / 6) * (WeaponLevel - 100),0,YourMaxValue)

    To make sure the first part of your formula will always be positive, since a negative number there will trigger the NaN response.

  • Dividing by zero also returns a NaN, i think.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • clamp((Overheat / 6) * (WeaponLevel - 100),0,YourMaxValue)

    I've never really understood how clamps work. I tried this out, and at some points the damage would come out as 0, which I'm guessing would normally be the NaN? However, this formula you provided is also different than the one I normally use.

    I tried putting it on my own formula, which turned into:

    clamp((Overheat / 6) * (WeaponLevel - 100) ^(1.5 * ((WeaponLevel - 100) / 10) /2) ,1, 100)

    When I used that, the values still came out as NaN every now and then.

  • Edit: (-10)^1.5 returns NaN, so the most likely reason is that WeaponLevel is less than 100 for some bullets. A simple fix would be adding max(WeaponLevel,100)

    I checked for this aswell. That is not the case. As long as the weapon is unlocked, it is at atleast 100. For the testing I'm doing, the WeaponLevel value is at 106.

    I will try out the browser log part aswell.

    However, I do believe it has something to do with the Overheat value, due to it constantly changing, and due to the NaN value coming out at random times

  • x^1.5 = x^(3/2) = sqrt(x^3)

    A negative number raised to the third power will result in a negative number, and the square root of a negative number will result in NaN.

  • x^1.5 = x^(3/2) = sqrt(x^3)

    A negative number raised to the third power will result in a negative number, and the square root of a negative number will result in NaN.

    So what do you reccomend I do?

    The weird thing is that the damage NaN thing only happens for this weapon, while for the others there haven't been any problems

  • You can use clamp() or max() as suggested earlier in the thread to make sure x can never be a negative number, or you can change your formula to not use a fractional exponent.

  • You can use clamp() or max() as suggested earlier in the thread to make sure x can never be a negative number, or you can change your formula to not use a fractional exponent.

    When using clamp the bullets still come out as NaN every now and then as I mentioned earlier.

    The clamp formula that was suggested earlier was different than mine, and I want to use this formula (or something that ends up with the same results).

    When I tried with clamp I did the formula:

    clamp((Overheat / 6) * (WeaponLevel - 100) ^(1.5 * ((WeaponLevel - 100) / 10) /2) ,1, 100)

    As I mentioned, still came out as NaN every now and then.

    Am I doing this wrong?

  • Try this:

    (Overheat / 6) * (max(WeaponLevel,100) - 100) ^(1.5 * ((max(WeaponLevel,100) - 100) / 10) /2) ,1, 100)

    .

    If it fixes the problem, it will mean that sometimes WeaponLevel can be <100

  • Try this:

    (Overheat / 6) * (max(WeaponLevel,100) - 100) ^(1.5 * ((max(WeaponLevel,100) - 100) / 10) /2) ,1, 100)

    .

    If it fixes the problem, it will mean that sometimes WeaponLevel can be <100

    The formula doesn't work. It has too many paranthesis or something, idk.

    And I know for a fact that the WeaponLevel is not less than 100. Otherwise I would not be able to use the weapon.

  • Sorry, try this one:

    (Overheat / 6) * (max(WeaponLevel,100) - 100) ^(1.5 * ((max(WeaponLevel,100) - 100) / 10) /2)

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