Hello and welcome back to let's code physics. We are continuing to have some fun with our rope simulator. Last time we got it to interact with some surfaces. There's, still a few glitches to work out there, but I think we've got it good enough to where we can proceed with making a simple cut the rope style game.

If you have not played cut the rope, you know feel free to pause. This go ahead and get it on your mobile phone or your or your tablet and play a few rounds. It's, a lot of fun and involves the all this rope.

Physics we've, been talking about. We have to treat a rope as a series of Springs really in order to in order to make this thing emulate a cut. The rope style game I haven't had to change much. I went over to my FTL code and borrowed this this grabbing code that we attach to the mouse down command.

So that just means that whenever you click on the screen, it's going to initiate this grab subroutine and basically what it & #. 39 s doing in this loop is it's, checking whether you have clicked on one of the atoms or you click on one of the springs.

I haven't quite gotten it to work for clicking the springs it doesn & # 39. T seem to respond to that. I think, because the springs position is defined as the beginning point, which is where the atom is anyway.

But I'm going to keep playing around with that to try to make that a little bit a little bit easier to work with. Let's see, so what this does then, is if you click on an atom. It turns that Adam invisible.

So, basically, when you are cutting the rope, your severing it at one, one of the connections, it's, making one of the springs invisible and it's, also turning off the connection between the between the neighboring atoms.

So this K list - this is just the spring stiffness that we had earlier. Only now I've, had to turn it into a list so that we can keep track of what spring still exists. You can see that down here whenever it creates.

These Springs, it also creates the spring stiffness. All them have the have the same default value and then later on, when we're, calculating the force caused by the springs instead of just referencing KS, its represent k list of that particular pair of of atoms, and so whenever this gets turned 20, Then there's, no more spring force there.

So let's, see I've left in my left. In my surfaces from last time, we & # 39. Ve still got the rope swinging like this. Let's zoom in a little bit, so when I click on this thing, if I successfully click on one of the atoms - and this is one things I need to fix today - is it being able to capture one of the springs? Basically, it cuts.

The it cuts the the spring connection there, so you can see we've, gotten rid of one of the M, so it's like we & # 39. Ve got a rope here and a separate rope here that's, actually managed to bounce down onto the bottom floor there.

So what I'd like to do today is a couple things. First, I want to see if I can fix this spring issue, where it's, not selecting the where it's, not picking up on the the spring and see if we can come up with some way of up having you you, You click on the actual physical spring and it cutting it there, because otherwise, you're stuck, you know, searching for those joining points.

You know in between the springs and then the other thing I'd like to do is turn this. Will be more straightforward as turn the surfaces into a little bucket and then the object of the game would be to try to get the IV to try to get the the bottom of the of the of the rope to fall into the bucket.

So we'll, make the last Adam bigger, so that'll, be like the candy, or something like that so in in looking at this, I'm thinking that one way to do this would be instead of asking what Happens if the instead of asking, if, if you & # 39, ve selected one of the objects because only defines the object, as you know, that particular point it's.

Looking like, I wonder if we get to find it as looking at the space for the spring, so in other words let's comment this part out, because we might need it later. You know if we want to go back to the to the old version, so what I'm thinking of here is, I'm thinking of taking a look at the location of the event.

So if event pause - and here we're going to do a little bit of pseudocode - is in the space of the spring right. So I need to get some idea of looking where the spring is extended to not just the location that it associates with the spring.

So what I need to do is I need to get, for example, the top of the spraying, the bottom of the spring, and then the I suppose, the the radius of the spring. So you think the springs as being a cylinder space.

If I click anywhere in that cylinder, I want to be able to to find that out. Actually, let me even before I do that I made the assumption that, if that dot pause is an attribute of this object event, so let's actually see whether that's even a thing.

Let's, see let's. Put on our semicolon here. So then let's. Have it print event pause? Actually let's. Do this so when I click somewhere its going, if I click within this area, it's, going to tell me you clicked on and then it'll, say event, dot pause.

