Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Game Physics Cookbook
  • Toc
  • feedback
Game Physics Cookbook

Game Physics Cookbook

By : Gabor Szauer
4.3 (4)
close
Game Physics Cookbook

Game Physics Cookbook

4.3 (4)
By: Gabor Szauer

Overview of this book

Physics is really important for game programmers who want to add realism and functionality to their games. Collision detection in particular is a problem that affects all game developers, regardless of the platform, engine, or toolkit they use. This book will teach you the concepts and formulas behind collision detection. You will also be taught how to build a simple physics engine, where Rigid Body physics is the main focus, and learn about intersection algorithms for primitive shapes. You’ll begin by building a strong foundation in mathematics that will be used throughout the book. We’ll guide you through implementing 2D and 3D primitives and show you how to perform effective collision tests for them. We then pivot to one of the harder areas of game development—collision detection and resolution. Further on, you will learn what a Physics engine is, how to set up a game window, and how to implement rendering. We’ll explore advanced physics topics such as constraint solving. You’ll also find out how to implement a rudimentary physics engine, which you can use to build an Angry Birds type of game or a more advanced game. By the end of the book, you will have implemented all primitive and some advanced collision tests, and you will be able to read on geometry and linear Algebra formulas to take forward to your own games!
Table of Contents (19 chapters)
close
18
Index

Reflection

One of the most important concepts in physics for games is collision response and how to react to a collision occurring. More often than not this involves one of the colliding objects bouncing off the other one. We can achieve the bounding through vector reflection. Reflection is also heavily used in many areas of game development, such as graphics programming, to find the color intensity of a fragment.

Given vector Reflection and normal Reflection, we want to find a vector Reflection that is Reflection reflected around Reflection:

Reflection

The reflected vector Reflection can be found with the following formula:

Reflection

Keep in mind, in the preceding equation, Reflection is a unit length vector. This means that theReflection part of the equation actually projects Reflection onto Reflection. If Reflectionwas a non-normalized vector, the preceding equation would be written as follows:

Reflection

Getting ready

Implementing the preceding formula is going to look a little different, this is because we only overloaded the vector scalar multiplication with the scalar being on the right side of the equation. We're going to implement the function assuming Getting ready is already normalized.

How to do it…

Follow these steps to implement a function which will reflect both two and three dimensional vectors.

  1. Add the declaration of the reflection function to vectors.h:
    vec2 Reflection(const vec2& vec, const vec2& normal);
    vec3 Reflection(const vec3& vec, const vec3& normal);
  2. Add the implementation of the reflection function to vectors.cpp:
    vec2 Reflection(const vec2& vec,const vec2& normal) {
       float d = Dot(vec, normal);
       return sourceVector - normal * (d * 2.0f );
    }
    
    vec3 Reflection(const vec3& vec, const vec3& normal) {
       float d = Dot(vec, normal);
       return sourceVector - normal * (d * 2.0f);
    }

How it works…

Given How it works… and How it works…, we're going to find How it works…, which is the reflection of How it works… around How it works…:

How it works…

First, we project How it works… onto How it works…, this operation will yield a vector along How it works… that has the length of How it works…:

How it works…

We want to find the reflected vector How it works…. The following figure shows How it works… in two places, remember it doesn't matter where you draw a vector as long as its components are the same:

How it works…

Looking at the preceding figure, we can tell that subtracting How it works… from How it works… will result in How it works…:

How it works…

This is how we get to the final formula, How it works….

bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete