I am working on programming a game, and I want the damage that a player takes to be based off of the distance they are from the explosive. Consider it like a grenade. Right on the blast radius (distance of 0) I want it to be the maximum damage. At the maximum distance away where damage is still sustained, I want it to be 0 (or 1, technically, but for simplicity I guess we can say 0.

let d = distance away the explosive is from the player when it lands.

let m = max distance away for a minimum damage

let x = maximum damage possible (when d = 0)

let r = result damage done

Now I think cosine is the perfect function to use here, because cos(d = 0) would be 1. Then I can multiply that by x to get the max damage. I am stuck on how to do the minimum damage though. I need to somehow use cos(pi/2) to get 0 so that when I multiply by x I get 0 damage. But what about all the cases in between?

What would be the formula I'm looking for?

r = .....?


Ok I got what I want.

let result be the resulting damage from the shot

let m be the max distance where damage is done

let d be the distance the shot was away from the targeted player

let x be the maximum damage possible (when d = 0)

 result = sin[((m - d)/m) * (PI/2)] * x;

Works perfect!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.