So if I see this and then i get an error for event, dot pause, doesn't exist, and i know that i that i don't want that i don't want to do that. It might be this event dot pic pause. I'm, not sure let's find out okay, so swinging back and forth.

Let's. Zoom in here, oh cool, you clicked on this so event, dot pause is a thing in fact, awesome. Now the trick is: hmm, ok, cool! Ok, so does it know that even if I don't click on one of the springs or on one of the atom? Excuse me so let's put that outside the loop.

So this will now it'll check for this, no matter where I click. So let's. Give that a try and let's, see here you clicked on this. You clicked on. Basically, this ok cool and it's, always taking it as being at Z, equals zero.

As long as I have that orientation yeah, I change my orientation, it will change to their okay. No look! I'm still able to cut the red part right, cool, okay, so event, dot pause is actually a thing, so that's good to know.

So what I need to do now is start figuring out sort of the space of thats that's taken up by the spring. Let's, see here. So what I need is, I need some edges kind of like what we did with our surface is down here, I to left edge right edge of automatic top edge, etc.

Um, okay, I think the way I want to do this. Hmm, let's. Maybe get a diagram out here that's, always I start out thinking this is going to be something simple. Then I get into the geometry: it's, actually quite complicated.

So let's, see let's. See where's, my whiteboard there is my whiteboard. Ok, so these Springs. You can think of them as a cylinder right. So they & # 39. Ve got a radius that's specified and they & # 39.

Ve got a length as festival, but they & # 39. Ve also got this direction right, so what I want to know is, I want to be able to tell whether some given point is inside of that cylinder. Ok, so let's. Think about this, and actually I wonder if I only need to hmm, i wonder if i only need to worry about the uh cuz it broke on one of these.

I wonder whether i only need to worry about it. No, i think i do need to worry about x, y & amp z, because this thing could be rotated back before. So i'm, actually making something that's more challenging the cut the rope, because i'm, making a two-dimensional rfu.

I'm, making a three-dimensional version. Maybe i should start with the three-dimensional version: how about that? Let's, move on to the three-dimensional version a little bit so for right. Now let's, assume that nobody's, going to rotate the screen on us and in fact we can rotate screen and see if the code still works.

So let's. Think of this way, so we've got this spring, so we're, looking at the cylinder head on, so we're, ignoring the the Z direction, and so I want to know whether a given point is inside this Rectangle know this rectangle can be rotated kind of any which way.

So let's. Think about this. So the the point that we pick let's, call that XP and XS right so that'll, be excess. No! No YP, otherwise it excess. Oh, you know what I was thinking back to police officer and speeder from the first video that we did together.

Why pick our X pick and why pink there we go so that'll, be the location of this thing, and I guess what I'm really concerned about is whether it's within these corners. Okay. So so i would need to think about the four corners of this thing.

Uh-Huh so call those point. One point two point three and point for doesn't, no matter how you label them, and so what I want to know is. I want to know whether these whether this point XP YP, is inside these points X, x1, y1, x2.

Y2. X3. Y3. +, x4, wife, or so I guess the way it needs to work is that there needs to be at least one of these. That is group that has a greater. Why well at least one of these. That has a lesser why at least one of these it has a lesser X, at least one of these that has a greater x, okay, um-hmm, ok, so, for example, what I could define would be a top point, so I could define a top that has a Top corners now so maybe, instead of thinking of edges, I should think of the corners.

So the top is going to be the corner with the greatest at with greatest. Why corner with the highest? Why? The bottom is going to be the corner with the lowest Y, and then I could do the same thing with the X and the with the exes.

I can make a left that has the let that has the lowest X and a right that has the highest X and then I could just compare those values against this points. Value, ok, cool! So let's. Do this thin! I need to figure out a way to translate these into the spring, so that basically the way it works.

Is you get a point over here and that's, the springs position, and then you get a point over here on the other end, that is the springs position plus its axis, not post axis, but pop position plus access there.

