Book Image

Learning Game Physics with Bullet Physics and OpenGL

By : Chris Dickinson
Book Image

Learning Game Physics with Bullet Physics and OpenGL

By: Chris Dickinson

Overview of this book

<p>Physics simulation is an integral part of almost all game development projects as it is essential to the rules and feel of the game (gameplay) regardless of the project&rsquo;s scale. Bullet is a 3D Collision Detection and Rigid Body Dynamics Library for games, and special effects for film and animations. Bullet is integrated into many 3D modelers including Maya, Houdini, Cinema 4D, LightWave, and Blender. It is free for commercial use and open source under the permissive ZLib License.</p> <p>A comprehensive guide to start building games with the Bullet Physics library. Learn how modern physics engines work by implementing key features such as collision event systems, user input handling, and simulation of soft bodies. Then learn to control it all with forces, constraints, and robust object management. This book will reveal what&rsquo;s going on under the hood of two modern and feature-rich graphics and physics APIs; OpenGL and Bullet Physics.</p> <p>This book begins by teaching you to write your first OpenGL application, and then dives in to exploring the many features of the Bullet library in a straightforward manner. Each new feature expands upon the last, teaching you more about how physics is simulated in a video game, and how Bullet gives you the power to control every aspect of your simulation. You will learn how to render simple and complex shapes, apply some basic lighting, and construct a simple yet robust rendering system. From here, you will pull back the veil to see what&rsquo;s going on underneath Bullet Physics, and learn to implement key game logic features through this widely-used and extensive physics library. After you finish this book, you&rsquo;ll be armed with a wealth of knowledge to tackle the more advanced aspects of game graphics and physics going forward.</p>
Table of Contents (16 chapters)
Learning Game Physics with Bullet Physics and OpenGL
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Modern 3D graphics and game physics can seem like complex and confusing elements of game development from the outside, but this book will reveal what's going on under the hood of two modern and feature-rich graphics and physics APIs: OpenGL and Bullet physics. After you finish this book, you'll be armed with a wealth of knowledge to tackle some of the more advanced aspects of game graphics and physics going forward.

This book can't hope to show all of the concepts and intricacies of modern physics and 3D graphical rendering, but it will cover all of the fundamentals in enough detail to let you hit the ground running when you take on future challenges. And if those challenges involve building an application with the Bullet physics library, then all the better, because you will also learn exactly how this library works from the ground up and help you focus on only the important parts of what you need to know about simulating game physics.

What this book covers

Chapter 1, Building a Game Application, identifies the files and libraries required to incorporate the FreeGLUT and Bullet libraries into a starter project, and how to build an application layer to communicate with the operating system.

Chapter 2, Rendering and User Input, introduces some core 3D rendering concepts, implements our very first graphical object complete with lighting and color, and adds user input to our application to control the scene's camera.

Chapter 3, Physics Initialization, introduces the essential concepts of Bullet and the core objects required to build our physics simulation, and attaches a physical rigid body to our graphical object, observing how physics and graphics work together to create a simulated world.

Chapter 4, Object Management and Debug Rendering, runs through some essential refactoring of the code in order to better handle multiple objects, and adds debug rendering to our scene, enabling us to visualize essential information from the physics engine.

Chapter 5, Raycasting and Constraints, introduces the flexibility of raycasting in finding, creating, and destroying objects, and will show us how to add limitations to the motion of our physical objects, allowing even greater control of the objects in our simulation.

Chapter 6, Events, Triggers, and Explosions, implements a simple and effective method for extracting collision event information out of Bullet, builds a basic trigger volume that can trigger these events, and demonstrates the power of these features by simulating an explosion.

Chapter 7, Collision Shapes, introduces several new types of physical object and methods for rendering them from basic spheres and cylinders to shapes built from any arbitrary list of points.

Chapter 8, Collision Filtering, implements a means of separating unwanted contact responses through a simple filtering method.

Chapter 9, Soft Body Dynamics, provides a brief look at complex soft body shapes and their requirements, and implements one into our scene.

What you need for this book

An intermediate level of understanding of the C++ language is required for this book as it is not a programming tutorial, but rather an exploration of existing APIs that have already been through countless hours of development. Also, a working knowledge of 3D mathematics is essential as it is assumed that you have a good understanding of concepts such as vectors and matrices, and how they can be used to represent a 3D space.

A C++ compiler is necessary to compile the book's source code applications. This book uses Visual Studio as a reference, and the source code comes with the Visual Studio solution files. Note that Visual Studio Express can be downloaded from the Microsoft website for free, and it has all of the features necessary to compile the source code and complete this book.

Finally, the Bullet and FreeGLUT libraries will be used, but since they are open source software, they can be freely downloaded from their project websites, which will be explained in Chapter 1, Building a Game Application.

Who this book is for

If you're a beginner or intermediate programmer with a basic understanding of 3D mathematics and you want a stronger foundation in 3D graphics and physics, then this book is perfect for you! Learning Game Physics with Bullet Physics and OpenGL will take you through a series of straightforward tutorials until you have a strong foundation in both APIs. You'll even learn some of the fundamental concepts in 3D mathematics, and software design that lies beneath them both, discovering some techniques and tricks in graphics and physics that you will use in any game development project.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "The glutKeyboardFunc and glutKeyboardUpFunc functions are called when FreeGLUT detects that a keyboard key has been pressed down or up, respectively."

A block of code is set as follows:

int main(int argc, char** argv)
{
  BulletOpenGLApplication demo;
  return glutmain(argc, argv, 1024, 768, "Introduction to Game Physics with Bullet Physics and OpenGL", &demo);
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

DrawBox(btVector3(1, 1, 1), btVector3(1.0f, 0.2f, 0.2f));

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "To run a different project, right-click on one of the projects, and select Set as StartUp Project."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: http://www.packtpub.com/sites/default/files/downloads/1879OS_ColoredImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected \pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.