The million dollar question: examples of math functions used in complex games (RTS, simulations)

We all know that balancing a game, is the equivalent of the gold pot at the end of the rainbow…few are able to do it, and refined their process to the state of the art.

For us common people; things works differently, so I am looking for some sort of “cheatsheet”, that give some ideas about various functions to use with the values of a game.

In particular, I am aiming at common genre, which are notoriously more math-intensive, on the side of the calculations for in-game parameters. Such as simulations (for example fifa-style games, or of the likes of football manager) and RTS.

For RPG types, I found success in implementing most of the AD&D or GURPS mechanics; which gave me generic ways to handle how parameters interact. Other genre are also more manageable, once you cover the basics, but I find hard to figure out exactly what would be a standard set of functions and math used for simulations and RTS.

For example; some designers use functions that plot a specific progression; like in a RPG you want to use a sort of function that plot as S shape, so the progression through levels is linear in the middle, steep and quick in the beginning and slow down at the end, close to level cap.

Some useful functions to give an idea:

  • Logistic
  • Linear
  • Exponential
  • Quadratic
  • Logarithmic

These are useful, but the implementation is not always simple (for example in a RTS, you could say that there is a value called morale, not displayed to the player, that get influenced by the number of units in game, their overall health and the ratio of units destroyed vs the units created).
Implementing a function that account for such case is probably done via code in some way, but I suspect that there is some sort of generic math function, that is used for such case. And similarly, for all the other cases where complex interactions between parameters is happening.

Or even better, see how certain commercial games handle the problems, and get an idea about how to write your own implementation.

Do you know of any of such math patterns, to handle generic cases? My assumption is that if there are design patters about how to make an object, and what data structure to use for specific cases and situations, there should be an equivalent for math functions too.

Actually, in most cases formulas are extremely easy. It’s mostly just primitive functions and polynomes.

Like Morale = Health*((UnitsCreated+100)/(UnitsDestroyed+100)) with Health being from 0 to 1. It can be balanced with coefficients.
Or iterative formula - check each minute: Morale = Morale + HealthChange + ((UnitsCreated-UnitsDestroyed)/UnitsTotal) where HealthChange, UnitsCreated, UnitsDestroyed all in interval of last minute. Balance with coefficients as well.

Another common example:
Damage = RawDamage/(1+Armour) which makes progressive armour formula act like: Armour=1 to mitigate half damage; Armour=2 to mitigate 2/3rd damage; Armour=3 to mitigate 3/4th damage, etc. Can be modified to support almost any progressive armour curve that will never hit 100% armour.

For experience distribution, you can specify several points then use function interpolation to find polynome you want then use it as formula. Polynome will give you desired result; interpolation gives you easy-to-make way to get it. If you really need features, consider several polynomes which have edge conditions f1(x0)==f2(x0) && f1’(x0)==f2’(x0) in points where they intersect and each of them acts on its own interval, this will give you S shape. But what you described as progression of levels is actually just one polynome, and isn’t S curve.

Look at f(x)=C1*(x+x0)^2+C2*(x+x0)+C3 and tweak it as experience needed for level(or as total experience till that level; doesn’t really matter). Play with C1,C2,C3,x0 coefficients, If you add up to x^4 you would get almost any usual exp distribution.

All formulas are then balanced using coefficients (any part of formula can have coefficient multiplied or added to them), then you test them (just draw graphic of function and check that it behaves how you want it to), and that’s it. If formula is too complex, you can make a table of values of that formula and look up values instead.

In most cases, you will stop at this level of math, without even needing logarithmic or exponential. Logarithm, exponent and almost any other function can be represented as sum of infinite amount of polynomes, but you don’t need such precision, do you?:wink:

3 Likes

Slightly relevant question I’ve head in my head for a while: is there a “curve” object (like those in the particle system settings) that I can expose as a public variable to the inspector so that I can visually tweak it in editor to do something that would be complex to achieve with pure math, and then access that curve from code? The usecase I had in mind was defining the movement arc of a leg in a procedural walking animation. But I’m sure this could have other useful applications.

