Book Image

OpenGL Game Development By Example

By : Stephen Madsen, Robert Madsen
Book Image

OpenGL Game Development By Example

By: Stephen Madsen, Robert Madsen

Overview of this book

OpenGL is one of the most popular rendering SDKs used to develop games. OpenGL has been used to create everything from 3D masterpieces running on desktop computers to 2D puzzles running on mobile devices. You will learn to apply both 2D and 3D technologies to bring your game idea to life. There is a lot more to making a game than just drawing pictures and that is where this book is unique! It provides a complete tutorial on designing and coding games from the setup of the development environment to final credits screen, through the creation of a 2D and 3D game. The book starts off by showing you how to set up a development environment using Visual Studio, and create a code framework for your game. It then walks you through creation of two games–a 2D platform game called Roboracer 2D and a 3D first-person space shooter game–using OpenGL to render both 2D and 3D graphics using a 2D coordinate system. You'll create sprite classes, render sprites and animation, and navigate and control the characters. You will also learn how to implement input, use audio, and code basic collision and physics systems. From setting up the development environment to creating the final credits screen, the book will take you through the complete journey of creating a game engine that you can extend to create your own games.
Table of Contents (19 chapters)
OpenGL Game Development By Example
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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