Maths to calculate enemy damage based on distance from player

BarrytheBarrythe Member Posts: 82
edited November -1 in Working with GS (Mac)
This is making my head hurt - I have a stationary player on the left of the screen, and enemies spawning on the right. The enemies walk towards the player, and the player can fire at the enemies. As a test I gave the enemies 100 health and each bullet removes a fixed amount of health per hit, until health reaches zero and the enemy dies.

Now I am trying to calculate the damage so that my weapon acts like a shotgun - the closer the enemy is the more damage dealt. I have set a rule on the enemy so that when the bullet hits, damage is calculated from a magnitude between the player and the enemy. The trouble is that this setup means that the larger the distance between the player and the enemy the larger the damage, whereas I need the opposite, so that the shorter the distance the greater the damage. My current rule attached to the enemy says that when the enemy collides with a bullet: (by the way "game.PlayerPos "is a global attribute constrained to the players x position).

self.Health = self.Health - self.Health * magnitude(self.position.X - game.PlayerPos , self.Position.Y) / 1000

This generally gives me a nice small number between 20 and 40ish, but is there a way I can achieve the inverse so that the number gets larger as the distance gets shorter? I'm no maths genius so be gentle with me if my equation is a load of rubbish :-)


  • BarrytheBarrythe Member Posts: 82
    Okay if anyone is watching this I've solved the problem with a bit of logical thinking. Instead of taking the magnitude from the player to the enemy, I am now taking the magnitude from the enemy to 480 (the far edge of the screen), so that as the enemy gets closer the resultant number gets larger instead of smaller. This game making is forcing me to use parts of my brain I forgot I had.
  • quantumsheepquantumsheep Member Posts: 8,188
    Nice one.

    My games force me to use parts of my brain I *don't* have.

    Which is why having a logical thinking coder close to hand saved me a couple of times! :D

    Dr. Sam Beckett never returned home...

Sign In or Register to comment.