Python 3D Graphics Tutorial 12: Understanding Orientation and Axis Parameters

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter from toptechboy.com and we're here today with lesson number 12. in our incredible new tutorial series where you're learning how to do 3d graphics and animations in python what i'm going to need you to do is pour yourself a nice strong cup of black coffee that is straight up black coffee no sugar no sweeteners none needed that's your goat juice go get you some what i'm also going to need you to do is i am going to need you to fire up your most excellent visual studio code and as you're doing that as always i want to give a shout out to you guys who are helping me out over at patreon it is your support and your encouragement that keeps this great content coming you guys that are not helping out yet look in the description down below there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough this shameless self-promotion let's jump in and talk about what we are going to learn today and what we are going to learn is we are going to begin to learn about orienting objects in three-dimensional space now what do we know how to do right now we can create an object and then we can move it in x we can move it in y or we can move it in z or we could change its size we could also change its color and change its opacity those are the things we've learned so far but you're not always going to have your atoms be lined up with the coordinate axis so it's not always going to be pointing along the x-axis what if you wanted it up here at an angle or to orient it more arbitrarily in three-dimensional space and that's what we're going to begin to undertake today is to teach you how to do that and we do that with the visual python axis parameter and i will be showing you how that works today so let me go ahead and get out of your way and we will come over here we are working in the most excellent v python we are working in the most excellent v python folder we will create a new uh uh we will create a new program called let's just call it axis a x i s dot axis orient like that axis orient dot p y no dot i said dot wrong it's axis orient dot p y okay the dot p y is kind of important we will hit that and boom we have a fresh new python program just waiting for you to write it we will start by importing who importing our friend uh our friend mr v python so from the python import star like that and who else is your friend mr nunpai is your friend so we are going to import numpy as np make sure that it doesn't autofill that for you it's hard to get it to not do that in p and it keeps doing that this is annoying me okay let's see all right so now we have v python and we have numpy all right now what we want to do is we want to just think of we're going to make an arrow right because an arrow points and therefore it's easier to understand orientations if we use an arrow so what i'm going to do is i'm going to create my x arrow and my x arrow is just going to be oh let's give it a color color equal color dot red and then i want to do a little better with doing parameter design and so stop that up here i'm going to set a parameter of arrow length and i'm going to set that equal to 2. so i'm going to be doing parametric design also i want a shaft width and so my arrow thickness is going to be the shaft width like how thick the shaft is and i'll make that like a 0.1 i think and so that arrow t is going to be the thickness of the arrow and then i will come down here and i will put those in and say my length is going to be equal to arrow l i will move that where you can see it i'll get this out of your way my length is equal to arrow l and then my shaft width make sure you spell that right shaft width is going to be equal to arrow thickness all right now you guys remember that when you're creating a graphic you've got to keep the program alive so i'll just say wow true when it's true true true is always true what i want to do pass so this is just going to loop forever ever so it keeps the graphic alive so this should create an arrow alright of length 2 and shaft width of 0.1 let's see how this looks whoa what did i do that was crazy wasn't it it's x arrow is my object and that is equal to arrow arrow is the python the v python command that creates an arrow all right we'll get this roll in here okay boom got an arrow and you can see the default position is it is pointing it is pointing in the x direction it is pointing in the positive x direction it is kind of clue g it is too big and fat i think i want to make that quite a bit smaller so i'm going to say 0.01 let's see what that looks like get something that looks like a decent arrow all right boom i think that's pretty good it's an arrow its length is two it's a little bit too thin so me and my ocd in my mind there is a perfect arrow and we want to get that perfect arrow and so here we go and that my friend is the perfect arrow that i wanted to get so the thing to notice is is that it points it in the positive x direction that is where it points it and you can point it in other places by when you create it make the axis parameter and the axis is a vector so it is equal to a vector and you give it three parameters where it's pointing in x okay where it's pointing in y and where it's pointing in z well if we want it to just point in x and not in y and not in z that would be saying let's come out 1 and x go 0 and y and go 0 and z and it should point it right there so i'm going to say go in the x direction not in the y direction not in the z direction and now that should be exactly what we had before because i think that is the default so let's just make sure boom exactly the same all right now you guys tell me you guys tell me if we wanted it to point up instead of over what would this be well if this is x and this is y and this is z we would make this zero we would make this one and then z would be zero and so if we do that boom it's pointing up all right what if we want it pointing at us in the z direction we would make it 0 0 and one like that okay and you see that's coming out the page at us in a kind of awkward way all right so you see that's coming out the page at us all right so you begin to see how this axis command allows us to know where to point and so let's play around a little bit more with it so i'm going to get my x arrow i'm going to change it back to x so it's going to be 1 0 0 and then let's snag that line of code because we don't want to type the whole thing in again and then we're going to come down here and what is this one going to be let's make this our y arrow and our y arrow is going to go in 0 turn on the y to 1 and then 0 in the z and then let's make this color let's make this green g-r-e-e-n i'm trying to create a coordinate axis with red and then green and then out towards me is going to be blue okay so that is going to be like that and then i can do that and let's see what happens we should have an x and a y now boom we got a coordinate axis an x and a y looking good i really like that okay i want to show you another thing that you could do you could come here and you could do another one and make that z and so this one is going to be blue so we're going to have red green blue and we're going to have a three dimensional axis now we're going to create red green blue and then we're going to have a three dimensional axis and this one is going to be zero and zero and one okay and so that we're gonna hit we're gonna kill the old one and we're going to run it and i should have a good three-dimensional axis boom look at that okay perfect do you see we have a three-dimensional axis all right now there's one quirky thing there is one quirky thing about this that you've got to know and that is let's say that i didn't give it an axis here okay let's say i created it and i still created it with that length of arrow length which is two right but let's say that i create it and then i come down here i come down here to set its axis so basically i'm changing the axis so now what i'm going to say is whoa and this was supposed to be 0 i'm sorry the reason it worked because it just created a new one all right but now what i'm going to do is i'm going to say z arrow z arrow dot what what am i going to change its axis because i want it to point in the right direction so that's going to be a vector and then what direction do i want it to point in well i want it to point in the z direction which is 0 comma 0 comma 1. and this is kind of exactly what we did before the only difference being is i'm creating it at the default position which is x and then i'm changing it to the z direction so let's see what happens when we do that okay and this is going to show you the problem what is the problem if you change the axis statement it changes its size when you change its axis it changes its length and i want you to see what it did okay when you set you set length to what up here you set length to two and it was two but then when you change the axis it changed its size and when it said 0 0 1 it's thinking well i guess he wants it to be one long because he's saying go out one and you could think oh we'll go out 0 0 2 and that would fix it no then you're not doing parameter design so this is the way you do it you set the axis and after you set the axis you immediately fix the length okay and that is back to arrow length like that and i think that should fix it but you've always got to be mindful anytime you change where something is pointing you have to go back and tell it what size it's supposed to be again and that didn't fix it that did not fix it why did that not fix it i think i must have had a error in there oh yeah typo typo did you guys see that let's try it now that one scared me it kind of scares me when it doesn't boom okay do you see that everything's the right size now so learn learn this lesson anytime you change where something is pointing after you change where it's pointing go back and remind it what its size is or you're going to have all kinds of difficulty okay so let's go back and let's do this the way we should do it and so we're going to take this and copy it and then we're going to set this up as axis is equal to vector like that so we get that back in there and we will take out this little experiment that we did now why are we okay here because we set the axis and then out here we set the length and so it points it and then it sets the direction up so that works okay now that is really easy if you're pointing in the x direction the y direction in the z direction but what if you want to point somewhere else i'm just going to tell you you guys it will not work like let's say if you wanted a 45 degree angle and you said well let me set x to 1 and y to 1 you're never going to make it do what you want by trying to do that things that way you have to think in terms of how long is your vector its arrow length and then what angle do you want to point it at so like this is an angle of 0 and that's an angle of 90 that would be 45 degrees so you've got to think in terms of pointing it and therefore you've got to learn a little trigonometry guys i'm going to teach you the trigonometry and if you will pay attention you will understand it don't get afraid i'll show you how to do it but you've got to learn the trigonometry if you're going to be able to animate things and make them do what you want them to do you've got to understand some very simple trigonometry better take an extra shot of copy here okay the first thing in trigonometry that you are going to have to learn is you are probably familiar with with degrees measuring things in degrees and when you measure things in degrees how many degrees in a circle there are 360 degrees if you go all the way around so this is like 0 90 180 270 and then 360. so if you go through 360 degrees you get all the way around right this is the problem the world of engineering does not operate in degrees and the world of visual python does not operate in degrees it operates in radians how far is it all the way around the circle in radians it's 2 pi radians so if you go all the way around you've gone 2 pi so this over here that was 180 that is half of all the way around that is pi so what is this this is pi over 2. so you could view it as zero one pi over two two pi over two is pi three pi over two and then four pi over two four pi over two is two pi so you see go all the way around you go 2 pi and each 90 degrees you can think of as pi over 2 another pi over 2. does that make sense so let's try to draw that up here just to make sure that you can see that and so we're going to draw a little bit of a coordinate axis here i think you can see that okay and so then you have this vector that you are spinning around okay this vector is spinning around and if it goes this far it has gone pi over 2 radians if it goes this far it has gone pi radians if it has gone this far it has gone 3 pi over 2 radians and then all the way around is 2 pi and so you go 1 pi over 2 2 pi over 2 3 pi over 2 4 pi over 2. 4 pi over 2 is 2 pi 2 pi over 2 reduces to pi so you count by pies over 2. now if you see this at 45 degrees what would that be well that would be halfway to 90 halfway to pi over 2 would be pi over 4. and if you want to go from degrees to radians with a formula what you can say is radians how many radians do you have well it's equal to degrees how many degrees you're in and you divide degrees by 360 and then you multiply by 2 pi okay so this will take degrees and convert it to radians if you ever need to do that okay now what we want to do is we want to think about we can do this case we can do this case we can do this case we can do this case but how about the cases in between well what you got to see is is that this vector as it spins around its length doesn't change it's that arrow length the length doesn't change well here clearly i go over one and i go up zero i go over one and i go up zero so for that case what i'm saying is i go over 1 and i go up 0 didn't mean to move that i go over 1 and i go up 0. or in this case i go over i go over 0 and i go up 1. those cases are really clear but how about the in between cases what i still need is i need the how far over in x and how far up and why which is just exactly what i did before over 1 up 0 over 0 up 1. it's how far over and how far up well for right here you're still going to go over by some x amount and you're still going to go o up by some y amount or i'm going to go way over in x and up just a little bit in y so it's still think of this tip of this pin as having an x value and a y value okay but what are those x values and those y values well you get them using trigonometry okay so i will draw another axis here and if you're not seeing it don't worry once i've drawn it i will show it to you [Music] okay that's pretty good you can see that and so then we're going to have this vector that is how long well it is arrow length but we're just going to call it l here and so this vector is l long and then you have to think that you are going through some angle and then you've got the tip of the vector here right the tip of the vector is at some x value and it is at some y value okay so remember you're going to go over some x you're going to go over some x just like before but it's not going to be the 2 or the 1 now you're going to go over some x and then you're going to go up some y over some x and up some y and then you end up where you end up at the point what x y well you can see if you have a little bitty angle you're going to be way over an x and up just a little bit or if you have an angle that's getting close to pi over 2 you're going to go over just a little bit x is going to be small and y is going to be large but there's a formula that allows you to calculate it as a function of what that angle is and what it is is is that this x value how far is it from here to here depends on the angle and that x value is equal to the length of the vector l times the cosine of the angle all right if you've never heard of cosine before if you've got a scientific calculator it's got the cosine button on it or you can go to a website and you can look up the cosine of an of an angle or in the python since we're using numpy numpy will calculate the numpy will calculate the cosine of an angle so how far over are you well you take l and you multiply by the cosine of the angle and then similarly with y y is equal to the length the length of the vector times the sine of the angle and that angle symbol is called theta okay theta t h e t a so if i want to put in pi over 4 here then i can use these two things to calculate the x and the y that will put my vector point right exactly there and so what is that point well that point is l cosine theta comma l sine theta and that will put me that l cosine theta comma l sine theta that will put me boom right at that point don't believe me let's give it a try so we will come back over here and let's add another parameter and let's call it theta and what is theta well we want theta to be pi divided by 4 right because that is pi pi over 2 45 degrees would be pi over 4. but now how do we get pi there is a function that will give you pi you could type in 3.141592654 or you could just do n p dot pi and n p dot pi will return to you 3.14 so anytime you need the variable pi you just put in np.pi and make sure that you have imported the numpy function does that make sense okay so now i have a theta now i'm going to create a pointer arrow a pointer arrow and that is going to be equal to the arrow and then i need to set up the axis now the axis is going to be equal to the vector all right but the x value is not going to be 1 because i want it at that pi over 4 position so what would the x value be what would the x value of that point be what would the x value of that point be l cosine theta so we will come over here and we will put in a row length right arrow length was our l times how do i get cosine well it is a numpy function so it's np dot cosine and what's your angle theta right i set the theta up over here so that's my x value oh you haven't seen that let me come back over here sorry about that okay so i'm going to create an arrow pnt arrow it is going to equal arrow and then i'm going to set the axis okay the vector is going to be arrow length times np dot cosine theta and then that's the x value then what is the y value what is the y value the y value is l sine theta right so we'll come back over here not forget to do that and say that is going to be that is going to be arrow l times np dot sine of theta okay and so that closes my sine function now i'm going to come on in i'm going to say color is equal to color.orange and then length i'm doing that after i point it so the length will be right is equal to arrow length and then shaft width is equal to arrow thickness like that now what should this do if i haven't made a mistake it should point it at the pi over four it should point it at the pi over four and then it should make it too long and the reason it's going to make it too long is i set the length after i gave it the vector command so x value is l cosine theta y value is l sine theta coming back over here x value is this is our length np cosine theta this is our length times np sine theta that is for y and what did i forget i forgot to put in a value for z so i got to put in comma 0. and why do i put in zero i'm spinning this way i'm not spinning this way so z is always going to be zero because i'm rotating in the x y plane let's just give this a shot and see what happens boom do you see that 45 degrees uh-huh look at that we got it right there at pi over four boom so what if we wanted it half again which would be half of 45 degrees which would be 22 and a half degrees well we would instead of pi over 4 we would make it pi over instead of pi over 4 we would make it pi over 8 right and so let's try that and that should be halfway to 45 degrees boom and you see as that thing is moving around it is always the right length what if we made it like pi over 24 and that would just be barely up a little bit and let's watch that okay boom okay now what you guys tell me if i just made this np dot pi over 2 where would that put it can you think through that let's try [Music] let's kill this come up here okay put it straight up right pi over 2 is straight up so man this is working perfectly okay this is working just like we expected it and we're doing good we're doing good to to use uh parameterized models okay now what do we want to do we want to animate this where that point arrow is actually rotating around in the x y plane rotating around in the x y plane how would we do that pause for a second and kind of think how you would do it and then i'll kind of show you how i'm going to do it okay we're going to come over here and we're going to create this but we're going to start it at 0 right so instead of starting it somewhere we're just going to start it at zero stop that that's really annoying there okay so we're going to start it at zero it's still going to be too long we're going to start it and now we want it to just sit and rotate well what would we do inside this while loop we're going to need a what we're going to need a for statement and i'm going to say 4 you know i'm just going to say my angle in what who's your friend np dot np dot lin space because that's the easiest way to create a for loop where do we want to start we want to start at an angle of what zero okay now if we're going to spin all the way around it's going to go to what all the way to 2 pi so how do i do that 2 times np dot pi and then how many steps do i want to go well we want it to go really smooth so let's make it go a thousand steps and that way it's not jumping right it'll be really smooth so let's go a thousand like that all right now we get rid of this past nonsense okay now what do we want to do well what am i changing to get this thing to spin i'm changing the axis i'm changing the axis of what my point arrow dot what do i want to change its axis and that's going to be equal to the axis it's always a vector and what is the x value what is the x value l cosine of theta right l cosine of theta so that is going to be arrow length with the upper case l like that arrow length times np dot cosine not of theta but the angle i'm stepping through is what this is my index my angle right if you set it to theta theta's not changing angle all right then [Music] so arrow length times np cosine of my angle got to be real careful with your commas now i've got to do the y value well that's arrow arrow length times np dot what who's your friend sine this time for the y and that's going to be my angle and then still i've got to do the z so the third parameter z is always going to be zero right and that's the only thing that i'm changing is point arrow dot axis now from what we learned earlier though when i change the pointing it's going to resize it to the wrong thing so immediately i need to fix that and say point arrow dot length is equal to arrow length just to fix the pointing messing up the length problem all right now let's see here we might need to go ahead and put a rate in here just because we are doing a v python in a loop and a lot of times it wants to see a rate so let's just put 50 i'm just guessing that might be way too fast or way too slow so let's see what happens here give that thing a try shazam do you see that pi over 2 coming up around the corner to pi going to 3 3 pi over 2 all the way back to 2 pi what's it going to do at 2 pi now in the while loop it goes back and it's starting again it's starting that for loop again so you see how we have animated that thing boom look at that and you learned a little trigonometry and you learned about radians today okay guys when you're doing orientations anything besides the very simplest of here or here or here or here you have to do your signs and your cosines okay you have to do your sines and cosines and i've shown you how to do that i've shown you that you have some angle and your x value is the length of your vector times the cosine of the angle the length of your vector times the cosine of the angle how high do you go that y value is l times sine of theta and then when you do that you get this work of art here this beautiful vector and you see how it's just going right in that x y plane it's not out in z at all and so that is just absolutely amazing you know i'm always kind of picky what i think i'm going to do is i would like to make uh i'm going to make that one a little thicker so where i have arrow thick thickness i'm going to also make a pnt thickness and i'm going to make that like twice as much like .04 just so it stands out a little bit more and then here where i created i would want to make it point thickness right there where i created it pnt thickness like that and that is where i created the point arrow all right let's try that it should be a little thicker and easier to see this time look at that i like that i really like that okay guys this is your homework assignment i want you to write a program that does what i just did but you're going to go around the x y axis when you get to here you're going to go around the x z axis like that okay and then you're going to go around the y z right so what am i going around right now in this illustration i'm going to go from x now watch me i'm going to go from x to y so i'm rotating in the x y plane after i make one full circle i want to start at x and go to z and rotate in the x z plane and then when i come back i want to rotate in the z y plane okay so you see you're going to take a trip around you're going to take a trip around going from the x y plane then you're going to make a trip around in the x z plane and then you're going to make a trip around in the y z plane that would be starting at green and going to blue like that i've given you everything that you need in order to do that i really have i've given you everything that you need in order to do that and so i want you to make three trips around transversing each one of the coordinate axis sets okay guys this is really kind of fun i hope you guys are enjoying this we're going to have some fun in the next lesson really try to do it you should be able to figure it out with what i've shown you you really should and it's very symmetric it's just you're going to kind of be you know the point arrow you're going to kind of be playing around with where this is and you're going to be playing around with where this is but i think if you play around with it you'll you know you'll be uh you'll you'll be able to figure it out okay guys i am having a lot of fun with this i love visual python i hope you guys are having fun uh be sure if you enjoyed the video to give me a thumbs up also if you haven't already subscribe to the channel when you subscribe make sure you ring that bell so you'll get notifications when my next videos are coming out and then share this with other people right because the world needs more coders and fewer people sitting around watching silly cat videos paul mcquarter from toptechboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 2,053
Rating: undefined out of 5
Keywords:
Id: ddWkgwBKNdA
Channel Id: undefined
Length: 38min 18sec (2298 seconds)
Published: Wed Sep 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.