Python 3D Graphics Tutorial 2: Animating 3D Objects in Vpython

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 two in our incredible new tutorial series where you're learning how to do 3d graphics and 3d animations using python what i'm going to need you to do is put yourself a nice strong cup of black coffee that is straight up coffee no sugar no sweeteners none needed this my friend is go juice and it'll let you take your programming to that next level and you're going to need it for this lesson okay what we're going to look at is we're going to look at the solution to the homework assignment which i gave you in lesson number one and then we're going to begin to learn some new stuff about how to begin to animate our 3d uh our 3d graphics so the first thing i need to know is i need to know were you guys able to do the homework if you were able to do the homework leave a comment in the description down below saying i am legend or let me know that you folded up like a cheap lawn chair okay how many of you were able to do it and how many of you folded up like a cheap lawn chair all right now the thing that you got to see is it's really important for you to try to do these homework assignments because if you just watch me do it and do it along with me it looks easy when i'm doing it and you understand what i'm saying and you're understanding what i'm doing just but just being able to follow me doesn't mean that you can write code on your own and that's what our goal is is to get you where you can do this without me sitting there and so i want you to really try hard to do the homework and then if you just absolutely can't do it then i'll come back and show you how to do it but at least if you've struggled and tried and failed then when you watch me do it then you'll really learn because you'll okay i understand i tried this i was thinking about it wrong you'll actually learn and that is what our goal is is to have you learn so i will need you to fire up your most excellent visual studio code and as always as you're firing that up 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 move on over and let's jump in and see if we can figure out how to get this how to get this homework done and so what i'm going to do is i'm going to start by moving out of your way and then after moving out of your way we are going to come over here and open up our explorer we are working in v python we will come into our v python folder and create a new file and this will be room sim room simulation which was your homework dot py the dot p y is kind of important and boom we get a fresh new python program just waiting to be written okay what we are going to do is we are going to start by saying from the python we're going to import our library from bbv python let me get this explorer out of your way there from the python we want to import everything which would be star and then from time because you never know we might need that time command import star import all okay now what are we going to do what was the assignment the assignment was to make a room that the walls are 10 foot high and 10 foot wide and to have a back wall a ceiling a floor a right wall and a left wall okay and then to put a marble a red marble in the center of the room so let's try that and then let's see if you guys were successful so i'm going to start by doing the floor and the floor is equal to a box and that box is going to be color equal color dot white okay and i think i will come over here and then we will give it a position and so the position is equal to the vector and where do we put that where do we put that floor remember it is x comma y comma z and y is up and down and so we want to be zero and then we want to be minus five and then we want to be zero and so that's going to put that thing down and then let's go ahead and let's give it a pos let's give it a size and so the link link the the length is equal to the length is equal to remember length goes in the x direction and so that in you know the floor is going to be 10 in the x direction and so length is going to be equal to 10 and then what are we going to have we are going to have the width and the width if i remember right was going in the z direction if i remember right we'll see if this is not right but the width was in the z direction i do believe the width should be 10 and then if i remember right the height was in the z dr the height hmm length width height i think the height is in the y so that's going to be 10 and then i do believe the width was in the z so this is going to be for the floor the length is 10 which is in the x and then the width is 10 which is in the z and then the height is y so it's the height should be 0.1 and this is kind of the hard thing is remembering where we are in three dimensions okay and so we've got length width height and color and then we don't want the thing to crash so we got to give it something to do so we say wow while true when it's true true true is always true and then what we're going to do is pass and guys just when i've been away from v python for a few hours doing other things i have sometimes forget linked with height but once we get this first one working the other one should work so let's go ahead and run this and see if we're thinking about dimensions correctly so we run this thing and then it pops up boom that looks right so it is length it is width and then it is height the height is in the y direction so that looks very good we're going to come on back over to here and now we are going to kill the program and now if we do floor what would be really really easy without even thinking we can do ceiling so i'm just going to copy and play paste floor and then this is going to be ceiling and instead of minus five it is going to be plus five and so now let's run that real quick and here it comes boom we have a ceiling and we have a floor man we are on a roll here did you guys have find this as easy as what i'm finding in here okay so we are going to come back to visual studio and now we will make another wall and this next wall is going to be let's go ahead and do the back so instead of this paste i'm going to say back wall back wall and so here the back wall is going to be this way that is z right and so it should go zero zero and now it should be i think minus 5 because remember if this is x and this is y you do the right hand rule from x to y and your thumb you go from x to y you curl x to y your thumb points in the positive z direction so positive z is this way negative z is that way so that back wall should be at minus five i do believe now remember we're not going to do a front wall we're just going to pretend the front wall is there because we want to be able to see in our in our box but now what we're going to need to do is the left wall we're going to need to do the left wall and that is going to be -5 in the x i do believe i guess i'll just paste here ctrl v and then i will come back over here okay and this is going to be instead of floor this is going to be left wall and then that is going to be in the x remember it's x comma y comma z and so it would be minus 5 and 0 should make us a left wall and we're going to run that i didn't kill the last one sorry now we're going to run it oh that oh yeah we gotta we gotta adjust we got we got some crazy stuff there going on and i know exactly what we did i got in a little bit of a hurry so we will come back over here and the top and the bottom are right and everything else is messed up so the back wall yeah it needs to be negative five that's right but then we've got to remember over here that length is this way yes length needs to be 10 and width is this way now width is going to be 0.1 on the back wall with this point 1 and the height is going to be 10 and that should fix that should now i fixed the wrong one the back wall we'll we'll fix left wall in a minute but the back wall needs to be the width is in the z direction and so the width needs to be 0.1 and the height needs to be 10. okay does that make sense the length of the back wall is 10 the width of the back wall is the z direction so that's 0.1 and the height is the y direction so that needs to be 10. so that should fix the back wall let's go ahead and run it and see if the back wall looks right now here we go yeah the back wall now we've got to fix the left wall so what you got to be doing is you got to remember which direction is linked which direction is width and which direction is height x is length height is y and width is z and you got to remember that so we got to go fix that left wall is in the right position it's just the wrong size so we will come back over here all right so now the position of left wall is right but we got to think about that left wall the length is this direction and for that left wall we want that to be 0.1 so this one should be 0.1 on that left wall and then the width okay which is the z direction that needs to be 10 that needs to be 10 and then the height which is which is why the height needs to be 10. all right so that length and then width and height and so i think this should fix it if i'm thinking about this right boom all right look at that we almost got our room built okay we will come back over here and we will kill the program and now what do we need we need the right wall so i'm going to copy left wall because it should be almost the same right and then i'm going to paste it and then this left wall is going to become right wall and then it instead of being at negative 5 is going to be at positive 5 for the right wall so positive 5. but then it should be the same size and so the height of the right wall the height of the right wall should be 10 all right the height of the right wall should be 10 and then the width of the right wall should be 10 and the length which is the x should be 0.1 and so i think this is going to make our little room just dandy here boom look at that you see that we have our little room now that was almost the assignment but what else were we supposed to do what else we were supposed to do was to come in here and we were supposed to put a marble in the center of the room and so how would we do that we are going to say my marble is equal to what is a marble it is a sphere and then what do i want i want it at zero zero zero in the middle of the room so i don't need to put a position but i need do need to say a radius what do you think would be a good size of a marble 0.75 0.75 and then i also need to give it a color is equal to color dot red like that okay let's do this and see what happens we're going to run this thing if i kill the old one and now run it boom we have a marble in the middle of the room okay do you see that were you guys able to do this let me know if you were able to do it all right now we're going to do something new how could we animate this how could we animate it all right how could we animate it well what we could do is we could come back to our program and always remember to kill the last last one i want to move that ball back and forth well how far do i want to move it well i want to move it by delta x each time through the loop below i want to change the position by a little bit how much am i going to change it by delta x delta x is going to be equal to 0.1 let's say and then i'm going to have an x position and x position is going to start at zero so the center of the marble is going to start at zero now i'm actually going to do something in the while loop so i'm going to change the position of the marble in the while loop well if you're going to do that i need to tell it how fast to run and you don't want to do this with a delay you want to do it with a python a v python command which is rate and we'll put a rate of kind of like let's say 10 that'll be pretty slow and then uh that you just always need to put a rate statement in a loop with v python you don't want to do it with delays and so if that's too slow we can make rate bigger if that's too fast we can make make rate smaller but now what do we want to say well x position is equal to x position plus what plus delta x so each time through it's going to make delta x a little bit it's going to make x position a little bit bigger a little bit bigger a little bit bigger but then what do we need to do we need to apply that to marble so marble dot position okay the marble dot position is going to be equal to the vector and what do we want well in x we want it to be at x position and then y we're going to leave it where it is and then in z we're going to leave it where it is and so we are going to update the marble position every time through this loop we're going to move it a little bit nudge it over by delta x and then apply that to marble and so let's run this thing and see what happens here and it is not moving why is that thing not moving what did i do wrong what am i not thinking of here okay so we say while true rate is 10 delta x all right marble dot position is equal to x position x position is equal ah look at that s position that was a bad one x position that scared me guys that scared me i thought why is my marble not moving i'm live on youtube and my marble's not moving all right let's try this did you guys catch that when i did that giddy up uh oh what do we have here we have a jailbreak did you see that thing it went right through that wall and where is it going it's on its way out to infinity and so that ball just keeps moving further and further and further from the box but what is the good news the good news is we did get it to move but what do we need to do here well what we got to think is we've got to think if x position is greater than what if if x position is greater than i think if x position is greater than 5 then what do we need to do well if x position is greater than 5 you're getting out to the wall you're getting past the wall you need to make delta x equal to delta x times minus 1. so we change the delta x if we hit the wall and so then it should come back the other way once it hits the wall so we're going to come in here and we're going to try that all right let's watch it what's going to happen boom it changes direction uh-huh look at that it changed direction and then it comes to the other wall and we get another what we get another jailbreak so let's come over here and see if we can understand why we are jailbreaking in the other direction well when we're going this way when we're going to the left x is getting more and more and more negative okay so like it's never going to be greater than 5 because it's minus 1 minus 2 minus 3. so what do we need to do here we need to say or if x position is less than negative 5. and so negative 6 is less than negative 5. so if it goes negative 5 and then it's going to go to negative 6 then we got to change the direction again so we want to change the direction if either one of these conditions is true if it's gotten to five in the positive direction and trying to go fast uh past it we reverse the direction and then if it starts getting to negative five and goes more negative then we reverse the direction again and so now i think this should work okay we're coming up to the wall and we hit it and we bounce and then we're coming over to the net to the other wall and we are going to hit it and we are going to bounce and so boom okay now what is the one thing that we don't like we are measuring the position of the wall is measured relative to the center of the wall and the position of the ball is measured relative to the center of the ball and so what we have is we change direction when the center of the ball or the marble matches the center of the wall and the problem with that is is that the marble is halfway through the wall before it bounces and so we've got to kind of sharpen our pencil on that but this is a good time to introduce a new concept because you can see that things are going to become very untenable very very quickly and so what we are going to have to do is and this is the problem that you see in one case we've got length as point 1 and then in another case we have width is 0.1 because that back wall needs to be narrowed this way and then this wall needs to be narrowed this way so as we move between the different walls it's like length and width and height start getting confused and so what we need to do is we need to stop using all of these constants and we need to parameterize our model and so that is what we are going to do for next week but i will show you kind of like the first step first of all this length width and height is too confusing because what is happening is it means different things depending on which wall we're going at so one is like thick and one is thin and so it just it gets too hard to think about so what i want you to see is there is an easier way to think about that and that is you can think about it with the size command and so what you will do is you will give it a size and then it's always the same size as x comma y comma z and so what i would do is my x would be 10 my and i'm this is let's look at what we are doing here we are doing the floor and so my x on the floor very clearly is 10 it's x comma y comma z what is my y my y is 0.1 okay and then my z is what it's 10 and you see that is going to get rid of this confusion of keeping track of what we mean by length width and height and so we're going to go to size because always size is x size is y and size is z so the first step into in parameterizing this model is to change from these length widths and heights into the size command all right so let's just make sure that this works and so we're going to come in here and we're going to run this and just make sure i think that was the floor what was that what did we do oh i know what i'm again going back to an earlier version of python size equals vector like that size is equal to vector 10 comma 0.1 comma 10. now let's run that and it comes up and boom it is working so this is moving towards a parameterized model and so what your homework is for next week is to go in and replace all of these length widths and heights into the size vector and then just get your room working and your ball bouncing using the size vector and then next week what we're going to do in lesson number three we're just going to parameterize this whole model because what you're going to see is it is just too confusing to keep track of which is length which is width which is height and we're going to write this in terms of parameters and then we can just change any parameter and it will always work okay we're going to change any parameter and it will always work okay so for for next week though just take out all these length widths and heights and put in sizes and then we'll take it to the next step next week okay guys i think we have made a lot of progress here in two lessons you already have an animated scene and i think that is pretty cool i think that is pretty cool and so next week what we're going to do is we're going to fully parameterize this model if you guys enjoy these lessons be sure to give us a thumbs up if you've not subscribed already make sure you hit that subscribe button and ring the bell so you'll get notifications when my new lessons are coming out and then share this with other people because the world needs more people who know how to code and less people that sit around and watch silly cat videos paul mcquarter from toptechboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 8,765
Rating: undefined out of 5
Keywords:
Id: 03ysGfNO-G8
Channel Id: undefined
Length: 25min 54sec (1554 seconds)
Published: Wed Jul 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.