We go ok. So if I set it up that way, that would get me sort of the central point here and the central point here and then I could simply add this distance going in this direction. That would be perpendicular to the the axis.

So this is going to be this distance right here, and I show drawn this: bigger is going to be half the radius in this distance, so I need to figure out a way to get a vector that's perpendicular to to the axis in The XY direction.

Ok, so let's. Do this one? Let's list list! Let's just start calling these things corner, one corner to corner 3-pointer for etc so corner one actually before I do yeah yeah. So let's say: corner 1 is equal to.

We'll start out with the position vector, so we'll start out with the pit with the position the spring, so we'll, say spring list of I pause and then I wan na say plus half of whatever. This direction is plus 0.

5 x perfekt, so perfec is going to be this vector here. Points perpendicular to the to the axis, so perfec is going to be this vector here this perpendicular to the axis. The best way that I know to make sure that you get a perpendicular vector is to take a cross product and the thing we could take the cross product of would be.

We could say that perp vector perpendicular vector is going to equal the z-axis cross with the spring axis. So, if i take right hand rule if i do z, cross-post axis yeah, that will point in the that will point in the direction of the vector.

Here I mean it: doesn't really matter, because i'll just do plus one way and then minus the other way, and then we'll figure out the maximum ends later: okay cool - I like this. I like this. So now my question is: does the Python know how to take a cross product? I have not done that.

Yet I don't think we've had to do that in oh, we have had to do that in class. Yes, it does take a cross product, the Python cross product. There we go, let's, see here, uh, okay, yeah, so you take one vector dot cross the other factor.

Okay, um. I think let's, actually open. The website find out. So I see here let's. Take a look at the cross product cross, a comma B, okay cool! There are lots of different things all right. I will come back and learn more about their vectors later.

So, okay, so what we're going to do? Is we're gonna say that the perpendicular vector is equal to perfec is equal to the cross product of the Z direction. So I need to get a Z hat equal to vector 0 0 1, which is the sector where earth is located in star trek by the way across Z, hat Zr hard hardy har har across Z, hat with the spring list of i dot axis cool.

So I've, taken the cross product of Z, hat with the springs, access cross practice iya with the spring access cool. So we'll, say: okay. Now that gets me a good direction, but that doesn & # 39. T necessarily get me the magnitude right, because this thing axis, the springs axis also contains the length and that's, not necessarily the link that I want this to be so let's.

Do this? Let's, turn back into a unit vector divided by magnitude of purbeck. So whenever you have a vector, if you divide it by its magnitude, you automatically get a vector of magnitude, one that's, pointing in the direction that you want.

It's, it's, a neat math thing. If I were writing a math textbook, I would call it a vector pro tip, but they don't. Let me write math textbook, so you know that's. Okay x, let's, see here times the i suppose it would be times the radius of the spring, so spraying list of I radius, and so that means actually don & # 39.

T need this half on there because the radius is going from the center to the outside. So we'll, say: corner 1 equals spring list of ipod, + perfec will say corner to. Excuse me wrong line corner 2. Is that thing minus perfec? So, basically, no matter which direction / vet gets defined.

One of them is going to be going this way, the ones going to going the other way. Now the trick is. I have to get from this point to corner 3 and corner for, and for that we have to start at the position plus the axis, so I can take these say copy paste make this corner 3 make this corner for and now all I need to do.

Excuse me now: all I need to do is take this and add to it a spring list of i dot axis because the beginning the spring, is it spring list pause. The end of the spring is at spring list, dot pause, plus springless axis again.

We are going to write a let's code physics, dr. Seuss book at some point cool. So we get a corner one quarter to quarter three quarter for okay, and this is something I will have to update every time.

I click because the because the because the Rope is swinging back and forth cool so now. What I need to do is I need to turn those into which one is at the top, which one is at the bottom etc. So, let's.

Do this? Let's say that so I need some new things called top corner. Left corner right, corners, etc. So let's, define a top corner equal to 11, a bottom corner equal to corner, to left, corner equal to corn or three at a right corner equal to corner for in it.