You can use AnimationCurve. It works in editor, can loop, has .Evaluate(float value) which allows to probe its value in specified point. It has range of (0…1) in both x and y dimensions, but that can be scaled in code with simple multiplication/divison. Not to mention it is meant for and used to work with animations… Although it’s general y=f(x) curve and can be used in other applications too.

This made me wonder if I understood you correctly though.

That sounds like exactly what I’m looking for, thanks a lot! :slight_smile:

The idea was to manually move IK targets for feet over time t from point a to point b and a curve would help a lot in mapping the height y to an f(x) function where I scale x into 0…1 range. The final goals is pretty much what @LaneFox did here:

https://www.youtube.com/watch?v=zhkddGL3fgk

Mmmm good times throwback video. =)

Animation Curves are the bomb for doing stuff like this. Just sample the curve at x time and add that to the position.

Here’s some recent code I used for fading an alpha over time.

        public IEnumerator FadeLineTrail()
        {
            while (_timer < Stats.Lifetime)
            {
                _timer += Time.deltaTime;
                _liveStartColor.a = TrailAlphaCurve.Evaluate(_timer / Stats.Lifetime);
                _liveEndColor.a = TrailAlphaCurve.Evaluate(_timer / Stats.Lifetime);
                LineRenderer.SetColors(_liveStartColor, _liveEndColor);

For a foot going up and down between positions you just need a mountain curve (0 on start/end with 1.0 at midpoint) to add to the goal position for the IK tool. Above example is sampling a normalized curve at [realTime over totalProcessLifetime] which you can apply to just about everything else.

1 Like

You can pretty much use polynomials to represent most models.

If polynomials don’t cut it use an animation curve.

If that’s still not enough drop to a more advanced model. But there is seldom a need for that. Even in my day job as a chemical engineer pretty much everything is polynomials.

2 Likes

Yeah, most of the equations used in games are pretty simple operation-wise. If you want to want to really understand the equations and get into the statistical theory, that would be where the math gets into at least calculus.

1 Like

Thanks for your replies.

So mostly, everything works based on symple polynomial math? I thought that there was some sort of incredible math functions behind the intricate actions behind the attack pattern in some RTS, or how a simulator is able to track your spaceship or airplane to fire at you; or how fifa is able to move the players to counter your moves efficiently.

I did assume that it was more complex than what it really is; thanks a lot for the great examples! I left math behind when I finished school in the late 90s…since then, all the math that I needed was already in libraries that I could load, so no need for doing that anymore.

The animation curve is a great idea; I thought that it was only for animations; but in the end it is a GameObject, so it can be used for other purposes.

Thanks for sharing!

I wonder if you ever made anything with that mech walker example, and if not, why? It looks quite promising.

I was intended for an experimental/learning project but the artist I was planning to collaborate with got busy and we never made anything.

I think that OP is most likely a non programmer.

I think the formula he is interested in is basically all the code that makes the game work.

Not to be that guy, but it would probably fit a thread on its own :wink:

Well, if you consider that I did start to write code in assembly on 68000 on an Amiga 500; in 1990, and work as developer in software companies; I would say that I am a programmer…not a good one maybe :slight_smile:

I am not looking for “al the code that makes the game work”; your thinking was off. Teravision and Boredmormon were spot on in giving me the answer that I was looking for.

I can say that make a game application, is something that is totally different from making applications and tools, and that’s why I appreciate when someone spend time to explain things that for them, may be basic or very simple.

4 Likes

My bad dude.

I guess the way you were talking about the ai logic there, I just thought you meant that like all the code behind the ai in RTS games were like a couple magical functions. Misunderstandings happen.

2 Likes

No worries; I was just suprised that you classified me as “non-programmer”, just because I was inquiring about math functions in certain games :slight_smile:

And I apologize if I am not always clear; English is not my first language, and sometimes it is hard to translate clearly what you have in your mind.

2 Likes