Collision Detection

10 minutes
Share the link to this page
Copied
  Completed
Without collision, there is not challenge. Let's implement collision detection to know when the bird has collided with the pipes.

Transcript

Hello and welcome to the temp tutorial in the folders to the x lab c++ series and in this part we're going to be looking at collision detection. This will be carried on from the previous part so if you don't have it they'll be a link in the description to the source code. Now we have a character that for who grabbed it and pop stars born and moves we will check for collision detection between the character and the edges aka top or bottom because he can't move left or right technically and collision between the character and the pipe. Firstly, we will create a couple of collision bit masks in the definitions file. So let's go to the far right here we do hash find bird on the scope collision on the scope bit mask and you can put like a regular integer for this but we're just gonna put a heck so there are x there there are there are just the key thing is here just keep him unique or less warm.

Certain collision bit masks to be the same as you want the same core functionality, but generally try and keep them you need. Also no griping about definitions or you can just have a quick scan over and see Oh yeah, these are the same or these aren't same this is how we should be instead of going through this scene and nothing in that class in that class. And this will finish itself on the school collision, school bit mask 02 Now we will assign the collision bit max bit masks to the bird and obstacle and set the contact test pitmaster true so collision can be detected with that particular item. So since we're just gonna go to a bird, where we create the body right here, we're gonna do flappy body. sec collision bitmask. And for this, we're just gonna do what's called collision bit math.

To floppy body, contact test bitmap. technique number integer perfect truth it just collides with everything or you can pick up collisions with our fingers should say can detect them. So you can say when these two have collided, do a particular piece of code. So now we're going to go to our pipe. So right here we're going to do top pipe body fat collision bitmap, I'm going to do obstacle collision bit mask and we'll copy and paste that. I will change this to bottom number one top point body.

Set contact tested math to true. We'll just copy and paste this. We'll change it to bottom point what is the top point body and then the last collision bit master we need to set is the edge so that you can gain seen And it's created right here, so produce a body collision mask. And for this we're gonna put obstacle, mass and body contact test bed mass we're going to predict true now that this is also true, we will declare the on contact begging method in the game scene. And so if we just go to a game single page, we can keep it private. Go on, contact begin.

There are other methods for collision like on contact, separate etc. But we just need the on contact begin to deform Kona physics, contact con. Now we're simply going to copy paste this to a game scene dot cpp we will do is just created here we won't implement the functionality just yet. We are now going to set up the contact listener in the game cpp file so we can well listen for context between the physical body so we just go here, it's gonna print here, auto contact listener, equals event listener. Physics contact closing column create semi colon, contact listener. On contact became equals cc underscore callback.

I'm going to do underscore callback one. The method takes one parameter jamesy on contact begin. And then for the target, we just simply specify this There we do director Conan Conan get instance, get a better dispatcher. event listener with scene graph priority. And then for the listener will just simply specify contact listener. For the No, we just put this semi colon.

The last step is to well implement the contact begin method. So we can actually detect when collisions have occurred and food scene but we also need to include the game of the scene header file so we can actually access that theme was going to hash include at the top, came over the scene. Go back to this one, and then there was some extra space news. I can see it but uh, and we'll go to this method here. What we'll do is fix body As quickly respond creating Murphy's policy with contact dot get shape a good body of the contact has two body basically the two that collided at that particular time. You can copy and paste it and this change a to b chain this to be as well.

Now it's not check if they have collided so if in here we're gonna do two checks. So we're going to do the Muskoka little bit mask equals A get collision bit mask and obstacle collision bit mask equals equals b. Get collision bit mask. What we're also going to do what we can Do it or copy and paste this. And the one the only thing you need to change is flip the A and the base a B to A, we could also just simply flip the bird collision but not on the obstacle collision boot mask because we don't know if the first physics body aka is going to be the ball, and B is going to be maybe the pipe or perhaps ain't going to be the pipe and be the ball. So we need to check for both instances.

And now in here, what we're going to do is auto scene, people gain overseen create scene, then the director can get instant replay scene and then we'll try and fish feed Hold on calm, create the duration and put transition underscore time for the scene as specified on the scene that we just created. We're not gonna have any color when we fade it out or not any particular color globally. Now the only one last thing to do is return true here. True. Now, let's run this sort of simpler law, and that the rights reserved wherever it is, and how I got that on there. And that was funny.

And what we're hoping for is we have the bird and if we wait for that hits the ground, the player dies. And if it hits the point to play, though, they will hit the ground first. And we'll just disable the ground collision in a moment that we can test the plug collision as well. But that's easy. You just go up and we'll show you in a moment. To do that, we click play, we have this folder, it's the water, I never switched things whenever we also want to see if it works with a pipe.

So what we can do if we go to our edge body here where we put second test bitmask for treatment false. What happened is we won't pick up the collision between it so used to bounce off it and so forth to do all that other good stuff, but it just won't pick up the collision. So we click play as you can see, but when he hit the point, it did switch. Switch this back to truth. That is it for this pattern series. In the next part, we're going to be looking at making our bird actually fly.

If you have any questions feel free to message us at support at solar systems coda UK email will be in the description you can comment on this video or just directly messages via YouTube or the link for source code will also be in the description. As usual, thanks for watching and I hope you have a great day.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.