Oh, actually, let & # 39. S start them all at one. Actually, yeah all right here's. The deal right when you've got a rectangle that's. Oriented in space, only one corner can be on the top. Only one core can be on the bottom.

Only one can be left alone would be on the right. The other way you can have it where they share one is, if it's, add up if its peril. If one of the sides is our excuse, me, two of the sides are parallel to the ground to the y axis.

The x axis excuse me or to the y axis, because if two edges of parallel one, the other two edges parallel Taylor, because it makes right angles awesome. So if i go over and loop over the possibilities and evaluate their their their exes and their wise, i should be able to get them sorted out.

So let's. Do this let's, see we are already in well. I ok yeah so now what I want to do is while I corner equals. Ok, I need to make a change. This is eat. Excuse me is less than or equal to 0. Actually, you know what let's.

Somebody was just commenting. You know you can use a for loop in in Python right and Ivan hesitant to do that. Let's actually learn how to do that now, because I think that'll be helpful in this situation for Luke Python wiki awesome, because I've, just been sticking with while loops that's.

What my students textbook does so I've, just been in the habit of psychic. I'll, just use a while loop and something one of these days. I'll, get to a for loop. Let's, see here for loop from 0 to 2, okay, and I think I can just use this with a list.

Um excuse me um 4x in range yeah see this. Is they always okay? Okay? Okay, so i can make a list. Okay for x, in a list: okay, perfect! So let's. Do this? Let's, make this corner list equals corner one corner to corner three corner for huh, and now we're, going to say four corner in corner list, and now i'm literally looping over this.

I don't even need to counter any more awesome if, okay, so let's, take a look at the bottom at the top corner. First, so if a corner dot y is greater than I top corner dot y, then you need to make top corner equal to the current corner.

Okay. So basically, what this is saying is is the corner that you're, looking at like let's, say, for example, that you started out with this corner here and they start looping over the corners when you get to this corner here You see that this corner that you're looping over, has a higher y value than this one.

So you call this one now the top corner instead of this one, because they're, also being default it to the first one example: doesn't work because corner one is the top one here, but you'll, see What I mean in a second, so let's, see if corner dot y is greater than the top corner.

Then the corner that you're currently looking at, becomes the top corner cool. If and then we can do the same thing for the bottom, just change this top to a bottom and make this a less than so. For example, we start out with bottom corner equals corner one so when it gets to two it's going to say, okay, the y coordinate of corner 2 is less than that of the current top corner.

So let's. Make this the current top corner, then he goes number three. It says. Oh excuse me bottom corner it'll, say: okay, this corner the Y component is less than the Y component of this corner. So let's.

Make this one. The new bottom corner, then it comes before it says this - for assuming this corners y component is greater than this one. So we're, not going to touch a bottom corner anymore. We're, just gonna leave it there.

I think I used all the right words. They're, not going to go back and try it again. Then I can do the same thing with the left and the right left in there right did it then did it the wonderful album there or royal crown revue oops forgot bottom corner there.

So here now, we're doing the right corner because we're saying if it's greater than this yeah. This is the greatest challenge is just changing everything that needs to be changed. Let's. Go to left X and we're, changing this one to left.

Ok, so let's double check this greater than top top. Yes, why why? Why why less than bottom bottom huh x x greater than right right, x, x, less than left left? Ok, so if I've done that correctly, the code should now I've identified.

What are the coordinates of the top corner of the bottom corner of the right corner and the left corner cool? So let's. Do this, if it don't pick it close Adam, lift or, if event dot pause and all we do is we need to check for things we need to check that event.

Dot pause X is in is, is inside this range between the left corner and the right corner, so it needs to be greater than the left but less than the right. So let's say if this is greater than the left corner, X and event, dot position X is less than the right corner x and now I need to do the same thing with the wides.

I need to check to make sure that this is less than the top corners y position, but greater than the bottom corners y position. So we're going to say if event y is greater than the top corner, why an event position? Dot y is less than the bottom corner position why this is now like the third time I've done this type of check.

