Introduction to Game Development (E22: trigonometry)

  Рет қаралды 90,188

Sebastian Lague

Sebastian Lague

7 жыл бұрын

This episode shows how to find a direction from an angle, and vice versa, using trigonometry. It also covers the conversion from degrees to radians, and shows how to use the trig functions in Unity.
Source code:
github.com/SebLague/Intro-to-...
Graph of sin, cos, and tan: www.desmos.com/calculator/jck...
If you'd like to support these videos, you can make a recurring monthly donation (cancellable at any time) through Patreon: bit.ly/sebPatreon
Or a once-off donation through PayPal:
bit.ly/SupportGamedevTutorials

Пікірлер: 124
@Splitter20
@Splitter20 7 жыл бұрын
A 15 minute from Sebastian > a full semester of trig 270. Although this was 9 years too late for me, I know it will help someone else out there. Thanks for the upload as always!
@cntxgaming6333
@cntxgaming6333 6 жыл бұрын
Exactly my thoughts. Awesome.
@davedogge2280
@davedogge2280 4 жыл бұрын
Shouldn't theta be 60 degrees for the first problem when using sin for the angle within the 90 degrees triangle ? the 120 degrees you're using for theta is outside of the internal angle for the right angled triangle.
@fancygoat1352
@fancygoat1352 4 жыл бұрын
Dave Dogge I think so.
@wtfimcrying
@wtfimcrying 2 жыл бұрын
definitely helped 8th grade me.
@benj4432
@benj4432 7 жыл бұрын
This has to be the most well-made and well-thought-out tutorial I`ve ever seen. I learned more from this 15 minute video, then 3 hours of work in school. And for that I thank you.
@PrototypeTheta
@PrototypeTheta 7 жыл бұрын
In all seriousness game dev is one of the easiest ways to learn complex maths. It's much easier to pick it up when you have an actual reason to use it.
@benj4432
@benj4432 7 жыл бұрын
Prototype Theta Yeah, it's really amazing. I wish it was possible to have lectures like this in school, would help me concentrate so much more. And the visuals really aid the understanding!
@diverseprogrammer18
@diverseprogrammer18 5 жыл бұрын
Prototype Theta, I agree with you.
@razakhan651
@razakhan651 4 жыл бұрын
How to remember Sine cosine and tangent: SOH CAH TOA SOH: Sine is opposite over hypotenuse CAH: cosine is adjacent over hypotenuse TOA: tangent is opposite over adjacent
@bobdagamer640
@bobdagamer640 2 жыл бұрын
We got taught it as Silly Old Hitler Coudnt Advance His Troops Over America
@Maggiethegsd
@Maggiethegsd Жыл бұрын
@@bobdagamer640 💀
@Narvalo_Lastar
@Narvalo_Lastar 11 ай бұрын
In France we say CAH SOH TOA (casse-toi = get the hell out)
@MadBunnyRabbit
@MadBunnyRabbit 4 жыл бұрын
You have no idea how painfull this was to me, ahving the objects rotated in the wrong way and me just adding angles to make it face the right way and being frustrated by it. Now I know why and how it all actually works. Why everybody who makes "tutorials" on rotating your characters just say, oh here, copy pasta this code don't worry about it, when the explanation is so simple! Zero starts at X axis and goes anti cloack wise. SO SIMPLE!!! YET NOBODY EXPLAINS IT!!! I'm sooooooooooooo mad that the answer was so simple and yet I learn about it just now. Sebastian is the real unity MVP!!!
@_-__-_6355
@_-__-_6355 6 жыл бұрын
The production on these videos is off the charts. Amazing!
@homeofgamesnews
@homeofgamesnews 7 жыл бұрын
FANTASTIC TUTORIAL, please make more like this.
@Niklas-cd3sb
@Niklas-cd3sb 4 жыл бұрын
Thanks for this great video. These 15 minutes explained a lot although c# programming, cos, sin and atan are new territories for me. Now i understand most of it and have a solution to my problems. Keep the work up
@lucascionis9542
@lucascionis9542 7 жыл бұрын
Great video! I really like how you teach difficult things in a simple way.
@Reivaj97
@Reivaj97 3 жыл бұрын
Thank you so much for this video. I just completed a computer science degree and I did not know before what is the meaning of a radian. Now I can understand what I have done so far.
@calvinz9126
@calvinz9126 4 жыл бұрын
me watching other trig videos: *visible confusion* me watching this trig video: *visible happiness*
@MG-mm7mn
@MG-mm7mn 2 жыл бұрын
I don't need to watch a Sebastian's video till the end to give a Like. Before watching I've already given.
@kyleme9697
@kyleme9697 4 жыл бұрын
This video video series is simply amazing, but this video in particular should help everyone understand what's going on in the movements. I was getting really confused about that because there are so many different ways of doing it, not to mention the rotation added on top. Thank you thank you thank you. And now I know trigonometry too ... I feel like I should go take a university trig course and get credit for it !!
@ruihong7912
@ruihong7912 Жыл бұрын
The explanation makes these so easy to understand, thank you!
@TheMabaet
@TheMabaet 7 жыл бұрын
Damn amazing! This bloody thing in just 15 minutes but so well explained. Thank you so much!
@bridgevlgrts9026
@bridgevlgrts9026 7 жыл бұрын
Thanks a lot for this tutorial :) You are a amazing teacher !
@enzozaqueu1759
@enzozaqueu1759 3 жыл бұрын
my my, the best explanation i ever had about trigonometry
@curryjl1027
@curryjl1027 Жыл бұрын
Fantastic video! Keep up the great work.
@nadim78nadim
@nadim78nadim 7 жыл бұрын
I swear it's like i asked you to do this just in time :) , Today i posted a question on unity form about this and now u uploaded, i think i'm able to do it on my own but i'll surely understand what im doing better after i watch this so thank you
@Villfuk02
@Villfuk02 7 жыл бұрын
Nadim78Nadim NFA SAME HERE, BRO
@sagorhossen7047
@sagorhossen7047 4 жыл бұрын
this guy is AWESOME math teacher ! i wish i had a teacher like u in the school.
@powersupersport
@powersupersport 7 жыл бұрын
I want you as my teacher in college!
@FlobbinHood0
@FlobbinHood0 7 жыл бұрын
You are a fantastic teacher!
@vpsop2745
@vpsop2745 3 жыл бұрын
Petition to make him college teacher. He explains better
@HyperDevv
@HyperDevv 11 ай бұрын
YES
@mertakbulut2541
@mertakbulut2541 3 жыл бұрын
Crystal clear. Appreciated.
@rolandsaavedra5351
@rolandsaavedra5351 3 жыл бұрын
I literally don't understand how anyone could dislike these on purpose.
@mujahidshaikh2837
@mujahidshaikh2837 6 жыл бұрын
best trigonometric explanation ever. :)
@luizguilhermepelegrini8297
@luizguilhermepelegrini8297 6 жыл бұрын
Nice info tutorial, Sebastian, well done! I would just like to point something out, this angle conversion is not necessary(?) when we are working in 2D once the positive z-axis (which is the main axis for rotation in 2D) is pointing "opposite to the user". This can be seen when as we increase the rotation in the z-axis, the gameObject will rotate anti clockwise instead of clockwise (without any conversion)
@masonmason22
@masonmason22 3 жыл бұрын
Thank you for making this.
@marcossocram6723
@marcossocram6723 7 жыл бұрын
More of these, please! :)
@ColtPtrHun
@ColtPtrHun 3 жыл бұрын
Half a year ago, when I first met Unity, I succesfully "implemented" the Quaternion.LookAtRotation & Quaternion.Slerp functionalities. Just when I finished, I saw in a tutorial, that what I wrote in ~60 lines in days, there are these functions prewritten for you:D Good times.
@K1appy
@K1appy 7 жыл бұрын
great as usual
@nomadcrossfire
@nomadcrossfire 7 жыл бұрын
perfect timing, i need to rotare sprites to face their moving direction and this will solve all my issues
@LeRouxBel
@LeRouxBel 7 жыл бұрын
Great work again, I wish you all the best for the future. I am aware you have had some contact with the good people at Unity, so maybe you know the reason why they didn't use the mathematical convention on this (and the standard forward direction for that matter). If you do, I'd love to know. That mini project looks cool, I might actually re-download Unity and review my C# to do it.
@freecash3474
@freecash3474 5 жыл бұрын
Some quality content damn
@davidmoellersztajnbok5193
@davidmoellersztajnbok5193 2 жыл бұрын
A little late to the party, but wanted to point out something that I didn't see in the comments and I don't believe was mentioned in the video: there's a difference between the two ways of implementing the rotation ("90 - " method and swapping x and z). If you use the "90 - " method (the first one you showed), whenever we give no input, Atan2 is going to become zero (as it should, since the inputDir will just be a Vector3 filled with zeros). However, because we have that constant 90 degrees subtracting from Atan2 * Rad2Deg, the input angle becomes 90 degrees. This means that when we are not pressing anything, our character will face sideways, rotated about the y-axis by 90 degrees. This doesn't happen when we swap the z and x in the Atan2 function because the constant 90 degrees is not there anymore. In this case, when Atan2 goes to zero (whenever we give no input), the entire inputAngle variable becomes zero, as it should, meaning our character will face forward (+Z) with no input instead of sideways (+X) as it did in the "90 - " method. I think this explanation is right? Please correct me if I'm wrong.
@perbergman8639
@perbergman8639 Жыл бұрын
yeah it worked for me, for some reason the player wouldn't rotate diagonally but this fixed it!
@pranav_r
@pranav_r 7 жыл бұрын
Ha! After all my struggles with figuring out everything on my own. I learn a lot, on the way and how trig can be used. Oh well, I guess its not too bad learning this anyway.
@ErtBaran
@ErtBaran 4 жыл бұрын
Great ! 💯 Thanks
@erudejade9763
@erudejade9763 7 жыл бұрын
very nice !
@zerosum535
@zerosum535 3 жыл бұрын
Thank you
@sonsenvrac945
@sonsenvrac945 6 жыл бұрын
You're the best.
@PrototypeTheta
@PrototypeTheta 7 жыл бұрын
Any chance of a video explaining rotation matrices/quaternions?
@novacti5254
@novacti5254 7 жыл бұрын
I think he said he´ll be making a video on Quaternions soon.
@JoeMakaFloe
@JoeMakaFloe 4 жыл бұрын
@@novacti5254 *so that was a fucking lie*
@marcosmanuelvillarreal8823
@marcosmanuelvillarreal8823 3 жыл бұрын
Check the chanel of 3blue1brown, he has awesome videos explaining math stuff
@pitchblack5422
@pitchblack5422 Жыл бұрын
@@novacti5254 its been 5 years
@novacti5254
@novacti5254 Жыл бұрын
@@pitchblack5422 The dream lives on :')
@jbldvdplay2
@jbldvdplay2 Жыл бұрын
excelent, thx
@Skyefaux
@Skyefaux 5 жыл бұрын
THANK YOU!!!!
@McBoccher
@McBoccher 5 жыл бұрын
Wow, I understood all of it! Nonetheless, great video!
@lesarch
@lesarch 2 жыл бұрын
And here I thought I would never use Trigonometry after I graduate.... 13 years later, I'm brushing up on Trig for my game development career. I've come full four quadrants.
@ChupachuGames
@ChupachuGames 7 жыл бұрын
Thank you so much for these amazing tutorials! :D Do you have any plans for Multiplayer/Networking?
@wva6809
@wva6809 7 жыл бұрын
Chupachu that would be cool!
@gwenturo9550
@gwenturo9550 2 жыл бұрын
Are the tooltips about all the methods and parameters you use a monodevelop feature? How can I get those to show up in visual studio? It would help me learn so much 😅
@luizguilhermepelegrini8297
@luizguilhermepelegrini8297 7 жыл бұрын
Can someone explain why it is used transform.Translate relative to the World space, I used relative to local space, and bugs on the movement have appeared. Awesome tutorial btw, well done Sebastian!
@Salvadorioo
@Salvadorioo 2 ай бұрын
I feel, it will be very interesting day☺
@neo7108
@neo7108 7 жыл бұрын
Sebastian could you make tutorial how to move empty object,with some velocity, along the circle with equations (cos(theta),sin(theta)) ?
@GK-dw1fo
@GK-dw1fo 3 жыл бұрын
You're fkin awesome dude!!!
@PuntoDamar
@PuntoDamar 6 жыл бұрын
BEAUTIFUL !!!! what software did you use to create the animations btw?
@Xyz-tw8ym
@Xyz-tw8ym 6 ай бұрын
Unity
@clodgozon3968
@clodgozon3968 4 жыл бұрын
Damn, this is understandable even for 8th-graders like me!
@rafaelcorrea7959
@rafaelcorrea7959 7 жыл бұрын
Could you teach on how to make a sphere mesh based on this? I'm having problems converting 2D to 3D.
@curt15pb
@curt15pb 6 жыл бұрын
I am wondering if you meant to keep 120 degrees as your reference angle? As I am on the toilet I cant check whether you actually used 60 degrees for your calculation and just forgot to update the graphic. I'm taking pre calculus and was wandering what real use trig has within programming. Now I know, very interesting video none the less, thanks for the time and effort. Cheers.
@davedogge2280
@davedogge2280 4 жыл бұрын
I can't see why he used an angle which is external to the right angled triangle, I think you're right, he should have used 60 degrees for the theta angle in the first example. 120 degrees has nothing to do with calculating the cos, sin or tan of that triangle.
@siavashnajafi8054
@siavashnajafi8054 7 ай бұрын
almost after 5 years of being professional game developer still watching this video for rotating god damn cube 😅
@bashirabugharsa8321
@bashirabugharsa8321 7 жыл бұрын
Nice
@bestman2670
@bestman2670 2 жыл бұрын
"B-but when am I ever gonna use this in real life?"
@gdk870
@gdk870 5 жыл бұрын
Wow dude, your tutorials are premium stuff. But could you perhaps explain one tiny detail about how you do the conversions at 8:11 please?
@gdk870
@gdk870 5 жыл бұрын
So after mulling over my problem, I think i might understand how you would get the angle in the opposing unit circle to whichever unit circle you are currently working with (which conventionally is the trig unit circle?). Anyway, from how i understand it, if the trig unit circle is the default, and the angle is 0 degrees. Converting the angle to unity's unit circle goes as follows: 1) 90 - 0 = 90, 2) Taking note that the result is a positive value, you should then "increase" the angle from the naught angle in the unity unit circle (or whichever unit circle for whom you are converting the angle). 3) If the result were negative, e.g. if the angle to convert were 180, then 90 - 180 = -90, which means you should "decrease" the angle from the naught angle in the unit circle of interest, or rather, "increase" in the opposite direction to how angles are usually incremented in that unit circle. Which in the Unity unit circle, where angles increase clockwise, you’d find this angle by "increasing" in the anti-clockwise direction from naught. Just my way of understanding. I would appreciate it if anyone could clear up any errors within it. Thanks for reading this far. Cheers
@cubeyfan2541
@cubeyfan2541 3 жыл бұрын
other trig videos: *confusion* this trig video: tan = blue line : green line
@gryffind96
@gryffind96 7 жыл бұрын
please trigonometric with IK for example climb system
@thedude4039
@thedude4039 3 жыл бұрын
Please make a video about quaternions.
@jugglingmonkey1633
@jugglingmonkey1633 6 жыл бұрын
Is there a reason to why some functions return a radian value while others return a degrees value?
@tribalworx5999
@tribalworx5999 4 жыл бұрын
Deep!
@AnimeScriptMe
@AnimeScriptMe 4 жыл бұрын
I have an advantge of asking my mom about maths since she is very smart at it🥳🥳
@user-ob7co6nb7l
@user-ob7co6nb7l 6 жыл бұрын
during about 9:20, you use Vector3 direction = new Vector3(Mathf.Cos(angleInDegress * Mathf.Deg2Rad), 0, Mathf.Sin(angleInDegress * Mathf.Deg2Rad)). I don't know why you use the Cos and Sin here, Can you please explain? thanks! 为什么这里的Vector3的x一定是Cos呢,为什么不能是Sin?
@kford9271
@kford9271 5 жыл бұрын
He is trying to find the direction his character should be facing based on the angle that is used. As he explained earlier in the video, you find the direction by taking the Sin and Cos of the angle, multiplied by the degree-to-radian conversion, and place that into your coordinates (x, y). (I know I'm answering this question 8 months after it was asked, but maybe someone will stumble upon this video later and have a similar question)
@loicyavorsky4095
@loicyavorsky4095 3 жыл бұрын
Ok so you never know, I'm posting here and hope someone reads this and can help me : I have litterally copied this code into my script. Everything behaves as it should except for one thing : when I dont have any key pressed, the rotation resets to zero. So when my "character" stops moving, it faces 0°, instead of facing the direction of its last movement. Can anyone help ? Besides, I love your series, this is a huge help ! Thank you.
@liteimation4013
@liteimation4013 2 жыл бұрын
Might be a little too late, but you can add an if statement to the area with the GetAngle variable.
@davedogge2280
@davedogge2280 4 жыл бұрын
Shouldn't theta be 60 degrees for the first problem when using sin for the angle within the 90 degrees triangle ? the 120 degrees you're using for theta is outside of the internal angle for the right angled triangle.
@OurielCharoum
@OurielCharoum 2 жыл бұрын
I know I'm super late to the party, but with trig, the triangle is for visualization. you care about the vector of r=1. so you take the angle from origin not in the triangle.
@Jonas-vh3dz
@Jonas-vh3dz 7 жыл бұрын
Could you make a series about AI? Especially about a goal orientated AI like GOAP. I can only find good tutorials about state machines.
@CodeLife_12
@CodeLife_12 2 жыл бұрын
Hey, a bit of a late reply, but if you want to dig deeper into AI I suggest this book : Artificial Intelligence: a Modern Approach. It really covers any AI you might need in a video game and more
@haykav
@haykav 7 жыл бұрын
Great videos as always, but I just want to ask why question. Why are you not using normal letters like A B C... instead of feta ? English is not my first language and I think a lot of people will think like feta is some kind of special thing(I thought the same when I first saw feta in your videos), but then they discover that it's just a letter. I'm pretty sure it confuses a lot of people.
@wylie2835
@wylie2835 7 жыл бұрын
Theta* The symbol represnets the angular position of a vector. Kind of like how X represents the the location along the "X-Axis" and Y represents the location on the "Y-axis". Yes technically you can substitute whatever symbols you want there. However then you have to explain what those symbols mean. By standardizing things like X, Y, Z and Theta you and everyone else immedietly understand what they represent. It confuses almost no one. Most people have already taken high school level math classes to know what theta is.
@haykav
@haykav 7 жыл бұрын
I didn't finish high school yet that's why I left this comment. I just learned the most basic things about trigonometry like sine, cosine and tangent(even though I already knew those. Anyway thanks for the correction
@wylie2835
@wylie2835 7 жыл бұрын
yw
@boring1930
@boring1930 4 жыл бұрын
I now know why we were taught trigonometry from class 8 hehe
@lee1davis1
@lee1davis1 6 жыл бұрын
POP! Brain fart! I've got goo leaking from my ear!
@honkinghorse7347
@honkinghorse7347 3 жыл бұрын
3:44 Wouldn't cos(0) = sin(90 + 0)?
@sb-jo2ch
@sb-jo2ch 3 жыл бұрын
8:27 "which you can maybe imagine as some sort of weapon" yea right... weapon
@777redhood
@777redhood 3 жыл бұрын
how to do this for a 2d charcter top down
@sameerkadam4830
@sameerkadam4830 5 жыл бұрын
finally that shit school math is worth now!!!
@yuriyparamonov1965
@yuriyparamonov1965 7 жыл бұрын
Fuck you are so cool!
@NihongoWakannai
@NihongoWakannai 6 жыл бұрын
syr cxr tyx is much harder to remember than Soh Cah Toa I feel like you should've mentioned opposite/adjacent/hypotenuse notation for this reason Also the fact that unity's angles aren't the same as the standard trig angles has given me so many problems in the past, it's so annoying.
@omaraly41
@omaraly41 4 жыл бұрын
I was going crazy, thank you for highlighting that "Also the fact that unity's angles aren't the same as the standard trig angles has given me so many problems in the past, it's so annoying. "!
@intuithue
@intuithue 5 жыл бұрын
o o f
@GamesOfVaibhav
@GamesOfVaibhav 2 жыл бұрын
Easier chart to learn greater than school 🌝
@pumpedsardines9190
@pumpedsardines9190 7 жыл бұрын
I'm just in 8th grade so this is super interesting
@maz5-870
@maz5-870 3 жыл бұрын
This is how to teach MAth
@cuttlas2955
@cuttlas2955 7 жыл бұрын
tnx who dare to dislike :(((
@rupabasu9511
@rupabasu9511 6 жыл бұрын
but the theta for the coordinates should be 180-120=60
@haph2087
@haph2087 4 жыл бұрын
Hmm... It looks an awful lot like they mixed up cosine and sine when designing Unity, and never changed it to protect old code. And you cannot change my mind. Deal with it.
@marcosmanuelvillarreal8823
@marcosmanuelvillarreal8823 3 жыл бұрын
They also mixed the axes in my opinion, in other 3d programs that i've used the z angle is up but no, Unity chose to go for the y angle, you gotta get used to it
@kolyngay-message6710
@kolyngay-message6710 2 жыл бұрын
As some who studied a lot of high-level math, I find the unity coordinate system is very annoying
@SpaceKebab
@SpaceKebab 2 жыл бұрын
damn you make this too easy bro u should become a math teacher if it were good paying cough cough
@SamuelFoster666
@SamuelFoster666 7 жыл бұрын
Are You Secretly a Genius?
@auklin7079
@auklin7079 4 жыл бұрын
.... WAT :P
@tyru2
@tyru2 7 жыл бұрын
Am I the only guy who came here to learn math?
@littlenarwhal3914
@littlenarwhal3914 6 жыл бұрын
I cringed so hard when he pronounced the name of the greek letter XD
Introduction to Game Development (E23: stealth game 1/3)
15:36
Sebastian Lague
Рет қаралды 130 М.
Watermelon Cat?! 🙀 #cat #cute #kitten
00:56
Stocat
Рет қаралды 37 МЛН
СНЕЖКИ ЛЕТОМ?? #shorts
00:30
Паша Осадчий
Рет қаралды 7 МЛН
Wait for the last one! 👀
00:28
Josh Horton
Рет қаралды 12 МЛН
Trigonometry Concepts - Don't Memorize! Visualize!
32:35
Dennis Davis
Рет қаралды 2,7 МЛН
I Spent a Week Making an AI's Video Game Idea
17:51
Sebastian Lague
Рет қаралды 3,2 МЛН
Trigonometry in Game Development
11:26
DaFluffyPotato
Рет қаралды 55 М.
10 Math Concepts for Programmers
9:32
Fireship
Рет қаралды 1,8 МЛН
What Is The Most Complicated Lock Pattern?
27:29
Dr. Zye
Рет қаралды 1,3 МЛН
Giving Personality to Procedural Animations using Math
15:30
t3ssel8r
Рет қаралды 2,4 МЛН
Creating An Ecosystem Simulation Game In 6 Months
16:28
Fun Master Ed
Рет қаралды 500 М.
Coding Adventure: Clouds
12:50
Sebastian Lague
Рет қаралды 1,2 МЛН
Coding Adventure: Portals
16:06
Sebastian Lague
Рет қаралды 1,3 МЛН
Watermelon Cat?! 🙀 #cat #cute #kitten
00:56
Stocat
Рет қаралды 37 МЛН