**Understanding Projectile Motion with Python**
In this tutorial, we will be diving into the world of projectile motion using Python. We'll start by understanding the physics behind it and then implement it in our code.
**Valve Y Times Time Plus Gravity**
To begin, we need to use the valve Y times time plus gravity formula. So, we're just gonna start by doing valley Y times time plus now these brackets are very important so make sure that you do not forget them. We're gonna do another set of brackets and now I know gravity is usually negative 9.8 meters per second squared but I like to set my gravity to about half of that just so it doesn't move down as quickly if you want to be more realistic you can change that number you can play with all these numbers you can play with and it's just gonna give you a different kind of motion.
**Calculating New X and Y**
Now, we need to calculate our new X and our new Y. To do this, we do new X is equal to and then we're just gonna round this here so we don't get crazy decimal numbers we're new distance X so that's how much we moved in the x-direction plus our initial starting direction our starting position. And remember in Python how moving positive actually moves you downwards rather than how you would think it works like if you're subtracting you go downwards so in this case we actually have to subtract to move upwards.
We're going to start Y minus and then our distance of y and then we're gonna return that value in a tuple. Let's go return new X and new Y again, we went kind of fast there but that's how this works, this is the physics that we're incorporating into this and that just gets our X component or Y component and then we can tell how to move our ball.
**Displaying Projectile Motion on the Screen**
We're not really bothered about finding like the max distance or the max height, we just want to display this on the screen so that we can see it. If we wanted to figure that out, that would be a different tutorial. Okay, so we've done that now and let's just check the rear code and make sure that everything should be working.
**Checking the Rear Code**
We have if shoot then we're gonna check to make sure that our ball is not shouldn't should be moving so it's not like below the screen. Then we're gonna move it, we need to add one thing here which is so once our ball hits the ground or goes lower than the ground we need to stop moving so we're gonna shoot it goes false and we're gonna set its position so that we make sure it's able to shoot next time.
**Setting Initial Position**
We reached go golf ball dot Y is equal to 494 and that's initially what we set it to here right. That should be about it, so I'm just checking through here quickly make sure it looks good and I think it does.
**Testing the Projectile Motion Code**
Let's try this out okay, so we have the line and let's see what happens when I click there, we go and you can see the ball shoots around the screen now. You'll notice this moves kind of fast right if we don't want it to move as fast there's a few things that we can do to fix that.
**Adjusting the Time**
The first thing we're gonna do to fix that is by changing this number here, now this time is well exactly what it does. By how much you increment it is how fast the program's going to go. If I change this to 0.01 and I run the program, you'll notice that we go very very slow like that if I change it to for example 0.1 and I run it then we go really really really fast.
**Playing with Time**
So there are a few things you can do to set exactly how fast the while loop runs but we're not gonna worry about that right now, so just play with this time number. I usually go anywhere from 0.01 to 0.1 depending on how fast your computer is and you can see that 0.025 seems to be working well for me.
**Conclusion**
I know it's probably not the best tutorial, I tried to go at a reasonable pace but again we can't spend too long going over everything this involves physics and involves math is pretty complex so if you guys understood it then give yourself a pat on the back.
"WEBVTTKind: captionsLanguage: enthe angle we're gonna have to use a bit of trig so I already have a function I've wrote here because I didn't want to have to do it on camera so I'm just gonna copy and paste it and I'm gonna go through it and give you as a second to copy it down so I'm just gonna paste it under here and it's called find angle so this is the function right here it pretty much takes a position and the position is well the position of our of our mouse and then vs. the golf ball so it already has a position the golf ball we just have to give it the position of the mouse and then what it does here is it tries to tangent the angle to figure or tangent the the golf balls X&Y to figure out the length of that line to start with right and if you can't do this so it can't arc arc tangent this then it's just gonna do math dot pi divided by two because the only time that math thought that this won't work is if it's if it's a certain value in radians so if you don't understand radians I'm gonna pull up a unit circle here quickly actually pretty much every degree has a Radian equivalent so 30 degrees is PI over six 45 degrees is PI over four and you can look on here and so on you don't really have to understand exactly how they work or why we use them but in one full circle we have two pi radians 180 degrees is pi radians and so on so you're gonna see that I have some like math dot PI's and stuff in here that's just because that's the way radiance work you don't really have to understand them but here's unit circle anyways if you want to look at it so we'll close that out so let me just go over this again I think I messed it up so pretty much the arctangent is what we use in for radians to figure out the angle if we're given two positions in a right triangle so let me just pull this up for a second I'm steadily good 0 for now and so if we have a line like this then that means if we go directly down and we go directly across then we can find the two components of those lines so the x value and the y value now if we know those two components or those two lines the values there and we know that it's a right triangle then we can use those two components to figure out the angle here that would be down here to the line it's hard to show you because my mouse is what's creating the line but I hope you can understand that so we're gonna we try to do that now with the with the arctan tangent if that doesn't work then we're just gonna default to this because that would mean that the one case where it doesn't work is is this alright so now this is where we determine what direction we're actually shooting it so once we figure out the angle now we have to determine if we're shooting left if we're shooting right if we're going down if we're going down left just copy this down here don't exactly you don't have to worry exactly what it does this just figure out what quadrant your mouse is in so looking at the unit circle we have four quadrants just figures out what quadrants in so copy this down pause the video and then we're gonna move on to the rest here so what I'm just gonna do for angle isn't I do find angle and then we're gonna put pause in here and pause is gonna be again the position of our mouse like that alright so now we've got that part down and we're gonna find the angle we're gonna find the power and we're gonna set where we're shooting from now we have to get the physics involved so we've used all the math now with the trig now we have to use the physics to determine exactly where the ball is going to be moving and how far at what time so we're gonna go up here to the beginning of our function and we're just gonna do a new thing here we're gonna say if shoot like this so that means the ball is in motion we're shooting the ball then we are going to what we're gonna have to do is now check if the ball has already collided with the ground or not so we know if we should continue shooting it or if we should stop the ball so I'm just gonna say if golf ball dot Y is less than 500 minus golf ball dart radius so this just means that we've now gone past the bottom of the screen or we're about to go past the bottom of the screen we don't want to continue moving the ball because that's our ground so it's gonna hit there and then ideally it would bounce but that's gonna be in the next part of the video now we're just gonna do time plus equals 0.05 the reason I'm doing this is because that's gonna be the increment of time that makes it look like it's moving properly Kanna so 0.05 seconds is what that's gonna be P oh this is just gonna be position I just don't want to override this position here so I just do this and we're gonna do full dot ball path don't worry we're gonna go back up and fix that method we're just gonna plug this in for now we're gonna do XY power angle time like that now we're going to set our positions or change them for the golf ball so golf ball that x equals P o zero and golf ball dot y equals po1 okay so I know I went really fast let's recap here quickly so pretty much what happens here if the user clicks the mouse then we're gonna check to make sure he's not already shooting the ball if he is not we're gonna set shoot equal to true we're gonna determine where he had shot the ball at so wherever that golf ball is at that time we're gonna set time equal to zero so we're gonna reset that to make sure that it is at zero we're gonna do power we're gonna set the power based on the length of the line and we're just gonna divide it by eight so it's not too big of a number then we're gonna find the angle between our mouse and the ball now the way that we do that is by using this function that I've written in here again you don't have to understand how this works but just copy it out and then we now need to do this ball power so this ball path function is what's gonna use the physics to determine how the ball is gonna move through the air so let's get into this now and this is going to be kind of the end or the wrapping up part of the video now so we're just gonna start by doing this we need to find the two components of our movement so we have a X velocity and a Y velocity now the way that a ball moves in physics in two dimensions is each second it moves X by us X by a certain amount of degrees and it moves Y by a certain amount or not degrees a certain amount right so it can move X by five maybe and it's moving Y by two or it's moving X by seven and moving down Y by three so we need to figure out exactly how that is gonna work so now that we have the angle and the power we know the diagonal power but we have to know what the velocity is going to be going X and what the velocity is gonna be going Y so we'll figure that out now to do that we simply need to just do math or cosine and then of angle multiplied by our power and this gives us our X velocity and to find the Y velocity we just do map both sine of our angle and our angle is in radians by the way that's this is y in Python all the mouth stuff it uses radians unless you convert it into degrees and then we have math dot sine angle times power so that gives us our Y component so now we go down and we have to calculate how far we moved on the x-direction and how far you moved on the y-direction now the X Direction is easy because we're moving at a constant velocity right so there's no gravity moving left and right gravity only pushes us downward so the X Direction is easy we just do our velocity of x times our time so if say for example if you're moving five meters per second and you're it was two seconds into your motion then how far would you be you would be ten meters because you moved five meters per second right now our Y is a little bit more complicated now this is just a formula you can find us online it's motion with downwards acceleration so since our acceleration of gravity is negative 9.8 meters per second down we need to use that in this formula so we're just gonna start by doing valve Y times time plus now these brackets are very important so make sure that you do not forget them we're gonna do another set of brackets and now I know gravity is usually negative 9.8 meters per second squared but I like to set my gravity to about half of that just so it doesn't move down as quickly if you want to be more realistic you can change that number you can play with it all these numbers you can play with and it's just gonna give you a different kind of motion x squared divided by 2 that so make sure you guys copy this out and it looks exactly like this otherwise it's not gonna work properly again you can change this number but not keep it the same and now we need to calculate our new X and our new Y so to do this we do new X is equal to and then we're just gonna round this here so we don't get crazy decimal numbers we're new distance X so that's how much we moved in the x-direction plus our initial starting direction our starting position and then remember in Python how moving positive actually moves you downwards rather than how you would think it works like if you're subtracting you go downwards so in this case we actually have to subtract to move upwards we're going to start Y minus and then our distance of y and then we're gonna return that value in a tuple let's go return new X and new Y again we went kind of fast there but that's how this works this is the physics that we're incorporating into this and that just gets our X component or Y component and then we can tell how to move our ball and this is exactly how projectile motion works now we're not really bothered about finding like the max distance or the max height we just want to display this on the screen so that we can see it if we wanted to figure that out that would be a different tutorial okay so we've done that now and let's just check the rear code and make sure that everything should be working so we have if shoot then we're gonna check to make sure that our ball is not shouldn't should be moving so it's not like below the screen then we're gonna move it we need to add one thing here which is so once our ball hits the ground or goes lower than the ground we need to stop moving so we're gonna shoot it goes false and we're gonna set its position so that we make sure it's able to shoot next time so it's higher on the ground reach go golf ball dot Y is equal to 494 and that's initially what we set it to here right and that should be about it so I'm just checking through here quickly make sure it looks good and I think it does so let's try this out okay so we have the line and let's see what happens when I click there we go and you can see the ball shoots around the screen now you'll notice this moves kind of fast right if we don't want it to move as fast there's a few things that we can do to fix that so the first thing we're gonna do to fix that is by changing this number here now this time is well exactly what it does by how much you increment it is how fast the program's going to go so if I change this to 0.01 and I run the program you'll notice that we go very very slow like that if I change it to for example 0.1 and I run it then we go really really really fast so you guys have to play around with this number it's gonna be different depending on your computer as well I know it seems weird but pretty much how fast your processor is is gonna determine how fast this while loop runs so there are a few things you can do to set exactly how fast the while loop runs but we're not gonna worry about that right now so just play with this time number I usually go anywhere from 0.01 to 0.1 depending on how fast your computer is and you can see that 0.025 seems to be working well for me now the longer that your line gets the more that your ball moves so I hope you guys enjoyed this tutorial I know it's probably not the best I tried to go at a reasonable pace but again we can't spend too long going over everything this involves physics and involves math is pretty complex so if you guys understood it then give yourself a pat on the back and if you want to see the kind of next parts of the tutorial where we make our ball actually bounce off the ground then leave a like and leave a comment just saying you want to see it and I'll be sure to do that later on so yep thank you guys and I'll see you in the next videothe angle we're gonna have to use a bit of trig so I already have a function I've wrote here because I didn't want to have to do it on camera so I'm just gonna copy and paste it and I'm gonna go through it and give you as a second to copy it down so I'm just gonna paste it under here and it's called find angle so this is the function right here it pretty much takes a position and the position is well the position of our of our mouse and then vs. the golf ball so it already has a position the golf ball we just have to give it the position of the mouse and then what it does here is it tries to tangent the angle to figure or tangent the the golf balls X&Y to figure out the length of that line to start with right and if you can't do this so it can't arc arc tangent this then it's just gonna do math dot pi divided by two because the only time that math thought that this won't work is if it's if it's a certain value in radians so if you don't understand radians I'm gonna pull up a unit circle here quickly actually pretty much every degree has a Radian equivalent so 30 degrees is PI over six 45 degrees is PI over four and you can look on here and so on you don't really have to understand exactly how they work or why we use them but in one full circle we have two pi radians 180 degrees is pi radians and so on so you're gonna see that I have some like math dot PI's and stuff in here that's just because that's the way radiance work you don't really have to understand them but here's unit circle anyways if you want to look at it so we'll close that out so let me just go over this again I think I messed it up so pretty much the arctangent is what we use in for radians to figure out the angle if we're given two positions in a right triangle so let me just pull this up for a second I'm steadily good 0 for now and so if we have a line like this then that means if we go directly down and we go directly across then we can find the two components of those lines so the x value and the y value now if we know those two components or those two lines the values there and we know that it's a right triangle then we can use those two components to figure out the angle here that would be down here to the line it's hard to show you because my mouse is what's creating the line but I hope you can understand that so we're gonna we try to do that now with the with the arctan tangent if that doesn't work then we're just gonna default to this because that would mean that the one case where it doesn't work is is this alright so now this is where we determine what direction we're actually shooting it so once we figure out the angle now we have to determine if we're shooting left if we're shooting right if we're going down if we're going down left just copy this down here don't exactly you don't have to worry exactly what it does this just figure out what quadrant your mouse is in so looking at the unit circle we have four quadrants just figures out what quadrants in so copy this down pause the video and then we're gonna move on to the rest here so what I'm just gonna do for angle isn't I do find angle and then we're gonna put pause in here and pause is gonna be again the position of our mouse like that alright so now we've got that part down and we're gonna find the angle we're gonna find the power and we're gonna set where we're shooting from now we have to get the physics involved so we've used all the math now with the trig now we have to use the physics to determine exactly where the ball is going to be moving and how far at what time so we're gonna go up here to the beginning of our function and we're just gonna do a new thing here we're gonna say if shoot like this so that means the ball is in motion we're shooting the ball then we are going to what we're gonna have to do is now check if the ball has already collided with the ground or not so we know if we should continue shooting it or if we should stop the ball so I'm just gonna say if golf ball dot Y is less than 500 minus golf ball dart radius so this just means that we've now gone past the bottom of the screen or we're about to go past the bottom of the screen we don't want to continue moving the ball because that's our ground so it's gonna hit there and then ideally it would bounce but that's gonna be in the next part of the video now we're just gonna do time plus equals 0.05 the reason I'm doing this is because that's gonna be the increment of time that makes it look like it's moving properly Kanna so 0.05 seconds is what that's gonna be P oh this is just gonna be position I just don't want to override this position here so I just do this and we're gonna do full dot ball path don't worry we're gonna go back up and fix that method we're just gonna plug this in for now we're gonna do XY power angle time like that now we're going to set our positions or change them for the golf ball so golf ball that x equals P o zero and golf ball dot y equals po1 okay so I know I went really fast let's recap here quickly so pretty much what happens here if the user clicks the mouse then we're gonna check to make sure he's not already shooting the ball if he is not we're gonna set shoot equal to true we're gonna determine where he had shot the ball at so wherever that golf ball is at that time we're gonna set time equal to zero so we're gonna reset that to make sure that it is at zero we're gonna do power we're gonna set the power based on the length of the line and we're just gonna divide it by eight so it's not too big of a number then we're gonna find the angle between our mouse and the ball now the way that we do that is by using this function that I've written in here again you don't have to understand how this works but just copy it out and then we now need to do this ball power so this ball path function is what's gonna use the physics to determine how the ball is gonna move through the air so let's get into this now and this is going to be kind of the end or the wrapping up part of the video now so we're just gonna start by doing this we need to find the two components of our movement so we have a X velocity and a Y velocity now the way that a ball moves in physics in two dimensions is each second it moves X by us X by a certain amount of degrees and it moves Y by a certain amount or not degrees a certain amount right so it can move X by five maybe and it's moving Y by two or it's moving X by seven and moving down Y by three so we need to figure out exactly how that is gonna work so now that we have the angle and the power we know the diagonal power but we have to know what the velocity is going to be going X and what the velocity is gonna be going Y so we'll figure that out now to do that we simply need to just do math or cosine and then of angle multiplied by our power and this gives us our X velocity and to find the Y velocity we just do map both sine of our angle and our angle is in radians by the way that's this is y in Python all the mouth stuff it uses radians unless you convert it into degrees and then we have math dot sine angle times power so that gives us our Y component so now we go down and we have to calculate how far we moved on the x-direction and how far you moved on the y-direction now the X Direction is easy because we're moving at a constant velocity right so there's no gravity moving left and right gravity only pushes us downward so the X Direction is easy we just do our velocity of x times our time so if say for example if you're moving five meters per second and you're it was two seconds into your motion then how far would you be you would be ten meters because you moved five meters per second right now our Y is a little bit more complicated now this is just a formula you can find us online it's motion with downwards acceleration so since our acceleration of gravity is negative 9.8 meters per second down we need to use that in this formula so we're just gonna start by doing valve Y times time plus now these brackets are very important so make sure that you do not forget them we're gonna do another set of brackets and now I know gravity is usually negative 9.8 meters per second squared but I like to set my gravity to about half of that just so it doesn't move down as quickly if you want to be more realistic you can change that number you can play with it all these numbers you can play with and it's just gonna give you a different kind of motion x squared divided by 2 that so make sure you guys copy this out and it looks exactly like this otherwise it's not gonna work properly again you can change this number but not keep it the same and now we need to calculate our new X and our new Y so to do this we do new X is equal to and then we're just gonna round this here so we don't get crazy decimal numbers we're new distance X so that's how much we moved in the x-direction plus our initial starting direction our starting position and then remember in Python how moving positive actually moves you downwards rather than how you would think it works like if you're subtracting you go downwards so in this case we actually have to subtract to move upwards we're going to start Y minus and then our distance of y and then we're gonna return that value in a tuple let's go return new X and new Y again we went kind of fast there but that's how this works this is the physics that we're incorporating into this and that just gets our X component or Y component and then we can tell how to move our ball and this is exactly how projectile motion works now we're not really bothered about finding like the max distance or the max height we just want to display this on the screen so that we can see it if we wanted to figure that out that would be a different tutorial okay so we've done that now and let's just check the rear code and make sure that everything should be working so we have if shoot then we're gonna check to make sure that our ball is not shouldn't should be moving so it's not like below the screen then we're gonna move it we need to add one thing here which is so once our ball hits the ground or goes lower than the ground we need to stop moving so we're gonna shoot it goes false and we're gonna set its position so that we make sure it's able to shoot next time so it's higher on the ground reach go golf ball dot Y is equal to 494 and that's initially what we set it to here right and that should be about it so I'm just checking through here quickly make sure it looks good and I think it does so let's try this out okay so we have the line and let's see what happens when I click there we go and you can see the ball shoots around the screen now you'll notice this moves kind of fast right if we don't want it to move as fast there's a few things that we can do to fix that so the first thing we're gonna do to fix that is by changing this number here now this time is well exactly what it does by how much you increment it is how fast the program's going to go so if I change this to 0.01 and I run the program you'll notice that we go very very slow like that if I change it to for example 0.1 and I run it then we go really really really fast so you guys have to play around with this number it's gonna be different depending on your computer as well I know it seems weird but pretty much how fast your processor is is gonna determine how fast this while loop runs so there are a few things you can do to set exactly how fast the while loop runs but we're not gonna worry about that right now so just play with this time number I usually go anywhere from 0.01 to 0.1 depending on how fast your computer is and you can see that 0.025 seems to be working well for me now the longer that your line gets the more that your ball moves so I hope you guys enjoyed this tutorial I know it's probably not the best I tried to go at a reasonable pace but again we can't spend too long going over everything this involves physics and involves math is pretty complex so if you guys understood it then give yourself a pat on the back and if you want to see the kind of next parts of the tutorial where we make our ball actually bounce off the ground then leave a like and leave a comment just saying you want to see it and I'll be sure to do that later on so yep thank you guys and I'll see you in the next video\n"