It's, it's, kind of getting its kind of becoming nice and routine for him between this code. In the up to yell code, I've had to put the send in several times. So let's. Double check this we've got the the Y position is less than the bottoms.

Why oops? I got those backwards, so let's flip these. So why would proofread? So if it's, Y position is greater than the bottoms y position. If it's, Y position is less than the tops y position. If it's, exposition is less than the rights exposition and if it & # 39, s come on come on.

Exposition is greater than the left exposition, okay cool. So we're, saying if etc, etc. So if you, you happen to click on an atom great or if this is true. Okay, I don't need the pseudo code anymore, right.

Okay, I've got my parenthesis taken care of then it executes this part. Okay, let's, see whether that worked. First, let's. Try clicking somewhere outside okay. We had a little bit of problem there. It's, trying to find something outside the range.

Let's see here. Click click vector attribute, has no position pause, oops right. If this thing is a vector, I don't need to call it position. Oh yeah, I don't need I don't need corn or dot position. Dude.

Wait! A minute: what is it, what is it saying is a problem here. Um I'll bottom corner about Plaza lives. I got paws happy, I just you know I try to maintain my positivity. You know let's, see how I & # 39.

Ve got bottom top right left. It also gave me a problem with the index being out of range. I think, did it okay, um? Let's, see here. Oh, it's, giving me an index. Let's set of range do with perfec okay.

So why does it not like the perfec calculation cross, Z hat spring list of I axis? Oh because my spring list is always one less than the atom list: okay spring list: minus 1, some English, i minus 1 - i minus 1 - i minus 1 - i minus 1, i minus 1.

I i just- one's all day long. I tell you um: let's, double check for any others. Let's, see spring list of I nope. Okay, we're good to go there. Okay, let's. Try that again let's. Watch for that again, okay, I'm, not getting that error whoa, oh man, that was so much easier.

Oh, my goodness! Okay, let's. Take a look here! Click! I can click anywhere now! Oh look! I got it down to one atom. I have. I have split the rope down to single atom and look oh right. It's, not going to check for the top adam.

Oh my gosh, that worked on the second try I'm. So proud of that Wow, okay, um, let's, see let's clean this up a little bit here. Let's, see here let's, see you'll appreciate. We're, going to define springs corners in the XY play.

This is to remind myself of what is going on here. Okay, and here we are check oops check whether a rope has been cut there. We go awesome. Okay, so now i can literally click anywhere. The rope is, and it comes flying off.

Oh, my goodness that is so cool wow, wow, wow, okay, um! I'm gonna. Do something really quick here beef? So we've accomplished the first goal wow I thought that was going to take longer and and require some cuts or something but wow that's.

Really great here's, something I'm going to do. Is that that bottom surface uh let's, extend that out a bit. I because I don't want the thing just kind of like falling off the edge like that like so I want to still be able to play around with it.

So I'm, wondering if gosh, if I make that bigger the thing, is going to zoom out. So let's. Do this instead of it being hmm. Oh, I know what I can do. I know what I can do. Um. Let's, make this thing enormous and then we'll, make it invisible.

So let's say this thing is 200 rope links by 200 rope links, but let's, make the visible equal to false. Ok, cool and now it just leaves me there so now, whenever I click on this thing, click click oh come on.

Buddy we were working just a second ago. There we go oh and there its lands on there um let's, restart that, because I want to show that it gets it'll. It'll, get caught down there at the bottom. Okay, cool that's; okay, so now it's just falling until it reaches the invisible infinitely big floor.

You know for basically infinitely big floor, ok cool, so we've got it to where we can click anywhere. On the rope and it & # 39, ll cut it that's, pretty cool. Have I set up the center for this view? Yet because it's, I feel like that should be higher or no, I feel like that should be lower.

So let's go with maybe two times and Adams times: L 0. No! Let & # 39. S go with maybe four times I don't feel like that is doing what I want it to, because I should be shifting the center down now. I think it's auto.

