Now, we are going to learn how to implement rectangular collision detection. It turns out that both Robo and our enemy (a water bottle) are very rectangular, making rectangular collision detection the best choice.
Let's introduce our Robo's enemy—a bottle of water to rust his gears. The code for this is included next.
Add the following sprite definition to RoboRacer2D
:
Sprite* enemy;
Now, we will setup the sprite. Add the following code to LoadTextures
:
enemy = new Sprite(1); enemy->SetFrameSize(32.0f, 50.0f); enemy->SetNumberOfFrames(1); enemy->AddTexture("resources/water.png"); enemy->IsVisible(false); enemy->IsActive(false); enemy->SetValue(-50); enemy->IsCollideable(true);
This code is essentially the same code that we used to create all of our sprites. One notable difference is that we use the new SetValue
method to add a negative value to the sprite. This is how many points the player will lose if they hit this enemy. We...