 Okay, welcome everyone to basic collision detection in Python with Tokyo EdTech. That is me in this video I'm gonna go over four methods of collision detection Talk about how they work and I'm gonna show you some code that actually implements that except for the last one because I don't know How to do that one using the turtle module. So let's take a look at what we have here today Our first method is called Overlapping coordinates so basically this is a very simple one This is where we compare the x and y coordinate of one object with the x and y coordinate of another object If they are exactly the same then we say that there is a collision This works really well for grid based games or tile based games where every Object moves exactly on the tiles But for games where it doesn't move exactly on a tile or exactly on a grid It doesn't work at all and you'll see some examples later. Okay, the second method is distance checking This is where we measure the distance from the middle or from the xy coordinate of one object To the xy coordinate the middle of the other object and we calculate the distance We use the Pythagorean theorem so the square root of a squared plus b squared so a little geometry there and if that distance is less than half of the width of this object Plus half of the width of this object Then you have a collision now this only works if each object is square or round or very very close to that Otherwise it doesn't work very well Third method is called an axis aligned bounding box and in this method We draw an imaginary box around each object and what that does is We well what we do that as we compare the corners I guess basically or the the sides of each box with the sides of the other box so if one of these coordinates is between the top and bottom and Between the left and right then we know we have a collision This is a very fast way of checking and what it lets us do is it lets us deal with Objects that are different sizes So if we were doing the you know overlapping method This would only register a collision when the ball was exactly here or if we were doing the distance checking method You know if it was here that it would be really close here, but not close Here so you can see there's different distances to the collisions And then the last one is called pixel perfect collision checking and what that does is it actually checks every single dot Every single pixel in the image against every single pixel in the other image So this takes a lot of processing power because there's a lot to check But you can see here It's more accurate Than say the axis aligned bounding box because you can see here. This would be a collision If you're using a a bb axis aligned bounding box collisions because you can see here There's an overlap of these boxes now, but you can also see that they're not actually Touching so depending on the speed of your game and how close or how big the objects are This might be really noticeable or it might not be noticeable Okay, so let's take a look at some code to do at least the first three and before I get to that Let me give a big shout out to my very first three 16-bit members of my channel Kevin was first Paul was second and joining them is yawn. Thank you so much So if you're interested in supporting the channel more directly Please consider joining and becoming a member down below. So what I have here is a very simple program that has some images and it has three different methods of Determining a collision you can see we've got the overlapping collision and this is where again We take the x-coordinate of one object compare it to the x-coordinate of the other and If that's the same and the y-coordinate of one object is the same as the y-coordinate of the other object We return true because this tells us we have a collision otherwise. We return false The distance method as I mentioned earlier is where we take the square of the difference in x Plus the square of the distance Difference in distance in y and then we take the square root So this is again the Pythagorean theorem so a squared plus b squared equals c squared take the square root of each side I won't bore you with the math But that's basically how it works and then how I did it here is if that distance is less than The width of one plus the width of the other divided by two That's half of each width that tells us that they are they have collided And then the last one is access-aligned bounding box now. I'll be perfectly honest. I don't quite understand this math I found this on stack overflow. I'll see if I can find a link again and put that down below but if not search stack overflow and Basically, you're checking for an x collision and you're using the math module. So you subtract multiply by two Do the absolute value check is that less than the width of the two objects? And then you do the same thing with the y again I don't quite understand it kind of get it but don't quite understand it and then you return Exclusion and y collision. So if x collision is true and y collision is true, then you have a full collision Yeah, it's pretty cool. Actually, so yeah, that's a a bb collision So I've got a little I got some code here that's going to test that and what I'm going to do is I'm going to show you the overlapping collision first for all three. So let's take a look at that Okay, so you can see here. I've got a wizard and a goblin. I've got a Pac-Man and a Cherry I got a ball and a paddle. I just called it a bar in my code So I'm gonna go ahead and move the goblin to the left Okay, and now this was that 128 negative 128 and the goblin is moving minus 64 pixels each time So what'll happen is it will exactly overlap at some point But you can see here the goblin is touching the wizard But there's no collision registered. I'm gonna go one more and you can see there That is a full collision because the center xy is the same as the center xy of both so the goblin and the Wizard so you can see how that collision wasn't quite accurate But if you're moving on a grid or if you're moving on Some kind of map with tiles this works really really well, and it's a very fast way of checking for collisions Now I'm gonna do the same thing with the Pac-Man Okay, and we'll see here. Okay now right now Pac-Man is touching but the collision has not registered And you can see here it actually goes right through because at no point does the center coordinate Exactly equal the center coordinate of this and I think you're gonna see the same thing down here with the ball Ball moves and it's very close to the center, but not quite and it goes right through So the overlapping coordinate method only works really well If you are on a grid system where everything moves the exact same number of pixels So it's useful in certain situations, but not in others. Okay, so I'm gonna go ahead and change this to distance collision and So let's go ahead just copy that Okay, so now I'm gonna do all three again with the distance collision and So let's try the goblin again, so we'll be measuring the distance Now this one's not gonna be too bad. I think so you can see how the collision happened even though the goblin is not Very you know on the center on top of the the wizard Let's do the same thing with Pac-Man Okay, and you see that worked pretty well as well, so it's not too too bad Now let's take a look here at the ball now This one is where we're gonna see that it's not gonna work so well and the reason is That the width of this paddle is much wider than it is tall. So watch this Okay, so even though the ball was here. I still registered a collision Because I was using the width as my measurement Okay, so but you know you just kind of intuitively see if the ball is over here The distance to the center is much greater than the distance to the center here So there's no consistent distance that we can use To find a collision with something that is this different in dimensions And for the last one, we'll do the aabb the axis align bounding box Um aabb And let's run it Okay, so again, this is aabb and let's watch that and that one works pretty well For these objects again, these are objects that are roughly the same size Can you see it didn't Pac-Man didn't trigger the collision and there we go So that works pretty well as well and now let's look at our last case and Now what's really cool is see how it doesn't register the collision here It's really really accurate and then as soon as I moved down a little bit more it did register that collision So the axis align bounding box is a really good method that works with almost any size object So it's probably the best one of the ones that we've used here But you know, you can make a choice depending on you know How your program works and the size and shape of your objects again, I didn't do the the fourth method which was You know pixel perfect just because the coding is really difficult for that and actually the Turtle module doesn't give you the tools you need to do that So I'm sorry about that and I can't I can't actually do it there It doesn't as far as I know there's no way to do it with just the turtle module Just you know some things You know, you might want to take a look at you might want to play around with moving The goblin a different speed or moving the Pac-Man at a different speed and you can kind of see where each different method Has its strengths and its weaknesses So, you know the the less this is that this was like one that would take forever to get across the screen We can probably see a little bit better the weaknesses and strengths of some of those different methods for each size Type of image so the dimensions are really important in the relative dimensions. So that is that So just going back through this we went through basic collision detection We talked about overlapping coordinates where the x and y are exactly equal Distance checking where we measure the distance between the center using the Pythagorean theorem The access a line bounding box, which is probably the best one to use that's at least pretty fast and then we had we talked about pixel-perfect collision checking and you can see again here It's a little bit of its weakness For round objects and square objects, especially or two round objects, but overall it works pretty well And you can even like make this a little bit smaller to make the collisions a little bit better For your game. So, yeah, that was kind of a quick introduction There'll be a link to the code down below so you can kind of play around with this and just kind of get an idea of how these Different methods work. So again, thanks to Kevin Paul and Jan everybody else keep on coding. Have a great day