• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

correct distance formula

Status
Not open for further replies.
Level 4
Joined
Jul 19, 2008
Messages
37
hi. :)
well i created a spell that heals all nearby allies in an area. the closer they are, the greater the heal ( from 50% heal if unit is in max range ( in this case, 300 ), to 150% heal if the unit is dead center ). i havent found the correct formula to do this and it really bugs me that i can figure this out.
My current formula is :
JASS:
set d = SquareRoot( Pow( GetUnitX( u ) - s.tx, 2 ) + Pow( GetUnitY( u )- s.ty, 2 ) )
set h = 0.5 - ( d / 300 ) // the factor of the heal
this formula i made seems to be working backwards as if the unit is farther the effect is stronger, im aiming for the opposite...

any ideas?
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
Well your distance formula appears to be correct.

The problem lies in your variable h. Let me try to give you some math to show this.

The first problem is that you're basing the calculation of h off of 0.5 - if you were to do this then you would have to add to 0.5 in any situation. What you're describing is that you want the factor to be 1.5 when d is 0.

So let's start with 1.5:

Code:
h = 1.5

Now we need to subtract from this value. Whether we subtract any value in between 0 and 1 is dependent on the ratio of the distance to the "max" distance. This ratio is represented as d / MAX_DISTANCE or d / 300.

So we know this:

Code:
h = 1.5 - X( d / 300 )

Now the value "X" should represent the range that you want to give to the effect. If it is 1.0 then your effect factor is going to range by 100% which is what we want in this case, so:

Code:
h = 1.5 - (d/300)

Let's examine this equation. When d is at its maximum, which is 300, the equation yields:

Code:
h = 1.5 - (300/300) = 1.5 - 1.0 = 0.5

Similarly, when d is at its minimum, which is 0, the equation yields:

Code:
h = 1.5 - (0/300) = 1.5 - 0 = 1.5
 
Status
Not open for further replies.
Top