I think it's. I things automatically changing it. Oh well, it's. It's within view now, ok cool, so I'd like to do now is. I would like to take this this surface right here and just turn it into a little bucket.

So let's, see yeah, so I still haven't solved the problem with with that thing that was giving that's, not gonna. Stop me from making a game out of this. Let's. See here so we & # 39. Ve got this surface.

I want to make another one that is immediately to the left of it. Let's see so this current one. Its position is there. Oh, I know how to get it. I know how to get let's, see here, let's, have its left position B and I'm glad to find these.

Let's. Have its left position be at the left edge of surface list of one? Now I'm, so glad to find that, because now, wherever I put the the bottom of the bucket, the the other part of the bucket will be there and then let's say here.

The Y position will be at the bottom edge of surface list of 10 cool, and so I need to now kind of flip. This thing's, sighs. So let's. Make this into let's, make this its height and 0.2, its width cool.

So I'm, basically taking the previous one and moving on flipping it. Let's, see if that works invalid, syntax. Of course, what did I do wrong? Oh, oh, I need to close up my parentheses there I close my parentheses.

They are cool. Okay, that's, not exactly what I'd, wanted! Oh right, right right. I need to shift this thing up by half of its width or behalf of its height. So let's see here. So this is 0 0. Actually I can make this thing even easier canta.

I can make this one surface list of 1 dot size dot. Why? Because I just flip them manually, but I could just have the code flip them for me. Let's, say paste here, sighs, dot, X and then here. Actually, I let's, just mimic this one's, size, dot, Z and now the position needs to be the bottom edge of surface list 1.

Plus half of I want to go up by half of its own with so that's, going to be plus half of surface list of one dots, sighs dot X, I think, will work yes, okay, I got a little elder and it should actually Shot the thing from ooh look at that it knocked into it.

Okay, I'm going to need to move this whole thing down. I think still, that's, pretty cool okay. So let's, move this whole arrangement down. Let's see here it's currently at in Adams plus 10. So let's actually make it like two times in Adams: five, ok cool! So now it'll.

Now this part, i'll swing freely awesome. Now i need a right edge. Excuse me a right edge of the box, and here we go copy paste. So now i want the size to be the same. It's. The position that i want to be different so now, instead of it being at left edge, I want it to be at the right edge of surface list one.

I should be a thing. I need to change, ok cool, so I've got myself. My rope I've, got a little bucket and now my goal is to try to get the rope. The bottom are up to fall into the bucket. We have a winner, alright cool, so I've just made a fun little cut.

The rope game I want it all right now makes me feel pretty good um, so you know if oh actually, this is a perfect opportunity to test whether this still works in the UH. It does work when I rotate the screen.

Oh, that's. So amazing yeah, I'll, need to turn on the drag force or something to get rid of anus. Maybe when it collides, I should send it back with like half the velocity or something like that cool.

So to finish this out, um, let's, see here so the last Adam I'm technical, ready, coloring green and let's, make it sighs its radius about ten times as big as it currently is times. Tim go 450, not 250! Oh yeah! There we go so there's.

Omnoms candy foosh, I haven't changed the mass of that. So it's, not like it's swinging you differently and Oh almost made it. Oh ups, and it is oh dear oops mom's, not getting his candy. Is he um? Let's, see let's uh.

If 50 may have been a bit much let's, see okay, yeah! That's, a much more reasonable, looking candy its wings. This way, I need to cut it at the right moment to get it to fall in. I need to actually click on the thing there we go and there we go.

Omnom has gotten his candy okay cool, so we've, gotten the basic elements down for a very simple cut. The rope style game this that's. Actually kind of fun would be really fun now to get the thing spinning in three dimensions, and then you have to try to get in there that I would have to add a wall here and a wall at the back.

So what I'll. Do in the next episode is it start creating some more complicated scenarios for what we might need to do for a for a cut the rope style game? So, thank you so much for watching. I will see you next time.

Bye, bye,

OUR SOCIALS: