Python 3D Graphics Tutorial 3: Designing 3D Models with 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 episode number three in our exciting new tutorial series where you're going to learn how to do 3d graphics and 3d animations in python or you're gonna die trying what i'm gonna need you to do is for yourself a nice strong cup of coffee that is straight up black coffee no sugar no sweeteners none needed that should go juice go get you some all right and i am also going to need you to call up your visual studio code and while you're calling up visual studio code 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 think about looking 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 of this shameless self-promotion let's talk about what we are going to do today we are going to start by looking at the solution to the homework problem that i gave you in uh in lesson number two and that was to take our simple animation program where we're creating a room with a marble in it and to stop thinking about length and width and height and start putting it in terms of the arbitrary vector x and then y and then z because x is always x y is always y and z is always z and it's just too hard to think in terms of length width and height because it means different things right so we've got to first of all just start thinking in terms of x and in terms of y and in terms of z and i kind of got you started last week in the first let's see here it was the first floor we instead of length width and height we said that the size of the floor would be what in x it's going to be 10 right in in y and y it's going to be 0.1 and then in z it's going to be 10 because it's like this and then in y it is it is 0.1 and so we were able to kind of take length width and height and we were able to create a size vector where the size is equal to the vector of 10 in x 0.1 in y and then 10 in z all right and what your assignment was was to adjust all of these other dimensions on the wall to the correct things and so for the ceiling i'm just going to copy what i'll do is i'm just going to copy and paste this and then we'll go in and make them all right in a minute so i'm just changing length to the size vector as we come down here right now i've got to go in and put the right numbers in so the ceiling is going to be just like the floor right the ceiling up here and x is going to be 10 and y is going to be 0.1 and in z it's going to be 10. and so the ceiling should be the same now the back wall the back wall in x the back wall in x is going to be 10 right the back wall and x is going to be 10 but the wall in y is going to be 10 so i need to put a 10 here on back wall okay it's going to be 10 and then in z the back wall in z is going to be 0.1 like that 0.1 all right now the left wall over here in x is going to be 0.1 the left wall is going to be 0.1 and then that left wall that left wall in y is going to be 10 and then in z it's going to be 10. are you seeing what i'm saying i hope so and then the right wall is going to be the same all right so now we're going to say 0.1 do you see now always x is x y is always y and z is always z so it's going to help us to think in three dimensions so now let's just run this thing and see if that is going to work we made a mistake what happened there something horrible happened to our right wall something horrible happened to our right wall i must had a typo in there somewhere so let's come back over here let's kill it oh i had point one and point one so the right wall is going to be 0.1 and i didn't change that to 10 and so that should fix that real quick and we'll come back over here and run it maybe i didn't kill it we'll come back over here and run it okay that looks like a nice room all right and now you see we're thinking in terms of x we're thinking in terms of y and we're thinking in terms of z and guys this is going to be like the most important lesson that you have out of this whole series because when we're thinking about three dimensional objects all of a sudden there is no way to do them with like let's see plus five minus five oh and then it's zero minus five because all these things are related so we have to stop putting constants here we have to parameterize it and so we've got to keep we've got to create parameters and then operate based on those parameters and so what is our first parameter going to be well really easy what you can see is the marble radius is going to be equal to 0.75 so anywhere where we use marble radius or the radius of the marble we need to replace that with our parameter and so this would be down here under marble radius it is m radius okay so i'm getting rid of all the constants out of here i'm getting rid of all the constants out of here now what i have to think of is i have to think of terms of the room okay the walls have a certain thickness so i'm going to call that wall thickness and what was the wall thickness the wall thickness was equal to 0.1 so i need to go in and i need to put in wall thickness for the 0.1 and so if we think about the floor the wall thickness is going to be in what direction it's going to be in the y direction so i need to take i'm just going to copy wall thickness i need to come down here in the floor in the y direction that is wall thickness and the ceiling the y direction the y direction is going to be wall thickness and so remember on this size vector it is x it is y and it is z so look at that now i have wall thickness well what else do i have i am going to have the width of the room the width of the room and the depth of the room so i'm going to have room width and let's just say that's 10. room width and then room depth and let's just make that 10 room depth is equal to 10 and then i'm going to have room height and let's just make that 10 okay so i have room width i have room depth and i have room height now i need to come in here and on the floor the x value is what that is the room width so i'm going to take room width here i'm going to take room width and then on the ceiling its x value is going to be room width so i got room width on the floor and then room width on the ceiling does that make sense okay now i've got x i've got y and i've got z all right it's x comma y comma z so what is z okay what what is z z is the room depth so i need to take room depth and i need to put it where my z is okay and then same thing on the ceiling the room depth is z so i'll take this and i'll say room depth and so room width is x wall thickness is y and then room depth is c man i hope you're understanding what i'm getting at here because we're going to turn this into parameters and then we can do anything we want by just simply changing these numbers up here and everything will work itself out because the room has been parameterized okay now i've got to think about the back wall okay the back wall the x value is still the room width and so i'm going to get room width here and then the back wall is going to be room width and then it is going to be the back wall is going to be room height in the y room height in the y like that and then the back wall the z right it's x it's y and it's c that back wall what is it c going to be that is going to be wall thickness right it is going to be wall thickness that one's going to be wall thickness so on the back wall the z value on there is wall thickness all right now let's think about the left wall let's think about the left wall what is the x value of the left wall what is the le what is the x value of the left wall it is wall thickness right it's wall thickness and it's also wall thickness on the right wall it's wall thickness on the right wall and don't just come in here and put random things because room depth and room height might not be the same later so we've got to keep track of which 10 is 10. so we're talking about the left wall the x is the wall thickness the y is the what it is the room height the y is the room height so we need to take room height and copy it and then we are going to make the y value remember it's x comma y comma z we're going to make the room the y value room height the y value is going to be room height and then same with the right wall the y value over here is room height now what is the z value that is the room depth for the left wall and the right wall the z value is the room depth and so now i need to get room depth and then i need to make that the z value and the z value all right now look at this we have gotten rid of all of our numbers over here and we're going to run it this should this should run okay this should run and then we'll come back okay we still have we still have a box that looks right but now instead of a bunch of co instead of a bunch of constants it is parameterized okay it has been parameterized guys man i can't tell you how important it is if you have to spend a week on this one lesson you have to understand this you have to do things in terms of parameters so you define your parameters up here which is defining the room i think you can see that this defines the room and now you have to use these down here that way if you want to change the room all you got to do is change a number here and everything will work out right now what we haven't done though is we kind of just put in positions like oh well this wall would need to be here this wall would need to be here we have to put the walls in terms of the parameters so where does the box where does the floor need to be and don't say -5 okay where does the floor need to be well i'm not going to move it left and right so that is still going to be zero i am not going to move it in and out so that z still needs to be zero i'm going to what i'm going to change y but instead of minus 5 what does that need to be what would the parameter be well it's associated with the room height but it is the room height divided by two okay so i wouldn't want to come to zero and go all the way to room height that would put it all the way down so it's half the height down so this floor how far down do we want it to go well we would want it to go room height divided by two okay and then how far up would we want it to go room height divided by two all right now when we think of the back wall where is the position of the back wall where you are changing what you're changing z how far do you want to change z that would be what the room depth divided by two so that would be the room depth divided by two okay now the left wall is going to be what what's this dimension this dimension is the room width so the left wall would be scooted over by the room width divided by two so this would be minus room width divided by two and then the right wall what are we doing with the right wall the right wall is moved over in x by how far the room width divided by two [Music] okay now we are going to run this thing if we remember to kill it while ago which we probably didn't okay let's try it boom the room looks correct well you could say well you just spent 20 minutes and we're right back to where we were before but here if we have done this right is where the magic happens so we're going to come back over here and we're going to kill it and so you should have all of this okay and just to make sure that you can see it i'm going to do a control minus so you can see it all at the same time and uh let's see i wish i could get rid of that oh i can make it where you can see it better here okay there it is there you can see the whole thing a little better in fact let's see if i can do that what i want to do is just give you a complete view of it all right so there you can see all the code all at the same time right and so if somewhere along the way you miss something you can pause it and you can look at this really carefully but i've set up the parameters wall thickness marble radius and then room width room depth and room height right and then everything is in terms of that but this is where the magic happens what if we say the room is 15 wide the room is 15 wide and it is only 5 deep okay and then it is still 10 high is it going to be able to go in and configure that room but because we put the position of the walls relative to the width and the position of the ceiling and floor relative to the height and the position of the back wall relative to the depth of the room this should be fully parametrized and it should work if if i am thinking right so let's go ahead here and let's run this thing [Music] boom do you see that look at that look at that that room is now 15 wide and it is only five deep and look the marble still okay now look the marble didn't go to the wall right so what is wrong the marble is not going to the wall so what is wrong let's go in and let's look at this and see man we are so close but do you understand about parameters now we got the size of the room in the position the walls right because we made a parameterized model and so let me come back over here and let's look and see why the one thing that was broken was it didn't go all the way to the edge well why did it not go to the edge because down here did i use a parameter no what did i do i put in a constant so what when x position the position of the ball gets where not to five but what room width divided by two okay and or on the left side it would be minus what minus room width divided by two minus room width divided by two now that should work because x position is a parameter it starts in the center of the room and then delta x is how far it moves every time those things are already parameterized and so i really don't have anything that is not a parameter here so let's try this again okay and it is not moving why is it not moving what did i break i broke something okay let's let's go back and let's see what i broke in doing that what did i break room with room ah okay it is room with with the uppercase w and that is probably what caused the problem room width all right let me get this up a little bit where you can see it better okay so i have x position is greater than room width divided by 2 and x position is less than minus room width divided by 2. and so that looks pretty good let's kill it and let's come up here and run this boom look at that do you guys see that it went right over to the edge and then it bounced i got something crazy going here okay look at that boom that is working so we should be able to go in now and we should just be able to change that room dimension to whatever we want and so we can kill this and let's just say that i am going to go uh let's say i'm going to go the the height is 20 okay and let's say the depth is let's say the depth is 50 okay and then let's say the height is five so let's get kind of a completely different room there and run that and here it comes look at that okay do you see you have like a radically different room there and it still works properly because we have parameterized the model okay guys i just uh i hope i have convinced you that the only way that we're going to move forward in 3d animations and in 3d graphics is we have to base things on parameters where we set the parameters up here and also you've got to like think about the parameters like i didn't make the position of the floor just be like floor position no the position of the floor has to be at the edge of the walls and so i had to put the floor position in terms of the height of the room otherwise i could you know it wouldn't just all come out a nice box with the mat with the ends matching all right guys let's come back to a more normal let's come back to a more normal room size and so let's say that the room width is 10 let's say the room depth is 5 and then let's say the room width is 10 the room depth is 5 and now the room height let's put it back at 10. all right now i want you to watch this and tell me the one thing that you don't like okay what is the one thing that you don't like what we don't like is that the ball bounces after it's gone through the wall so if you think about it what is the condition that we bounce when the center of the ball is at the center of the wall we bounce but what would you really want you would want it when the edge of the ball when the edge of the ball hits the edge of the wall when the edge of the ball hits the edge of the wall that's when you would want to bounce and so when you come down here what you can see is when i say x position well x position is what that's the center of the ball is is greater than room width divided by 2. well where is room width divided by 2 that is the center of the wall so that's not right i want to do it when the edge of the ball hits the edge of the wall and so what your homework for next week is is to adjust this model so that it more perfectly bounces when the edge of the ball hits the edge of the wall and that's your homework for next week and then i'll come in next week and i'll show you how to do that and then we'll kind of move forward with our animation and learn some some new stuff so guys this is the most important lesson this is the most important lesson of the whole series okay this is the most important lesson of the whole series and so what i need you to do is stop and if you don't understand this you need to go back and do it where you understand it in fact you really should start with a blank sheet you should start with a blank sheet and you should write this program again without watching me because you have to understand this concept of parameterizing your model okay and so really you need to go in and you need to develop this by yourself and then after you can get it to this point by yourself then you need to adjust this condition where it is more perfectly balancing okay guys i hope i didn't lose you here but man this was just something that we had to do we had to deal with because if we have just these fives and tens everywhere it becomes impossible then to adjust your model you have to do it in terms of parameters okay leave me a comment down below if this makes sense if you guys like this lesson give me a thumbs up if you haven't subscribed to the channel already make sure that you subscribe and share this on social media because we need more people that know how to code and less people sitting around watching silly cat videos paul mcquarter from toptechboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 6,793
Rating: undefined out of 5
Keywords:
Id: JqnWhWWIehM
Channel Id: undefined
Length: 26min 2sec (1562 seconds)
Published: Wed Jul 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.