MC2 – Technical – Sometimes I make silly mistakes…

I just started implementing stat tracking in MC2. I don’t mean user data tracking, or anything nefarious like that, I simply mean in game stats, like how many kills a player has scored, how many jumps, and so on. One of the stats is damage taken and damage caused. I named these floats damageAgainst and damageFor, respectively.

As I was testing the stat tracking I noticed something that made no sense. When two spheres collided, the damageFor was always substantially higher than the damageAgainst, say 7f vs 5f. This meant I had been calculating damage incorrectly all along! TRAGEDY!!!

First, let’s determine the design (this may change in future, but for now here it is): I want the faster sphere to damage the slower one. That’s it. The way I’ll find out who is faster is by subtracting my speed from my opponent’s. If the result is higher than 0f, then I will have lost that collision and I must therefore take damage. I will also include a small variation where I account for both of our original mass. The heavier sphere will get a small advantage. So, in words:

opponentSpeed (plus)
opponentMassModifier (all minus)
mySpeed (plus)
myMassModifier

Take a look at this snippet…

This is the first line in a function called 
CalculateDamage. This function is used to 
determine who won the 50-50 in a collision 
and how much damage was caused to whom.

damageRawValue = (
  opponentVelocityMagnitude +
  opponentVelocityMagnitude *
    ((opponentOriginalMass / playerAttributes.maxHP) *
    GameManager.singleton.massDifferetialDamageInfluence) -
  myVelocityMagnitude +
  myVelocityMagnitude *
    ((playerAttributes.maxHP / opponentOriginalMass) *
    GameManager.singleton.massDifferetialDamageInfluence)
);

OK, so let’s do some arithmetic…

First, values! Let’s give our variables the following values:

– opponentVelocityMagnitude = 6f;
– myVelocityMagnitude = 3f;
– opponentOriginalMass = 10f;
– playerAttributes.maxHP = 12f;
– GameManager.singleton.massDifferetialDamageInfluence = 0.1f;

Now let’s replace them as necessary:

myDamageRawValue =
6 + 6 * (10 / 12) * 0.1 – 3 + 3 * (12 / 10) * 0.1 =
6 + 0.5 – 3 + 0.36 =
6.86 – 3 =
3.86;

opponentDamageRawValue =
3 + 3 * (12 / 10) * 0.1 – 6 + 6 * (10 / 12) * 0.1 =
3 + 0.36 – 6 + 0.5 =
3.86 – 6 =
-2.14;

Aaaagh! Why are their absolute values different! Here’s why, silly (talking to myself!)

Here’s what I think:

myDamageRawValue =
6 + 6 * (10 / 12) * 0.13 + 3 * (12 / 10) * 0.1;
opponentDamageRawValue =
3 + 3 * (12 / 10) * 0.16 + 6 * (10 / 12) * 0.1;

Here’s what the computer thinks:

myDamageRawValue =
6 + 6 * (10 / 12) * 0.13 + 3 * (12 / 10) * 0.1;
opponentDamageRawValue =
3 + 3 * (12 / 10) * 0.16 + 6 * (10 / 12) * 0.1;

Nothing that a few well placed brackets couldn’t solve.

myDamageRawValue =
(6 + 6 * (10 / 12) * 0.1)(3 + 3 * (12 / 10) * 0.1) =
(6 + 0.5)(3 + 0.36) =
6.53.36 =
3.14;

opponentDamageRawValue =
(3 + 3 * (12 / 10) * 0.1)(6 + 6 * (10 / 12) * 0.1) =
(3 + 0.36)(6 + 0.5) =
3.366.5 =
-3.14;

That was a little stupid, wasn’t it, Mike? I hear you all think. And you would be entirely accurate. Sometimes, I am THIS silly…

– Mike

Leave a Reply

Your email address will not be published. Required fields are marked *