Math Code Question - Scaling effect based on range
Hey Math-Braniacs and Code-Slingers -
I'm working on a new game concept and I was hoping I could get some insight as to perform a function based on a feature in the game I'm working on. The concept itself is relatively simple in description, yet implementation is where I'm bonking my brains against a wall.
I'm trying to generate the effect of a blast radius in which the amount of damage done scales down based on how far a unit is from the point of impact. (i.e. throwing a grenade into a room - the units right under the grenade when it goes off will take full damage yet units farther away from the detonation point would receive less damage, based on how far away they are).
At this time, I'm looking at creating and saving to REAL global Attribute variables of the Grenade X & Y co-ords so that I can calculate the Magnitude (distance) each unit is from the detonation point (Grenade X,Y). When the Grenade detonates, I set a fixed blast radius of 100 pixels (circular collision box) and the Grenade will inflict 100 Hit Points of damage to units at ground zero. Units within the blast radius will calculate their range to the grenade detonation center point via magnitude.
Where I run into trouble is the math in which I scale down the damage to units as they are within the 100' radius yet not at ground zero.
Thanks in advance..
-- J
Comments
100 - magnitude(grenade x - self x, grenade y - self y)
The answer above will give you a precise division of 100 ( going on your idea of using a REAL attribute to store distance), the version below quantizes point scoring to 10s, so at the very centre you score 100, as you move outwards from the grenade impact point the score drops by 10s, so at 100 pixels away the score is 0.
100 - round(magnitude(grenade x - self x, grenade y - self y)/10)*10
Much appreciated, @Socks !
Quick follow-up question - if I wanted to call variables, say for different types of grenades, would this be a fair mod to your code suggestion:
GRN - round(magnitude(grenade x - self x, grenade y - self y)/DMG)*10
GRN = Grenade Damage (so I can call different types)
DMG = Damage Increments (for differing types as well)
-- J