 How is it going everybody? What's going on and welcome back to another pie game python programming tutorial in this video? I'm gonna call back to one of the earlier concepts we were talking about in a previous video and that's all about collision and Earlier we were looking at collision detection Then we were looking at collision resolution and that sort of thing and we actually have that working just fine in our gravity Kind of platformer example that we have working I can jump up and down and I can collide with these blocks now That's using the arrow keys to move but earlier I had a video that I would move with the mouse and I can't really demonstrate it right now but there was some mouse movement work that we had going on and I Didn't explain or show how we can use mouse movements and still have the collision effect that we wanted to Well in this video, I'm gonna show you how we can do that. So it's pretty pretty simple the thing is earlier we were using the Rect dot x and the position of the actual block itself to determine where we should be aligned with the mouse and In this example, we're gonna be changing it up a bit So we're actually using the h speed and v speed and that is what will determine Whether or not we're actually aligned with the mouse or not. I'm not gonna be using the origin I'm not gonna be using that at all because I just haven't figured out a good way to actually implement that boom How's it going everybody? I'm talking to you right now from the future Thanks to the wonderful power of video editing. I just wanted to shove in a quick notice at the very end of the video I do end up showing you how to use the origin X and Y values that can kind of change the position of where object is with the mouse So kind of disregard the fact that I say I am ignoring this I do show it to you at the very end of the video. Awesome. See you soon So I'm kind of gonna disregard the origin But we're just gonna do stuff and it's gonna happen. Okay I also realized this is indented a little bit more than it needs to be I can fix that Okay, so the way that we do this first of all is we can go ahead and First I'm gonna go ahead and save this as a different file name. I'm gonna call mine just mouse other dot pi because I want to keep This same file the current one that we had and just use this as a different one because I'm gonna make a few Drastic changes here and when I say drastic, I mean I'm going to completely chop out the event handling loop and At least conditional statements inside the player object The block object in this case in its update function. I'm gonna chop all of that out and I'm also going to Chop out the experience gravity function and we don't have to have that call at the beginning of the update function itself Now back down at the very bottom of the program with the event loop in The main function essentially We don't need to reset the event and we don't even need to pass it into the update function at all Now if I run the program, hopefully I don't have any errors. I might have overlooked something But okay, very simply now our block is just in place Doesn't move nothing's happening. It's just kind of there Now let's set up the control for actually moving with the mouse The way that I'm gonna do this is by setting up change with the H speed and V speed I'm gonna do that by going over to the update function in the block itself And I'm gonna have a call to the mouse dot get position function And I'm gonna store what that returns remember returns a tuple In variables called mouse x and mouse y Pog game mouse dot get POS or get position Okay, now at the very bottom of all this before the for loops. I'm going to test I'm gonna look at my notes here to make sure I've got this okay if The mouse x Is equal to self dot rect dot x Then self dot H speed is going to equal zero Otherwise We're gonna change up self dot H speed Now I'm gonna try and explain this but it's not going to make much sense at all until you actually see it happening but if the mouse x is equal to the rect x which means that if the mouse is at the current position of The object itself. It's not gonna move in the horizontal direction at all H speed is going to equal zero Now if the mouse x is it not is it any other position other than the objects x value We're gonna change the H speed to equal the difference and when I say the difference I mean mouse x Minus self dot rect x now whether or not this is positive or negative It's still gonna kind of move to the position of Of the mouse x now we can copy and paste this and do this for the y variable and Of course change it to be v speed Now if I run this program We'll have a bit of a different kind of setup I can move this around very easily remember I'm not taking part of the origin and I also have slight Collision detection because 8 speed is what's being manipulated whenever we actually Test for collisions 8 speed and v speed those those speed variables But you'll notice something if I move my mouse Further than the size of the object itself past the collision Then for some reason it'll jump right to the other side now We don't want this we want to keep the collision happening no matter what because you you're not supposed to just be able to just Clip through the objects you're colliding with especially if you're moving around with the mouse So the way that we do this is by setting up some tests and actually determining what way we are colliding with things So over at the top of the update function underneath the mouse x and mouse y set setting in the definition I'm gonna create a few new variables, and I'm gonna call them colliding left and That's gonna equal colliding right which is gonna equal colliding up Which is gonna equal colliding down and All of these are going to equal false I'm just using the equals equals equals syntax. So all of them equal false all in one line Okay, now because we have these at nice little comments here that tell us when we're actually colliding in what direction We can go ahead and very easily say okay colliding right now is going to be true and Of course colliding left it's gonna equal true over here And that sort of thing and we'll do the same exact thing for down and up colliding down Equal true and colliding Up equal true over here if we run the code nothing is gonna really visually change because all we're doing is just adding the variables I I meant to show that to you and I'm actually pressed escape before the program even finished But yeah, see there is no change really we have to account for it in our conditional statements I'm gonna do it in the else statements because that's really where all the things happen At least resetting the position Okay, now I said earlier. There is a change. There is a difference in what happens mouse x minus Self dot rec dot x or mouse y minus self dot rec dot y. So I'm gonna call that change Change is equal to that Now the kind of the mindset that we had with our collision testing Actually testing if the speed is coming from one direction or not whether or not it's up or down We're gonna kind of have that same mindset Even for the mouse collision detection that we're doing right now We're gonna test if we're colliding to the left or if we're colliding to the right and if we are then okay That means we cannot move in the direction that we're actually colliding in so we're gonna test for the Opposite direction with change if we're moving to the left if we're colliding to the left side of the object Then that means we can't move anything that is actually to the left of us because we're already colliding in that direction So we have to be able to see changes that only occur Sorry that only occur in the right direction moving to the right and that means that change has to be positive So we'll only allow for change to happen if change is greater than zero or if it's positive in that case Self that H speed can equal change. You guys see that works The same thing happens at least vice versa with the right direction If we're already colliding in the right direction, then we can't move to the right anymore We have to move to the left which means that change will have to be negative Awesome. Now, let's set the same sort of thing up for the vertical direction Get into our L statement here Set change to equal the difference that we actually have and we'll test. Okay, if we're colliding up That means that change has to be positive and v speed is going to equal change and if we're colliding down That means that change must be negative and v speed will then equal change Now if we run this program It'll only change if we're in the corresponding way I must have done something wrong here Because right now it's not working. I'm gonna test this here. Okay. I do see the problem This is actually only happening It won't let us move at all because none of these things are true yet So we actually kind of have to change the way that our if statements are working I'm actually going to say if colliding left can be first and then this if colliding right That's going to be using an L if statement for else if and if none of these things are true Well, then we obviously aren't colliding with anything So we still want to account for any of the changes in the mouse movement H speed can equal change. We'll do the same thing with the vertical direction with V speed and y else self dot v speed. It's going to equal change Okay, now when we run this code now, we'll see it happen. Hopefully I'll drag the program up and I can keep moving with the mouse and the moment I collide I can keep moving further and further and further with my mouse, but I'm still colliding awesome Now the moment I move past it moving I I release it I jump right over to that other side because h speed and v speed are being set appropriately. Oh Okay, it looks like it looks like v speed isn't working for us. We must have done something wrong there Cliding up colliding down Okay, so I did some quick experimenting with this and if I run it like I like I just showed you beforehand the Up and down will not work at the moment And I realized that this is because of some code that we left from the earlier collision detection Earlier when we were testing for all the colliding objects in the collision list Every time we were colliding with the up or down because of gravity We set v speed equal to zero now that gravity doesn't exist We can just kind of go ahead and cross out this command here the statements and If we were on the code now v speed does Not actually have anything that happens with that other setup So v speed will work the in the correct way that we want it to whether or not we're colliding up or down And the mouse keeps moving of course we had h speed working beforehand So this works perfectly fine for us everything is working as expected We have collision with the mouse movement that we were using beforehand awesome. It works guys Don't know what to tell you other than that it works. Thanks for watching. I hope you enjoyed this and I'll see you in the next tutorial. Bye Hey, sorry everybody I realized again that I am making a mistake at the very end of the video in this case I do want to show you that I You can actually implement the origin kind of concept and idea that we had in in previous videos All it takes is a very simple subtraction I noticed in my set position function All we're doing is actually taking the x value and then subtracting the origin x or y and you know We can do that exact same thing with our change variable over here in the testing for the new mouse coordinates we can of course subtract self the origin x and You know for the y value you can of course subtract self origin y and what do you know? This will have the exact same output and kind of result That we really kind of expected with the origin of course being in the center or wherever we put it in our object So very very simple all it takes is a subtraction sign I'm sorry did not include that earlier But I just realized after I finished recording and now you know how to do it simple stuff Thanks for watching guys. I'll see you in the next tutorial