Menu
O mnie Kontakt

W najnowszym filmie SimonDev omawia, jak funkcje trygonometryczne rzeczywiście działają, wprowadzając widzów w bardziej przystępny sposób ich zrozumienia. Zaczyna od klasycznego trójkąta prostokątnego, gdzie definiuje podstawowe funkcje takie jak sinus, cosinus i tangens. Simon zauważa jednak, że te metody są w dzisiejszym programowaniu mało praktyczne, a znacznie bardziej pomocne jest rozważenie okręgu jednostkowego. Pojęcie radianów jest również wyjaśnione, co reprodukuje zależność między kątami a długościami łuków. Przez tę wizualizację, Simon stara się uprościć zrozumienie wykresów funkcji sinusoidalnych, pokazując, jak ich wartości zmieniają się przy obracaniu wektora wokół okręgu jednostkowego.

Następnie, Simon rozwija temat dot product, który jest powiązany z funkcją cosinus. Używa on pojęcia wektorów oraz skalarów, podkreślając, jak te matematyczne koncepcje mają zastosowanie w obliczeniach w programowaniu gier. Dzięki wizualizacji z okręgu jednostkowego, widzowie mogą lepiej zrozumieć, dlaczego dot product odpowiada wartości cos theta, niezależnie od kąta. Simon również porusza sposób implementacji funkcji trygonometrycznych w kodzie, przedstawiając różne metody aproksymacji i wykorzystywania tablic z danymi.

W filmie SimonDev podaje przykłady historycznych koncepcji obliczania sinusów, w tym metody indyjskiego matematyka Bhaskary, który stworzył formułę pozwalającą na bardzo dokładne wyniki w zakresie od 0 do pi. W kontekście współczesnego programowania, Simon wyjaśnia, dlaczego tablice z danymi nie są już powszechnie używane z powodu problemów z pamięcią podręczną. Zamiast tego programiści korzystają z rozwiązań wykorzystujących rozwinięcia Taylora oraz inne metody, które ułatwiają obliczenia.

Pod koniec wideo Simon obiecuje, że opublikuje więcej materiałów dotyczących trygonometrii i macierzy w przyszłości, co z pewnością przyciągnie zainteresowanie widzów poszukujących intuicyjnych wyjaśnień matematyki dla programowania gier. To podejście do nauczania, które stara się uprościć zrozumienie złożonych tematów, jest otoczone pozytywnym odbiorem.

Na chwilę obecną film na kanale SimonDev ma aż 315401 wyświetleń oraz 15131 polubień, co pokazuje, że tematyka wzbudza dużą uwagę w społeczności programistycznej. Dodatkowo, zbliżająca się oferta kursu do samodzielnej nauki matematyki, w tym trygonometrii, z pewnością przyciągnie jeszcze większą publiczność.

Toggle timeline summary

  • 00:00 Wprowadzenie do funkcji trygonometrycznych, koncentrując się na trójkątach prostokątnych.
  • 00:22 Omówienie stosunków sinusów, cosinusów i tangensów.
  • 00:35 Wizualizacja funkcji trygonometrycznych za pomocą okręgu jednostkowego.
  • 01:10 Wprowadzenie radianów jako jednostki pomiaru kątów.
  • 01:45 Wyjaśnienie sinusów, cosinusów i tangensów w kontekście okręgu jednostkowego.
  • 02:19 Graficzna reprezentacja funkcji sinusów, cosinusów i tangensów.
  • 02:39 Zrozumienie nieokreślonej natury tangensa w niektórych kątach.
  • 02:52 Wyjaśnienie iloczynu skalarnego w odniesieniu do cosinusa.
  • 03:41 Rozważanie związku między iloczynem skalarnym a cosinusem.
  • 04:25 Dyskusja na temat implementacji funkcji trygonometrycznych w kodzie.
  • 05:48 Wprowadzenie rozwinięcia Taylora jako metody obliczania funkcji trygonometrycznych.
  • 06:15 Techniki optymalizacji obliczania sinusa, w tym redukcja zakresu.
  • 07:01 Opis, jak funkcje sinusowe są obliczane w sprzęcie.
  • 07:29 Ogłoszenie nadchodzącego kursu matematyki dotyczącego trygonometrii i tworzenia gier.

Transcription

