Fundamental Math for Game Developers
pikuma.com
pikuma.com
Math is equational and declarative. When you write:
S = \sum_{i=1}^{100} (2i + 1)
you are declaring that we shall call S the sum of the elements of a certain set. You are not prescribing how this sum ought to be calculated: S is a pure value that doesn't depend on what practical operations you do -- be them on paper or on a CPU -- to actually find out what S is.For example, using equational reasoning, we may write:
S = \sum_{i=1}^{100} 1 + 2 \sum_{i=1}^{100} i = 100 + (100 * 101) = 101^2 - 1
Or, without computing anything at all, we may prove by induction that: (1) \sum_{i=1}^{n} (2i + 1) = (n + 1)^2 - 1
The base case is obvious, and to prove the inductive step it is sufficient to observe that indeed: (n + 1)^2 - 1 + (2n + 3) = n^2 + 4n + 3 = (n + 2)^2 - 1
From which it follows: S = 101^2 - 1
Tying math to code is more like tying your own hands behind your back.All of this is basically an excuse to point out that the code for the product is wrong, the initial value should be 1 and not 0 (or, in general, your monoid identity).
I don't see a problem, if eg it helps with getting some intuition.
Sure there is a formula for this example. But the point in the article of writing this sum as a for loop is to explain what the notation means.
For a programmer who is not familiar with sigma/pi notation for sum/product, explaining it with a for loop is a quick way to understand.
Either is fine for explaining something to a human, as long as it makes sense from the context.
\sum i. lower upper t[i]
and then write the above sum as \sum i. 1 100 2 * i + 1
(after * and + and numbers have been defined as well)There is no reason why the above cannot work both as math notation and as code.
\sum_{n=1}^{\infty} \frac{1}{n^2}
Note that I am not saying that math is code. Practal is based on logic, and logic transcends code. What I am saying is that code is math. At least, that's obviously true for purely functional code. So there is no reason to write the subset of code which is purely functional not in math notation.
Not to pick fights, but your example seems sort of orthogonal to that aspect. What seems more relevant is how you are choosing to structure your code, and how mutable your state is.
It feels a bit like how I found it impossible to memorise all the countries of the world as an adolescent, but then when I discovered Seterra as an adult I was able to memorise them all in less than a week. I need an equivalent perspective shift, but for math.
Hopefully I am wrong and there is a way to learn with a game as with Seterra.
> it takes me multiple days to understand a single formula
This is not weird. Papers are not hard because notation is hard but because the ideas behind them are difficult to understand. It usually takes me several hours of uninterrupted work to read a paper; I'm not a researcher and have never been in academia, but everyone I know says the same thing, so I'm comfortable reassuring you that you're definitely not the odd one out.
A few pointers to go faster:
- Write down your observations (on the paper itself, if you can print it). Ask yourself questions and see if you got things right. Try to replicate their computation steps. Actively engage the topic.
- Keep a dictionary of symbols. What do the authors mean with this ridiculous scribble? Ah, that thing.
- Mathematical notation suffers from catastrophic overloading. For example, if A and B are numbers, then AB is multiplication in their set. If they are matrices, it's matrix multiplication. If A is a matrix and B is a vector, it's the image of B through A. If one is a vector and the other is a number it's multiplication by a scalar, and so on. Try to undo this process and figure out what the virtual method call resolves to :)
- Try to assign "types" to variables. Notation is "dynamically typed", in the sense that a variable could (syntactically) be anything. If you have trouble understanding a formula, try to understand what kind of object each variable represents. Is this squiggle a set or an element? If an element, from what set does it come from? What are the arguments of this function? What is its image? Is this "i" a real variable or a mute variable? To what quantifier is this variable bound?
- Sometimes notation is abused. E.g. we may write:
10n^2 + n = O(n^2)
This is, strictly speaking, nonsense: O(n^2) is a set, and we're saying it's equal to some undefined stuff. What even is "n"? Obviously, it "compiles" to: f(n) = 10n^2 + n
f(n) \in O(n^2)
It's normal to abuse notation, but it may cause confusion if one is unfamiliar with the topic. Try to undo this process if you find it's preventing you from understanding a formula.Translating math to code in itself is an art (or rather, a technical discipline called Numerical Methods). That's (at least one) full course at any real engineering school.
The article is good, but I would expect anyone calling himself a software engineer to have already studied and mastered the material covered.
The rationale is simple. If you want to learn math fast, be prepared that you will stumble upon the lack of training as soon as you go from theory to applications. Normally, you need to go through a few books of exercises to start feeling comfortable with calculus or discrete math. And that's where computer algebra systems step in. They do your math for you.
It's much simpler to learn the basics of any CAS than to train yourself to solve all kinds of equations with pen and paper. It's easier (and more fun to be honest) to practice with a CAS and gain experience on the go too.
In my book, an introduction to SymPy takes only a few pages, but enables practicing with linear systems, matrix operations, calculus, polynomial approximation and interpolation, Bezier curves, NURBS, and polynomial transformations as the book progresses.
And SymPy is not even the most potent CAS out there. It is free and easily accessible though. If you know a little Python, you already know a little SymPy.
Where this might help is if you don't have a strong background in algrabra, and don't know all the properties of e.g. quaternions by heart. You'd implement essentially the answer that you get after playing with a computer algebra system
This is much easier than it sounds. And it's fun too: https://wordsandbuttons.online/sympy_makes_math_fun_again.ht...
I suppose, a lot of not games but game developers use CASes and we don't even know :-)
But what I love about pure SymPy is that it doesn't bring anything new to the language at all. You just write in Python, business as usual, it's just half of your variables are now computable symbols, and you can solve things symbolically whenever you want to.
The best part for me is the SymPy API methods have the same names as the concepts I am trying to teach: solve, expand, simplify, factor, integrate, etc.
Here is a short tutorial to introduce SymPy for anyone who hasn't seen before: https://minireference.com/static/tutorials/sympy_tutorial.pd... also available as notebooks: https://mybinder.org/v2/gh/minireference/sympytut_notebooks/...
It's also worth checking out the SymPy Live Shell, where you can try things out without installing anything: https://live.sympy.org/
For calculus it would make a lot of sense though. You can learn the formulas and general rules so you can do derivative and integrals by hand, but I think we could cut-down significantly on the "integration techniques" topics without any loss. I would love to see a course like that... but I doubt any teacher would be "allowed" to teach it this way, since CALC I and CALC II course curriculum is usually imposed by the university.
Thanks Gustavo.
> covers much of the material in a somewhat project-oriented way
> Lots of detailed examples and exercises
I guess that's why the guy sells a half-dozen 20-40 hour courses from this website which purportedly do that.
There are also many tutorials he and others did in the form of GDC talks over the years in the Math for Game Programmers topic.
And he has a book. https://www.essentialmath.com/book.htm
Some of the more esoteric stuff might take you multiple papers/books to fully-grok. An example of this for me was clipping of geometry against the view frustum (for building a software rasterizer - GPUs handle this for you now). I've got 3 different papers on this exact topic still sitting on my coffee table. I think the hardest overall aspect is thinking in 4 spatial dimensions and getting your head around all of the transforms.
If you are using a 3rd party engine like Unity or Godot, you may find that mastery of this stuff is not as essential. I think it still helps to understand how the scene graph is ultimately rendered, but unless you are building these engines with your own hands, worrying about acquiring this knowledge could be prohibitive to progress.
The author uses JS and HTML5 to implement what he explains but you do not need any JS or HTML5 experience to understand any of it and you can recreate all the code in any language. All you need is experience in a programming language
[0] https://youtube.com/playlist?list=PL7wAPgl1JVvUEb0dIygHzO469...
Note: I am not associated with the author and haven't watched all the videos in the playlist but watched enough of thwm to recommend it.
It's difficult enough for the general audience to learn math notation (alien symbols), but coders face the extra difficulty of "namespace conflicts" for the meaning of math symbols (e.g. = sign does not mean assignment).
I did 2 of his courses and RoI in terms of learning is huge. The crucial point is: he starts with an empty file and codes up the stuff with you at an understandable pace, line by line. A lot of concepts I just understood in hand-waving sense became much more clearer after going through this routine.
I'm planning to next take his Physics course but maybe code it up in Rust so that I'm not re-inventing some wheels (like dynamic arrays). But if anyone wants to brush up on most important pieces of maths: vectors, matrices, calculus I will heartily recommend his courses.