Mathematics in Video Game Programming

I just finished the second year at Uni, conducting a game course, it always pushed me to how mathematics and game programming are related. So far I have used Vectors , Matrices and Quaternions in games, I can understand how they fit into games.

This is a General Question about the relationship between math and programming for real-time graphics, I'm curious how dynamic math is. Is this the case when all formulas and derivatives are predefined (semi-defined)?

Is it even possible to calculate derivatives / integrals in real time?

Here are some of the things that I don’t see how they fit into programming / math. As an example.

  • MacLaurin/Talor Series I see that this is useful, but do you really need to transfer your function and its derivatives, or can you pass one function to it and develop derivatives for it?

     MacLaurin(sin(X)); or MacLaurin(sin(x), cos(x), -sin(x)); 
  • Derivatives /Integrals This is associated with the first point. Computing y' function executed dynamically at runtime, or it is something that is statically executed, possibly with variables inside a given function.

     f = derive(x); or f = derivedX; 
  • Bilnear Patches We learned about this as a way to create landscapes in small pieces that could be β€œstitched” together, is this something that happens in games? I have never heard of this (provided that my knowledge is very limited) is used with procedural methods or otherwise. What I have done so far includes arrays of processing vertex information being processed.

Sorry if this is off topic, but the community here seems to be in place, about this.

Thanks.

+4
source share
8 answers

Skizz's answer is true if taken literally, but it needs a little change to calculate the derivative of the C ++ function. We change the skizz f function to

 template<class Float> f (Float x) { return x * x + Float(4.0f) * x + Float(6.0f); // f(x) = x^2 + 4x + 6 } 

Now you can write C ++ - a function to calculate the derivative of f with x. Here is a complete stand-alone program for calculating the derivative of f. This is accurate (for the accuracy of the machine) since it does not use the inaccurate method as the final differences. I explain how this works in paper I wrote. It generalizes to higher derivatives. Note that most of the work is done statically by the compiler. If you increase the optimization and your compiler makes decent lines, it should be as fast as anything you could write manually for simple functions. (Sometimes faster! In particular, it is nice to amortize the cost of computing f and f 'at the same time, because it makes it easier to eliminate a common subexpression for the compiler than if you write separate functions for f and f'.)

 using namespace std; template<class Float> Float f(Float x) { return x * x + Float(4.0f) * x + Float(6.0f); } struct D { D(float x0, float dx0 = 0) : x(x0), dx(dx0) { } float x, dx; }; D operator+(const D &a, const D &b) { // The rule for the sum of two functions. return D(a.x+bx, a.dx+b.dx); } D operator*(const D &a, const D &b) { // The usual Leibniz product rule. return D(ax*bx, ax*b.dx+a.dx*bx); } // Here the function skizz said you couldn't write. float d(D (*f)(D), float x) { return f(D(x, 1.0f)).dx; } int main() { cout << f(0) << endl; // We can't just take the address of f. We need to say which instance of the // template we need. In this case, f<D>. cout << d(&f<D>, 0.0f) << endl; } 

He prints results 6 and 4 , as one would expect. Try other functions f . A good exercise is trying to develop rules that allow you to subtract, division, trigger functions, etc.

+5
source

2) Derivatives and integrals are usually not calculated on large data sets in real time, which is too expensive. Instead, they are precomputed. For example (at the top of my head) to make one diffuse carrier Bo Sun et al. use your β€œair lamp model”, which consists of many algebraic shortcuts, to get a pre-computed lookup table.

3) Streaming large datasets is a big topic, especially in the area.

Many mathematicians you will encounter in games have to solve very specific problems and are usually kept simple. Linear algebra is used much more than any calculus. In Graphics (I like it most), many algorithms come from research conducted in the academic community, and then they are modified for speed by game programmers: although even academic research speeds up their task these days.

I recommend two books of real-time collision detection and real-time rendering, which contain the courage of most of the mathematical and concepts used in programming the game engine.

+2
source

