So how does your computer ACTUALLY compute sine? Basics of trig and more…

preview_player
Показать описание
What is sin/cos/tan really? How do they relate to the dot product? How are they even computed by your hardware?

Follow me on:

Trigonometry is often cited as an are of math for game development. In this video, I explore trig functions a bit, what they actually compute for you in a visual way, and tie that together with basic linear algebra. Lastly, we explore how these functions are actually computed under the surface.

Рекомендации по теме
Комментарии
Автор

Patrons can now vote for the next video! Thank you for your support.

simondev
Автор

As someone learning game programming with almost zero useful formal mathematical education (through no ones fault other than my own - I was just a terrible math student), this was really accessible. Thank you for taking the time to put this stuff together in such a nice format.

swolfington
Автор

Tangent is very useful for converting angles to slopes. Tangent of an angle is literally precisely the slope of any line with that angle from +x.

rarebeeph
Автор

Yeah, a circle was what I taught myself what sin and cos were about when programming in Scratch as a child. And when school started explaining sin and cos with triangles I was like "huh?"

kangalio
Автор

In defense of the triangle, everything with the unit circle is literally just an application of the triangle! And in my opinion the most intuitive explanation of the weird tan graph actually comes from looking at the triangle: since tan is opp/adj, what happens when the adjacent side gets shorter and shorter? The output is going to get bigger and bigger until you get ridiculously big numbers (ie: 10/1=10, etc.), then when the length adjacent side is 0...

(Also, quick correction at 4:47, the visuals for the Bhaskara I's Approximation show it matching for [0, 2π], though it actually breaks off pretty soon after π.)

orangenostril
Автор

Digital logic designer here. I use the CORDIC algorithm to calculate sine and cosines because it is simple, fast, and small (in terms of chip area) and involves only bitshifts additions and subtraction.

You can also use it to calculate absolute magnitude of a vector, or atan2() of an x/y pair.

hamsterworks
Автор

Very good video. I appreciated the part where you show how the symmetries of the sine function can lead to very efficient and accurate Taylor expansion computations.

A note I will make is while a circle can be divided into any number of parts, the choice of 360 wasn't completely arbitrary historically. 360 is a highly composite number, that is it has more factors than any number before it. You can easily divide it by 2, 3, 4, 5, 6, 8, , 12, 30, 60, 120... (there are several more). It's easy to imagine why this was useful for say, cartographers and navigators trying to make easy shortcuts for computing angles.

spore
Автор

I have been wondering what Sin Cos and Tan were for ages. I could never find any halfway descent explanation of what they were only that they were. Thank you immensely.

williamfox
Автор

5:35, Recently seen this concept again in the Kaze Emanuar M64 optimization videos. Using a folded polynomial solution, with 1/8th of the sine wave to reconstruct the whole sin/cos graph.

Kaptime
Автор

This is one of the best videos I've seen on this topic!
As an oldskool gamedev, the dot product, lookuptables, and quadrantifying/octantifying periodic values were very useful.

This video just makes understanding them easier even for us retired users.

Have you made videos on splines? I have been using both the Bezier and Catmull-Rom to do enemy patterns in SHMUPS. They're way easier to predict than using a combination of trig functions. A piece on how to derive an angle in regard to "t" as well as reparameterizing them so that points across splines are uniform in distance would be swell.

Looking forward to your next vid!

richardericlope
Автор

Love the video. Unlike many commenters here I learned trigonometry exactly as explained in the video with a circle of radius 1. In fact trigonometry was the first time that math "clicked" for me. I guess having a good way of visualizing what is being explained helps me a ton.

rin
Автор

I've fiddled around with approximating sines and cosines. I like the idea of reducing things to the range from 0 to pi/2. One more step, I've found, is to care only about the range from 0 to pi/4. What happens from pi/4 to pi/2? Well, that'd be the complementary function of the complementary angle. Like, the sine of 80 degrees would be the same as the cosine of 10 degrees. Polynomial expansions close to zero tend to get very accurately very quickly.

So that region around pi/4 (45 degrees) is where things would converge the slowest, but even then, I think we've got an approach for that. Remember that sin(a - b)= sina*cosb + cosa*sinb, so if you wanted to calculate the sine of 40 degrees, that's the sine of (45 - 5) degrees. Okay then, so we'd have to calculate sin(45)*cos(5) - cos(45)*sin(5), and sin(45) = cos(45) = 0.7071etc, so we've got very little to calculate. The answer will be 0.7071etc*(cos(5) - sin(5)), and those will converge quickly. At this point we're approximating just from 0 to pi/8.

kingbeauregard
Автор

I have spent a year trying to find a video that explains what “radians” are as a unit of measure and you give it as a 1-sentence aside! Such a clear explanation, thank you.

peddlereffects
Автор

This method works pretty well for sine, cosine, and tangent. There’s some other optimizations you can do, such as representing the Taylor polynomial with nested multiplication. Also, for arcsine, arccosine, arctangent, and log, some basic identities come in handy.

JM-usfr
Автор

I hated trigonometry in high school because any time I asked what the trig functions were I would just get a vague hand-wavy "ratio of triangle sides" explanation. Even though I eventually learned all of this on my own eventually I still appreciate you giving a more intuitive explanation.

HopUpOutDaBed
Автор

I never understood trigonometry before and even in coding all I needed to know that they make waves that I can exploit to make something move smoothly back and forth.
But now I finally understand... until I forget again because I still won't use them to their fullest.

csaki
Автор

Thanks! Solid video as always! Can't wait for the new course

nathanhedglin
Автор

This 5 minute of trig taught me more than my math teacher could in 6 months

bp_cherryblossomtree
Автор

This is how sin and cos routines in various math libraries work, and it is often very possible to write an algorithm for trig operations which is lower latency than using a single CPU instruction with the trade off being less precision. Though latency can also be a little misleading at times since those operators are highly parallelized. If the FSIN instruction takes 170 cycles to give a result that doesn't mean your computer spends all that time waiting for an answer. It will try to do other operations with that core while it waits and can even have several FSIN operations being calculated simultaneously. So which is faster in practice can be more nuanced, as always seems to be the case with modern super scalar architectures.

In hardware these operators are usually implemented using the CORDIC algorithm. The resources you linked discuss that a little.

Essentially you implement a more general function that rotates a vector (x, y) by an arbitrary angle.

For anyone with textheworld installed:

[;\begin{bmatrix}x_{out} & -sin(\theta)\\ sin(\theta) & cos(\theta)\end{bmatrix};]

Sine and cosine in that situation are just the x and y coordinates that result from rotating the vector (1, 0). Using only rotation in various ways it's possible to implement several other tricky functions as well including inverse trig functions, logarithms, hyperbolic trig functions, square roots, and even standard multiplication and division. The documentation for the Xilinx CORDIC IP core is pretty good for seeing how all that works in practice.

The CORDIC algorithm takes advantage of the fact that rotation by an arbitrary angle can be expressed using a series of rotations by different angles which sum to that angle. For example to rotate by 67 degrees you could rotate a vector by 45 degrees, then by 30 degrees, then by -8 degrees. These smaller angles are chosen using powers of 2 so that the algorithm can use a bit shift operation rather than a more expensive multiplication. With this structure CORDIC calculates an answer to N bits of precision with N iterations of shift-addsub. The output is still an approximation, but its as precise an implementation of the trigonometric operations as floating point numbers are able to express.

esven
Автор

Well this video is really intuitive and useful, people use these built in libraries directly in their code and they don't know how the background process goes, thanks a lot for this video

harshans