Hello and welcome to another episode in the Godot basics tutorial series. This episode we will be taking a look at the theory of collisions. Now a collision is a Short duration interaction between two bodies causing change in the motion of bodies.
This would be the physics terminology of what a collision is. However in game programming when we say collision we really care about is that when two bodies intersect. We would like to do something we would like to create. An action.
And in most cases that action is to make sure that our objects do not intersect. In Game Programming collisions are based on shapes these shapes can be as simple as squares rectangles Intuit in 3-D. They can be as simple as. Cylinders. Or. We can get into more complex shapes such as polygons in game programming collisions are detected through algorithms. These algorithms can be. Simple
Or complex. Let's go ahead and take a look at a simple collision detection.
So in this example we'll be using rectangles. As you can see here we have our origin. We have rectangle 1 positioned at 10 and 10 and rectangle to position that 30 10 both rectangles have a width of 10 pixels and a height of five pixels. Now this algorithms very simple. We're just checking to make sure that either our X or Y position when added to their respective width the right is in fact intersecting with the other game object's position with respect to the x or y position.
Let's go ahead and check the algorithm in regards to the x dimension. What I will be explaining also applies to the Y dimension for this algorithm as well. So understanding what we're doing to one dimension allows us to understand what we're doing to the other dimension. Now in this case we're taking one of our game objects x position in this case rectangle 1 and we're comparing it to the other rectangles x position and adding the value of its weight. And on top of that we're also checking the other rectangles position in this case rectangles to exposition and we're checking that against rectangles one x position and adding it with value to that.
In this case we're going to throw false and quite simply the reason for that is because when we're checking the x position plus the width what we're saying is in regards to the exposition that if we take our rectangle one's position 10 and we add its width which is 10 this new value becomes 20. And so we're checking is 20 greater than rectangles to exposition which is 30. So again is 20 greater than 30 in this case. No 20 is not greater than 30. And therefore rectangle 1 is not intersecting with rectangle 2.
And in this case where we're going to throw the error is in the second evaluation. Rectangle 1 plus rectangle 1 which is not in fact greater than rectangle 2s X position. And this is apparent when we actually add all the values as you can see here since we're checking rectangle to first forty is in fact greater than ten over where we throw the error is when we check rectangle one x position plus the width in this case that will be twenty and twenty is not greater than rectangle 2s x position which is 30.
So again twenty is not greater than 30 therefore we do not have a collision. Now the reason we write checking for both positions in this case checking against rectangle 1 and rectangle 2 is because we do not know in fact where exactly our rectangle in this case rectangle 1 will be positioned in regards to rectangle 2.
So let's go ahead and take another look.
Now imagine if rectangle 1 is now 50. Now keep in mind the error was on the second check and notice now that our second check or in this case our second evaluation of our if statement is in fact true because now rectangle ones exposition plus width in this case sixteen is in fact greater than rectangle two's exposition which is still thirty. However when we check rectangle 2 in regards to rectangle 1 what we find out is that rectangle 2 which is 30 plus its width which is 10 which becomes forty is not in fact greater than rectangles one position which is 50.
So again rectangle 2 which is 40 is not greater than 50.
And so in this case or evaluation for the if statement will fail on the first evaluation.
Also keep in mind that we are using the logical operator and in this case the double M percent and what we're saying is that everything needs to be true and as a matter of fact if everything evaluates to true we are in fact intersecting. Let's go ahead and take a look at another example. So in this case our rectangle 1 is exactly 40. In this case 30 plus 10 equals 40. But notice that our comparison operator we are not using the equal to or greater one
Symbol we are in fact making sure that we need to be exactly greater. So in this case we fell in the first evaluation because 40 is not greater than 40. And the reason algorithms have something like this is because we do want to have that perfect alignment.
In this case we don't want to throw an error or rather we don't want to create some action when they're just barely intersecting because for example when you have a player that's walking on ground you would like to see the player's feet actually walking on ground. However we do in fact care when we go beyond that. So for example in this case because we're actually intersecting you'll notice that when we plug in all the values everything evaluates to true and we run our action in this case we say that a collision has happened.
Also keep in mind that everything is pseudocode. Because I just want to show how collision detection works at a lower level. Now even though the previous example was an over exaggeration what's really going to happen is when we barely go past the intersection point in this case instead of being 40 we're thirty nine. What we say is that we've intersected and therefore because we have intersected we would like something to happen. And in most cases we just want to make sure that we don't move forward.
And so we have code that keeps us from going inside of other game objects.
Now one thing to note is that collision detections are naturally expensive when done naively now for two game objects this would be fine. However imagine if we had more game objects for example. If we have this game object here we have to check against every game object on screen and then after we're done with that we have to go to another game object and check everything on screen and we got to rinse and repeat that for every game object on the screen. And that is something we don't want to do that will in fact cause performance issues as your game objects grow
In number. Now lucky for us there are performance ways to handle collision. One common way is by separating our collisions in two phases the first phase is the broad phase and then after that is the narrow Phase the first phase.
Basically we are checking which game objects are close to each other. And then when we find out which game objects are in fact close to each other we move to the narrow phase. And in the narrow phase we actually run the collision algorithm against game objects that are
In close proximity to each other. Now that we have this new way of handling collision we can group things together and we can say OK we're only going to check collision between these two because they're close together.
However we're not going to check the collisions inside our circle against other game objects and the same thing for everything grouped together in this case we're going to check everything against each other here and our game object off to the side won't check against anything because it's not close to anything.
This is an oversimplification of broad and narrow phase. However you can see here that we basically increased performance by doing that.
And of course there are other different types of collision detections different algorithms that speed up performance. However we will not go over that in this series. One last thing. So far our. Algorithm has only been checking against rectangle and rectangle collision. However. If we were to decide to detect rectangle and circle or circle and circle perhaps even circle on polygon the algorithms will change. As a matter of fact different collision detection is required different. Algorithms. Now lucky for us Godot
Actually handles collision detection. Under the hood so we don't have to worry about it. As long as we tie the appropriate collision object to our game objects. The Gordo. System will in fact run its algorithms for us. And so we don't have to worry about performance. We don't have to worry about the collision detection algorithms. Ever I thought it was a good idea. To show you. How. Some collision detection algorithms. Work when you have to implement them yourselves so you can appreciate the fact that the DOH. Or rather all game engines handle this for us.
Thank you so much for joining me. If you have any questions or comments please feel free to leave them in the comments section down below. Thank you for subscribing and thank you for clicking the Like button. I look forward to seeing you in the next episode. Have An amazing day.