In school, I learned the trigonometric functions kind of like this. You have a right-angled triangle, an angle typically called theta here, mathematicians love Greek letters, and you label the sides opposite-adjacent hypotenuse. Then you have the famous formulas, sine equals opposite over hypotenuse, cosine equals adjacent over hypotenuse, and tan equals opposite over adjacent. So in 20 years of development, even in graphics work, I think for me this triangle has come in handy close to zero times. So instead, another way of visualizing this is to start with a circle. And to simplify things further, we can use one with a radius of exactly 1. That is, any point that lies here on the circle is at a distance of exactly 1 from the center. Typically, this angle here between our vector and the x-axis, theta, is expressed in degrees, ranging from 0 to 360. But apparently this is some arbitrary system left over from, well, they're not quite sure. But if you think about the circumference of a circle, that's defined by 2 times pi times the radius, r. With a radius of 1, that boils down to just 2 times pi. So instead of degrees, if we started at the x-axis here, and moved one unit of distance along the circle, we could invent a new unit for angles, called a radian. So now we express angles in terms of distance along the outside of the circle. And thus a circle is 2 pi radians instead of 360 degrees. And that's how radians are made. Now, given this point here on the circle, it forms an angle theta. And since the radius of this circle is 1, the hypotenuse is 1. So sine is just the distance here along the vertical. And that means that cosine is just the distance here along the horizontal. And tan? Well, if you extend a line here from x equals 1 upwards, and you extend the vector forming your hypotenuse out this way, where the two meet, tan gives you this distance here. The distance from their intersection down to the x-axis. I never use this, but it's there. As you move this point around the circle, we obviously expect cos theta, sine theta, and tan theta to change. And we can graph those changes. So sine theta oscillates between 1 and negative 1 as this vector rotates the circle. And cos theta oscillates in much the same way, but just slightly offset from sine of theta. Tan is interesting, because at pi over 2 and negative pi over 2, you can see that tan theta is undefined. Hopefully now seeing this in action, and what tan really is, the graph of tan theta makes much more sense. That's why it kind of freaks out at certain values. This is where it gets a little interesting. If we call this vector a, we know that cos theta is just the x component of this vector here on the unit circle. Or you could say this in a different way. A slightly longer way of saying it is in terms of the full vector a. It is a dot x times 1 plus a dot y times 0. Or again, now we can say it in terms of both a and b, where you know the b vector is just the x axis, or 1, 0. So it's a dot x times b dot x plus a dot y times b dot y, which is now defined in a more mathematical way. It has a name. It's the dot product. So the dot product of a and b is equal to cos theta, which is just this horizontal distance here, if a and b are unit vectors. The more general equation you were taught in school was that a dot b is equal to the length of a times the length of b times cos theta. So now it might make more intuitive sense, then, for why, given any two unit vectors, if we take the dot product between them, you get cos theta. Because if you were to rotate these things around the circle until one hits the x axis, and suddenly this is just reduced to our simple distance along the x axis. Actual real proofs exist, with, like, real math and everything. This isn't one of them, just a nice way of visualizing things. But this is also why the dot product is used to get the scale or projection of one vector onto another. Because that's all this distance is. So then, how are these functions implemented? If you have a chunk of code, when you call your hardware or library function for stuff like sine, what kind of math sorcery are they doing? Well, these tend to be computed in different ways. Various approximations have existed for ages. Back in the 7th century, the Indian mathematician Bhaskara gave the following formula, which I'm not going to read, but it worked really well in the range of 0 to pi, giving an absolute error of, like, 0.0016. In game development, way back in the day, it was often acceptable to use something called a lookup table. By precomputing values and either finding the closest match, or linearly interpolating between adjacent values, it was possible to use a relatively small lookup table to get acceptable results. A lookup table with 512 entries might have an error of around 0.00001, or 1 e to the negative 6. But getting things super right in games isn't usually critical. No one's life is on the line, unless you're Jigsaw. So for example, you could use 16 values from 0 to pi over 2, and that gives an error of 0.0014. But cache misses are expensive these days, so lookup tables aren't used as often anymore. If you were to compute these trig functions directly, the first thought is a Taylor expansion, and courtesy of Wikipedia, they look like this, which does the job nicely. But a common complaint with Taylor series is that the error isn't uniform. The error scales the further you are from 0. Better approaches have a general theme. They combine a whole bunch of ideas. They tend to do what's called a range reduction, followed by an approximation, and then a reconstruction step. First off, looking at a sine wave, you clearly don't need to approximate from negative infinity to infinity. We already know that it repeats after 0 to 2 pi. From there, you can exploit symmetry. This half is really the same as this half, just upside down. And this half of the wave is really just a mirror of the other side. So in reality, you only need to approximate a small portion. So now you have 4 identical quadrants for this function, just flipped and reversed. So given a point in any of these quadrants, you'd map it to a point in the first. Then you may use a polynomial approximation, like the Taylor series we just covered, or something more advanced like minimax. Anyway, you compute your value, then you map it back into the appropriate quadrant, and voila, you have an idea of how your hardware computes the sine value. The sine function on the screen is running the code you see on the left. Intel has a paper from like 20 years ago which details how a lot of their trig functions were implemented. You can also poke around the new C library and see similar ideas in action, if you can read the code. It's not the easiest to work through. Robin Green also has a nice write-up on their blog as well, which is linked in the description. Lastly, this explanation is taken from an up-and-coming math course I'm working on. It'll be available soon. If you're interested in more intuitive explanations of trigonometry, matrices, and math for game development, check it out. Until next time, cheers.