I think there is a fundamental problem with your understanding of the C ++ language itself. Functions in C ++ are not the same as mathematical functions. So in C ++ you can define a function (which I will now call methods to avoid confusion) to implement a mathematical function:

 float f (float x) { return x * x + 4.0f * x + 6.0f; // f(x) = x^2 + 4x + 6 } 

In C ++, there is no way to do anything using the f method, except to get the value of f (x) for the given x. The mathematical function f (x) can be easily transformed, for example, f '(x), which in the above example is f' (x) = 2x + 4. For this, in C ++ you need to define the df (x) method:

 float df (float x) { return 2.0f * x + 4.0f; // f'(x) = 2x + 4 } 

you cannot do this:

 get_derivative (f(x)); 

and the get_derivative method converts the f (x) method for you.

In addition, you will need to make sure that when you want to get the derivative of f that you invoke with the df method. If you accidentally called a method for the derivative g, your results would be incorrect.

We can, however, approximate the derivative f (x) for a given x:

 float d (float (*f) (float x), x) // pass a pointer to the method f and the value x { const float epsilon = a small value; float dy = f(x+epsilon/2.0f) - f(x-epsilon/2.0f); return epsilon / dy; } 

but it is very unstable and rather inaccurate.

Now, in C ++, you can create a class that will help here:

 class Function { public: virtual float f (float x) = 0; // f(x) virtual float df (float x) = 0; // f'(x) virtual float ddf (float x) = 0; // f''(x) // if you wanted further transformations you'd need to add methods for them }; 

and create our specific math function:

 class ExampleFunction : Function { float f (float x) { return x * x + 4.0f * x + 6.0f; } // f(x) = x^2 + 4x + 6 float df (float x) { return 2.0f * x + 4.0f; } // f'(x) = 2x + 4 float ddf (float x) { return 2.0f; } // f''(x) = 2 }; 

and pass an instance of this class to the serial extension program:

 float Series (Function &f, float x) { return ff (x) + f.df (x) + f.ddf (x); // series = f(x) + f'(x) + f''(x) } 

but we still need to create a method for the derived function, but at least we won't accidentally make a mistake.

Now, according to others, games tend to prefer speed, so the math is simplified: interpolation, pre-computed tables, etc.

+2
source

Most mathematical exercises in games are designed to calculate the speed of trading by accuracy as cheaply as possible. For example, most of the number of crunches uses integers or float with one precision, rather than doubles.

Not sure about your specific examples, but if you can predefine a cheap formula (for calculating) for a derivative, then it is preferable to calculate things on the fly.

+1
source

In games, performance is paramount. You will not find anything that would be dynamic when it could be done statically, unless it leads to a noticeable increase in visual fidelity.

+1
source

You may be interested in the symbolic differentiation of compilation time. This can (in principle) be done using C ++ templates. No idea as to whether games do this in practice (symbolic differentiation can be too expensive for proper programming, and such widespread use of templates can be too expensive at compile time, I have no idea).

However, I thought you might find the discussion on this topic interesting. Googling's "C ++ template symbolic derivative" provides several articles.

0
source

There are many great answers if you are interested in symbolic computing and computing derivatives.

However, like a sanity check, such a symbolic (analytical) calculus is not practical to do in real time in the context of games.

In my experience (which is more three-dimensional geometry in computer vision than games), most calculus and mathematics in 3D geometry happens by calculating things offline in advance and then coding to implement this mathematics. Very rarely, you need to symbolically calculate things on the fly, and then get analytic formulas on the fly this way.

Are any programmers checked?

0
source

12)

The MacLaurin / Taylor series (1) are constructed from derivatives (2) in any case.

Yes, you are unlikely to need to symbolically calculate any of them at runtime, but surely user207442 has a great answer if you need it.

What you discover is that you need to perform a mathematical calculation and that you need to do it in a reasonable amount of time, and sometimes very quickly. To do this, even if you reuse other solutions, you will need to understand the basic analysis.

If you really need to solve the problem yourself, then up is something that you often only need an approximate answer. This means that, for example, an extension of the type of series may well allow you to reduce a complex function to a simple linear or quadratic one, which will be very fast.

For integrals, you can often calculate the result numerically, but it will always be much slower than the analytic solution. The difference may be the difference between practice or not.

In short: yes, you need to learn math, but in order to write a program, and not run it for you.

0
source

All Articles