Python 3D Graphics Tutorial 8: NonBlocking Dual Animations in Vpython

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter with toptechboy.com and we're here today with lesson number eight in our incredible new tutorial series where you're going to learn how to do 3d graphics in 3d 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 i am 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 support and your encouragement 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 jump in and talk about what we are going to do today and what we are going to do is look at the solution to the homework assignment which i gave you in lesson number seven and that was to create two animations like two thermometer animations that are both going at the same time but the second one going twice as fast as the first one and not like this and then like this but then both running at the same time leave a comment down below how many of you were successful and how many of you folded up like a cheap walmart lawn chair leave me a comment down below let me know how many of you guys were able to figure this out even better if you guys are able to do a screen capture take a screen capture of your animation upload it to youtube and then leave a link down in the comments down below so we can kind of see if anybody came up with a good solution and then maybe a few comments about how you got it done because it's always exciting to see what other people are doing i'm going like this because it's actually three degrees inside i mean it's three degrees outside we're in the midst of a polar vortex here in west texas and inside my room it's about 40 degrees well you might say you idiot put a coat on no this orange shirt is my trademark and so i will not cover it up with a coat even though i might freeze but i might be wiping my nose a little bit more than normal and i might be clearing my throat a little bit more than normal because it is absolutely freezing in this room okay but i digress let me get out of your way and let's see if i can jump in here and show you my solution to these uh to these uh two animations now probably it turned out to be a little bit harder than you were thinking and we get back to this concept of blocking that if i have a for loop running this thermometer then this thermometer over here is still and if i try to run both thermometers inside of one for loop then that is not going to work that is not going to work very well either and the reason that is not going to work very well either is because one of them wants to run at a different speed than the other one so i will jump in and kind of show you've got to think in terms of non-blocking and for that really i don't think a for loop is the right solution but let's come over here and we are working in the most excellent v python folder i will create another one call it call it dual animations and dot p y the dot p y is pretty important and boom we have a fresh new python program just waiting to be written so let's jump in i think for the sake of demonstration here on the thermometers i'm not going to put the glass bulbs and the mercury balls and all that i will just have cylinder one and cylinder two and so i will call it my i better import my libraries and so i am going to from the python import import star and then i don't think right now that i will be using numpy on this one i will create my cylinder one and my cylinder two so i will say my cylinder i don't like one and two i will call it let's see my cylinder i will call it my cylinder orange and that and i will call the second one my cylinder cyan so i'll have two different ones and this is going to be equal to a cylinder cylinder and then i will need a radius radius is equal to 1. and then i will need a color is equal to color dot orange and then i will need a length and make it six all right so i've got a radius i've got a color and i've got a link that's pretty good now come down to this one and this one is going to be cylinder radius is equal to one length is equal to six and color is equal to color dot cyan i think it should know those now this one i better give a position to because i don't want it on top of the other one so i better put it offset a little bit so i'm going to put a position is equal to vector and i think i'll just move it up and so i'll move it up to like two and then zero comma zero and this should make two this should make two cylinders and i'm going to need to do a while loop while true and then just pass i just want to see if i've got two cylinders all right so let's run this thing that moved it the wrong way i was not thinking that is not good i see exactly what it was i don't want to move it in x it's like x comma y comma z i want to move it up and looking at that i think that i should also move it up by about three and so that should give me one cylinder on top of another let's kill it and let's give it a try okay two cylinders they are both round and that looks really good now what do i want i want the top one to be moving twice as fast as the bottom one and again your first intuition was probably your first intuition was probably to have two different for loops and you probably had a for loop and a for loop and then you realized it was going like this okay and that was not what the assignment was then you put a for loop inside of a for loop and this one's going like this and this one's not moving and so what i hope that you see is i hope that you see that you can't do it with a for loop and so we're going to do it with a while loop but if we're going to do it with a while loop what we're going to have to do is we're going to have to set some parameters up and it's going to start looking a lot more like that animated ball bouncing in a box because we're going to have to set up certain things well one thing is we're going to have to have my cylinder orange length okay and let's set that equal to six and then my cylinder orange my cylinder my cylinder cyan length is equal to 6. now why am i setting up variables because i'm going to be changing the links down in the for loop so each one of these needs to have its own length and i think maybe i'll start it at one both of these at one okay now what i'm also going to need is i'm going to need an increment of how much each one of these changes every time through the loop well i want the cyan one and the orange one to both have their own increment so i'm going to say my cylinder orange delta how much difference in length what is the increment in length each time through the loop well i'm going to say 0.1 now my cylinder psi n what is its increment going to be it's going to be going twice as fast so i want it to be twice as big and i should say that more clearly it's this increment that sets how fast it's going to be moving so if i want my cylinder cyan to be moving twice as fast the delta needs to be twice as big so this is going to be 0.2 all right so i've got a length and i've got a delta and i've got them created and so inside the loop what we are going to do is we are going to increment those two things so i'm going to say my my cylinder and i'm going to start using these autocompletes orange length is equal to my cylinder orange length okay plus my cylinder orange delta okay i'm going to have to scoot out of your way more my cylinder orange delta so next time how big is it going to be well it was my old link my my my cylinder orange length is equal to my cylinder orange length plus the delta all right and now what you can imagine is i think i could probably do this quicker by snagging this copying it putting it here and pasting it and then here the orange becomes cyan and then i'm going to copy cyan and then i will change orange to cyan okay so every time through i'm making it a little bit bigger but because the my cylinder cyan delta is twice as big that one's going to be growing twice as fast but one of the things we are going to have to okay and then let's just apply those so now my cylinder orange is dot length is equal to my cylinder orange or inch dot length okay so let's make sure that we understand this my cylinder orange is the object up here that is the object what parameter do i want to change i want to change this parameter length so my cylinder orange is the object and dot length is the parameter that i'm going to change and then i set it to the variable i created my cylinder orange length and then each time through the loop that is going to be getting bigger well similarly i need to apply that length to cyan and so i will change my oranges here to psi n cyan like that my okay so now every time through the loop both of them are getting bigger but what is happening they're both getting bigger but the cyan one is getting bigger twice as fast because each time it's going to jump more but now with this they're just off to the races they're getting longer and longer and longer i need to make sure that they turn around and go the other way well when do you want them to go the other way well if they equal to or greater than their total length which is 6 or if they're less than or equal to their minimum length which let's just say their minimum length is 0.1 all right so we need to put some if statements in if my cylinder orange link my cylinder orange length is equal to is greater than or equal to what six or my cylinder orange length if it's what less than or equal to 0.1 so i've either gotten as big as i want to get and i want to turn around and go back or i've gotten as small as i want to get and i want to turn around and start getting bigger so it's bouncing off of those two edges what do i want to do in those cases don't forget your colon what i will want to do is i will want to make my cylinder orange delta equal to my cylinder orange delta times minus one right so if it's positive and getting bigger and hits the edge i make the delta the opposite and then it will get smaller until it hits here and then it will get bigger so i'm switching the delta from positive to negative so it's getting bigger bigger bigger bigger hits the edge then gets smaller smaller smaller smaller hits the edge bigger bigger bigger bigger does that make sense the delta is going from positive to negative and then that means it will be getting if it's about to get too big it'll start getting smaller if it's about to get too small it'll start getting bigger and then i think these should be the same i do believe for the cyan so i'll just copy those come over here paste them that didn't quite paste right with the indentation i'll get that like that and now i need to change this to cyan cyan cyan cyan all right so let's see this at least should be a good shot at starting it so every time it gets bigger then we make the application but if we've gotten too big then we turn around and turn it back the other way so let's try this this is absolute insanity this is insanity okay so let's look here one thing is we didn't put a rate so we need to put a rate in there to slow it down so i'll put a rate of five but man there was some something not right so i have my cyan length my cyan length one of those looked like it might have been working so let's come up and let's take a look at it now that we're running it slower cyan cyan cyan orange orange orange all right let's take a look at this running slower maybe something will look reasonable kill it and run it this actually looks kind of interesting it seemed like the first time it worked but then the cyan is going negative and so that cyan did not see that it had gotten too small and kept getting small so let's see what we're doing wrong you see the the orange is working the cyan is going faster but it doesn't bounce off the lower side the cyan doesn't bounce off the lower side so if my cyan length is greater than or equal to 6 that is working or my cyan length is less than or equal to 0.1 so it's this one that is not working let's take another look at this let's run it again so they come up and when it's greater than six both of them turn around they are going up at different speeds but it's this lower one when it's less than or equal to my cylinder okay so it comes down and it just jumps right over it you guys have any ideas it's kind of strange behavior isn't it okay i think maybe what we need to do is we got to be a little bit more careful let's say because i think this is already jumping negative so let's make these increments smaller because the problem is i think that at that boundary condition i'm putting it at point one and the step size point one and so let's make the delta 0.01 let's make the delta 0.02 i just want the deltas different than the endpoints and now i'm going to make it go faster because i made those increments so small so let's see what happens here because now these are far away from the point one so let's try this okay so that is looking good so far they're coming out smoothly the blue turned around now will the blue jail breaker will it bounce off that bottom one all right boom okay i think it was just a quirky thing because i was going to point one and i was comparing at 0.1 and it just doesn't work well for those to be the same thing look at that boom okay so what do we learn this gets back to that kind of blocking versus non-blocking thing when you want to do something like this where you're independently working with two different things and animating them you can't do them in for loops where this one has a for loop and that one has a for loop you have to have one while loop and then you have to have parameters that will work together that you increment those parameters within a while loop does that make sense as i hope i'm explaining why this is important it is important to understand how to independently operate with two different two different animations and the best thing is to do that with one while loop and then independently control the parameters of each one of the objects that you are trying to control so we got that working all right i think uh what i will do is what i will tell you is if you were unable to do this on your own without watching my video but you were able to do it after watching my video what your homework now is is to start with a blank program and write it on your own without watching me and without looking at your old program because you do need to understand this so that you can do it by yourself so do it by yourself and make sure that you don't just move on without understanding it well enough to do it doing it by yourself and then i think next week the lesson that we will work with the next thing that we will do which will be lesson number nine we're going to start going in and understanding colors a little bit better because right now in our program we're just using the kind of can colors of like cyan magenta yellow red green and blue and there's just a few other colors that it will do but what if you want the full spectrum of colors how do you really handle colors in v python and that's what we'll do next week okay guys i hope you're enjoying this this was probably a little bit more tricky to figure out on your own but hopefully you understand it now that you've seen me do it and you guys if you like this lesson be sure to give us a thumbs up if you haven't subscribed already make sure that you hit the subscribe button when you do make sure that you ring the bell so that you'll get notifications when my future lessons are coming out and then uh you know also please share these videos because we need many more people who know how to code and fewer people that just sit around and watch silly cat videos okay all right guys this is paul mcquarter from toptechboy.com i will talk to you guys later [Music]
Info
Channel: Paul McWhorter
Views: 2,755
Rating: undefined out of 5
Keywords:
Id: DB-GA5VAHmc
Channel Id: undefined
Length: 24min 34sec (1474 seconds)
Published: Wed Sep 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.