Python 3D Graphics Tutorial 4: Understanding 3D Graphic Parameters

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mccorder with toptechboy.com and we're here today with lesson number four 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 my friend is your goat juice go get you some okay so i'm also 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 encouragement and your support that keeps this great content coming you guys that are not helping out yet look down in the description there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough of this shameless self-promotion let's jump in and talk about what we are going to learn today we are going to look at the homework solution to the homework problem that i gave you in lesson number three and what that homework assignment was was to adjust your parametric model for the room with the little marble bouncing around and get it where it properly bounces off the edge of the wall now what i need to say is that lesson number three is going to be the most important thing that you learn in this whole series of lessons if you are going to have any hope at all of doing complicated 3d animations and 3d graphics you have to develop parameterized models and that's where you don't just go in and say well this is at 5 and this is at 7 and this is one thick and then you go in and you try to change something and it's hopelessly complicated if you change the dimension of one thing it puts the position of something else in the wrong place and so you have to look at something like a room and you have to build a parameterized model and then if you change any part of it the other parts adjust so that you still have a room where the wall and the ceiling meet in the corner and so if you don't understand that last lesson go back and take it again take it two or three times it's just like you've got to understand it and it's not just for animations when you go in and start doing to trying to do 3d design work maybe for a 3d printer or you start trying to use autocad or fusion 360 or any of those cad packages you have to do parametric design parametric models and so that's what last week's lesson was about and you've really got to understand that and spend some time on it before you move on if you do not fully understand it if you do not fully understand it spend some time on that okay what we are going to do is we are going to start where we left off last week which which was where we had a marble mount bouncing back and forth in the box i want to make sure you guys can can start exactly in the same place that i started and so what you can do is to make sure we're starting in the same place you can go to the most excellent www www.toptechboy.com and you can search in the friendly little search bar on python 3d graphic lesson three you're parameterizing your 3d model you'll come down here and you can click here and then right mouse click and copy and you can get that code and then you can come back to your most excellent visual studio code and then we are operating in uh let me let me do a little bit of maintenance here okay we are let me make sure you can see this well we are operating in the v python folder and we are going to create a new folder which is i mean not a new folder a new file corrected correctedparameter.py so this is where we're going to correct our parameter model and then we're going to click enter and boom we have a fresh new python program just waiting to be written yes sir okay so we're going to come here and we're going to start where we left off last week so we're going to paste that old code in here and then you know guys even if you wrote it yourself you might have kind of like named a few variables different than me and so on this one let's really start at the same starting point and so let's come up here and run this thing and remind ourselves what it does okay so what do we do we like it that we have a little marble bouncing in a room okay the room has everything matching up properly it's properly dimensioned and then if we come in and look at this what do we not like we don't like that the way that it's bouncing okay because when we wrote this first simple-minded program yes we did parameters and we did it exactly right that's the way you need to do it you need to get all your parameters in place but then when we get to that actual line of code where we are bouncing off of a wall we just say the position of the marble is like exposition and that is the center of the marble and then when we're talking about the position of the wall when we're talking about the position of the wall it is the center of the wall okay and so what was our condition for bounce when the center of the the marble got to the center of the wall when the center of the marble got to the center of the wall i'm trying to make sure you can see it that's when the bounce occurred but in fact what do you want you want to bounce when the edge of the marble hits the edge of the wall so our parameter model is correct but we just need to change the condition at which we bounce and if we're going to do that guys what i want to really show you is we are to the point in our programming where this is complicated enough you need to get it to work on paper before you try to get it to work in code because if you get it drawn up in paper then it's really easy to code if you just jump in and start coding you're never going to understand what you're doing and so i am going to try to kind of draw this up for you and so what i am going to do i'm going to try to get a little snazzy here and see if i am a little better on this and so this is going to be let's see if i can make it around yeah okay so that is going to be my marble and let's say that that is the center of the marble right there and then i'm going to draw two walls and so i'm going to come over here and let's make it like that okay and then let's come over here and draw another wall okay and we will draw it like this i think i did pretty good there actually even though i'm not really an expert even though i'm not an expert on the sketch pad i think that is pretty good okay so what we need to keep track of here and i need to make sure that i go back to the pin okay i think this should be the pin we are tracking this right here and what is that that was x position okay and then over here this was kind of the center of the wall and that's what we were tracking was on the center of the wall but what i really want is i really want to know this and i want to know this so right now i'm comparing this to this but what i really want to compare and i think i am going to do a little cursor management here for you and i think that would make you happy because that uh that color is not going to show up well let's see if i kind of do something more like that okay i think you can see that better with that background so you can track my cursor a little bit better okay so let's call this let's call this one okay and then let's call this two all right and so we're gonna what is the position of one and what is the position of two just so i have time i have room to write it and that is starting to annoy me now even and so let's see if i can change that a little bit we're just kind of winging this thing on our as we go here i will make it more faint what is that that's what i want to want to make it more faint okay i think that is less annoying but i think that you can see it all right so now i need to call that point one something okay so i'm going to call it uh i'm going to call it the x because it is in the x direction and then it is the right x right marble edge and i don't want to write out x right marble edge so in the x direction it is the right side marble edge well because we might do y later and then we might also have x left mar marble edge and then we might do something else so i think that makes sense so what is the x right marble edge well if this is x position that is going to be x position but then it is going to be plus what half the diameter half the diameter is the radius and what we got to recognize is that's already a parameter okay that is m radius and so this is going to be x position plus m radius okay and notice that we are not going to have to divide radius by 2 because the radius is just the distance from there to there so that is good now now for number two what is number two that is the edge of the wall and so what should we call that that is right wall edge okay right wall edge and that's the only one that we're going to do there's only one right wall and there's only one edge and so that's going to be right wall edge now what is that going to be well the distance between here and here the distance between there and there is what is our parameter that would be room width so the room width is the distance between the centers and so the distance between there and there is room width okay and let me make sure i'm using the same parameters it is room width and this was a small our room width okay so then the distance from here the distance from here to to here would be room width divided by 2. now let's think about that that is room width the whole width and then if i go from the center if i go from the center over to here that would be the room width divided by two and so what is this position this position is room and i'm going to get further out of your way that position there is room width divided by 2. then what is this position here this is room width divided by 2 in the negative direction okay and so now we know where those positions are okay but i don't want this position i want this position okay and that's position two i want that position well what would that be that would be i'm gonna have to abbreviate room width divided by two okay which is that minus wall thickness wall thickness divided by 2. and i'm sorry i'm not using the actual variable names here but i just have to kind of abbreviate so i'm going to have the room width divided by 2 is going to put me out here and then i'm going to subtract the wall thickness divided by 2 because this is wall thickness here so i've got to subtract half that okay so that should be good now i'm going to need position three and then i'm also going to need position four so this is going to be position three and this is going to be position four and i'm going to take a second to erase a few of these things so that i have room here okay i want to make sure that i'm leaving room so this is position three and position three is just going to be the x value of the and that's not right x value of the left marble edge x left marble edge is going to be x position but this time it's what it's minus m radius okay so this position is x position minus the radius so now i know where 3 is and now what is 4 going to be 4 is going to be the left wall edge and it is going to be equal to it's going to be equal to minus the room width divided by 2 minus room width over 2. so that is going to be minus room width over 2 would put us right here but then i need plus what to get back over here i need plus wall thickness divided by 2. does that make sense guys think about this and make sure that you understand it because it's pretty clear when i draw it i don't want the center of the marble to bounce when it's in the center of the wall i want the marble to bounce when its right edge is against the left edge of the right wall and when this when this position 1 equals position 2 is when i want to bounce and when position 3 equals position 4 is when i want to bounce and i think i have labeled these pretty pretty clearly right i think i have labeled these pretty clearly from here to here is the room width okay this from here to here is half the room width okay and so i go half the room width in the negative direction and then i add half the wall thickness and i get there to position four guys i am showing you how to think like an engineer and what are you learning you're learning you have to do things on paper before you do them in code now we're going to be switching back and forth between these views to kind of make this thing work all right so now what you got to see is i am not going to change my parameters because my parameters are defined and they are correct and they work what i have to do is i've got to come down here and change my conditions i've got to change the conditions under which i bounce okay now x position is still going to be x position because i'm still going to be thinking about where the center of the ball is right what's the easiest way to keep track of the ball the center of the ball but it is i've got to define these new i have to define some new parameters and so like what is one new parameter well i need this x direction the right marble edge x right marble edge i need to define that okay so x right marble edge is going to be equal to what x position plus and then what was it it was the m radius m radius this then x r m e now unfortunately you see how that lit up it seems like that it is a reserved word so i need to kind of do x right marble edge like that because it was a reserved word x right marble edge and if i have x right marble edge i need the x left marble edge and this should have been x position here this should have been x position and now the x left marble edge is going to be what x position minus m radius right and so let's go back and look at that so this x left marble edge is the x position minus the radius okay so that's pretty good so i have those two positions now i need to get those two wall edges in my code right and so let's see here we're gonna come back over here and that would be let's go with the let's go with the right wall edge that's number two here right wall edge well that's the room width divided by two minus the wall thickness divided by two so room width divided by two room width divided by two and so what was that up here room width so we're going to come down here room width i'll go ahead room width room width divided by 2 and then it was minus the wall thickness divided by two and so that was wall thickness divided by two right and now what were we going to call that we were going to call that we were going to call that right wall edge rwe right wall edge so the right wall edge is equal to and that looks like a reserved word to right wall edge is equal to that and then we're also going to then have the left wall edge and what was left wall edge that was this position 4 here and that is in the negative direction so it's minus the room width divided by 2 would put you here and then plus wall thickness divided by 2. so we're going to have minus room width divided by two and then we're going to have plus wall thickness divided by two okay now we've got these new parameters we haven't changed x position we're still keeping track of x position okay as far as the marble moving around but now we're also keeping track of the position of the edge of the wall and the position of the left and right edge of the marble so now what i'm going to say is now i am not going to think like before it was when the marble gets over to that right wall and it's greater the center of the marble is greater than the center of the right wall no what we're going to do now is we want that right edge so x marble x what was that x r m e i forgot what the r is x uh o x right marble edge so when the right of the marble edge is greater than what well it's the position of the right wall edge and we're going to say greater than or equal to so if the right edge of the marble is greater than or equal to the right wall edge we want to change direction and then also on the other side we would be looking at the x left marble edge and if that gets less than if that gets less than the left the left wall edge we want to change direction and notice that i didn't make left wall edge negative because it's already made negative up here and so go back and look at the picture if it doesn't make sense okay so now this is kind of interesting uh i think that probably i would bet in here somewhere i probably made a typo but let's go ahead and let's try to run this suspense build suspense builds boom did you see that all right now is it going to work on the other side let's look let's look boom who's your huckleberry i'm your huckleberry i've shown you how to do parametrized models and now everything's working and this wasn't that hard number one you start with a parameterized model number two then when you have quirky little things like the ball going into the wall you make some new parameters based on those other parameters you see i didn't define a completely new parameter i just made that edge of the marble reference to the center of the marble so i haven't changed any dimensions i've just added new positions within the parameter set that i had look at that now here's going to be the full test i might have made myself your huckleberry a little bit too soon because as coders we really need to do a better job of really checking for a lot of different conditions i think one way we could really check this is what if we make the wall thickness one and let's say we make this a little bigger like 15 and we make this a little bigger like 15 and then let's say we i want to do these different though so that we really are checking it out and then let's say the room height is eight and then we're 15 by 12 and then let's make the marble two so you see we're really changing things up and so this is going to be a pretty big marble and pretty thick walls but let's see if it still works right well you know by really coming up with these weird things we're going to see if our parameters are really set up all right hold your breath wow okay let's look over here okay i couldn't quite see exactly there the room is certainly looking good now this is the moment of truth shazam did you see that did you see that let's look at that boom okay and you got to look really really carefully and sometimes it's kind of hard to tell exactly no that was perfect shazam did you see that wow now is this pretty cool or what you guys leave me some comments down below are you grasping what i'm telling you are you grasping the importance of having a good parametrized model so what have you learned the first thing that you learned is in the last lesson you define things that really make sense like you've got a radius of a marble that physically makes sense then you have physical walls they have some wall thickness you have a width of the room you have a depth of the room and you have a height of the room okay now notice i did not make a parameter of the position of the wall why did i not make a parameter of the position of the wall because the position of the wall needs to be at the edge of the ceiling okay and if i made a separate parameter for the position of the wall i could change the dimension of the ceiling let's say i went from here to here and what would happen the wall would be here and when i change the position or the wall would be here and when i change the size of the ceiling they don't match anymore so when we did this parameterized model the position of the wall is at the edge of the ceiling so when you change the ceiling width it changes the wall position so you see you've got to understand which parameters depend on other parameters because if i'd made independent parameters for the positions of the walls and the positions of the ceilings then when i change one of the other parameters the room breaks but you see i never made the position of the walls or the ceilings a parameter they were based on the position was based on the height of the room right you see the position of the floor is based on the height of the room and that way when i change the height of the room my floor is in the right place guys go back and watch the last lesson in this lesson again so at this point we have a good parameterized model and then what we did was we said okay it's all good all the parameters of the model are good but what we need to do is we need to not just keep track of the x position the center of the ball but the edge position of the center of the ball okay that was not a new parameter that was just the old parameter x position plus the old parameter radius of the ball okay and then we defined this not as a new parameter but in terms of the other parameters which was the width of the room divided by 2 minus half the thickness of the wall so you see the new parameters are based on the old ones and you've got to not introduce more parameters where you end up breaking things i hope this makes sense man this was kind of fun i actually love this stuff and once you kind of start understanding it it's fun and if you can do this then you can actually go in and start doing 3d modeling in something like fusion 360 or autocad and you can start building models that then you could go 3d print or something like that so i am really really hoping this that this made sense for you guys okay so i'm going to give you a homework for next week okay you see this this ball bouncing back and forth what your homework for next week is is to have it moving in three different dimensions and so it's not just going back and forth in x it's going back and forth and y and it's going back and forth in z all at the same time okay so each time through the loop you're going to have a new x position a new y position and a new z position and so what you got to see is it's not just going like this or like this or like this each step it is going to go over a little bit in x over a little bit in y and over a little bit in z and then if it hits the ceiling it's got to bounce if it hits the back wall it's got to bounce if it hits the floor it's got to bounce if it hits the left wall it's got to bounce if it hits the right wall it has to bounce okay now imagine you're going to have to have an imaginary front wall even though you can't see it you're going to have an imaginary front wall so when it comes out to the front here you don't want it to jailbreak you want it to bounce back in but we're not physically putting the wall there so that we can watch the ball okay does this make sense you guys should be able to figure this out so you guys leave me a note down below and say i am legend if you're able to get it done or i fold it up like a cheap lawn chair like a cheap walmart lawn chair okay but guys really uh some of this stuff it's so simple when you watch me do it but it's kind of hard to see it the first time and if you just watch me do it you're going to think oh that's easy i understand it where you don't appreciate what it takes to figure this stuff out so before you see me do it you need to spend at least an hour or so in the next week just banging your head against the wall trying to get it to work okay and then you guys even like man take a screenshot of your thing working in you know post a little youtube video and put put the little link to your video over in the description down below so other people can see that you're really getting this stuff done okay man i'm having a lot of fun with this and what i'm going to do now is i am going to go work on next week's lesson and i hope you guys will go work on it too and a little bit about where we're going on this we're going to kind of learn more and more about how to do animations and simulations in 3d like we're doing here and then what we're going to do at some point is we're going to tie this up to arduino and what things do on the screen depends on what's happening with your arduino and you know sensors and things like that making things happen or also visual python making things happen in the arduino so we're going to start linking up we're going to start we're going to start linking our 3d models to the real world through arduino and that is going to be pretty darn cool okay guys i hope you're having as much fun with this as i am if you like the video give me a thumbs up that really helps me with the old youtube algorithms and then also if you haven't subscribed to the channel already make sure to hit that subscribe button ring the bell so you'll get notifications when my future lessons come out and then think about sharing this on social media because we need more people in the world world that know how to code and are sitting doing something useful like coding and fewer people sitting around watching silly cat videos this is paul mccorder from toptechboy.com i will talk to you guys later ah
Info
Channel: Paul McWhorter
Views: 3,970
Rating: undefined out of 5
Keywords:
Id: zexjGsPVduY
Channel Id: undefined
Length: 32min 16sec (1936 seconds)
Published: Wed Aug 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.