Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Game Physics Cookbook

You're reading from   Game Physics Cookbook Discover over 100 easy-to-follow recipes to help you implement efficient game physics and collision detection in your games

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781787123663
Length 480 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Gabor Szauer Gabor Szauer
Author Profile Icon Gabor Szauer
Gabor Szauer
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Preface 1. Vectors FREE CHAPTER 2. Matrices 3. Matrix Transformations 4. 2D Primitive Shapes 5. 2D Collisions 6. 2D Optimizations 7. 3D Primitive Shapes 8. 3D Point Tests 9. 3D Shape Intersections 10. 3D Line Intersections 11. Triangles and Meshes 12. Models and Scenes 13. Camera and Frustum 14. Constraint Solving 15. Manifolds and Impulses 16. Springs and Joints A. Advanced Topics 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….

You have been reading a chapter from
Game Physics Cookbook
Published in: Mar 2017
Publisher: Packt
ISBN-13: 9781787123663
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image