Rectangular collision detection

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.

The enemy within

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 ...

Get OpenGL Game Development By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.