Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
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

Angles

We have had a brief introduction to the angle between vectors when we discussed the dot product and the magnitude of a vector. In this recipe, we will discuss how to find the actual angle between two vectors. The formula to find angle theta between two vectors is:

Angles

Getting ready

We have already implemented both the dot product and magnitude functions for vectors; this means we have everything needed to find the angle between two vectors already written. In general, this is a very expensive function, as it performs two square roots and an inverse cosine. Because it's such an expensive function, we try to avoid it whenever possible.

We can save a little bit of performance if, instead of multiplying the length of both vectors, we multiply the squared length of the vectors and then do just one square root operation on the result.

How to do it…

  1. Add the declaration of the angle function to vectors.h:
    float Angle(const vec2& l, const vec2& r);
    float Angle(const vec3& l, const vec3& r);
  2. Provide the implementation of the angle function in vectors.cpp:
    float Angle(const vec2& l, const vec2& r) {
       float m = sqrtf(MagnitudeSq(l) * MagnitudeSq(r));
       return acos(Dot(l, r) / m);
    }
    
    float Angle(const vec3& l, const vec3& r) {
       float m = sqrtf(MagnitudeSq(l) * MagnitudeSq(r));
       return acos(Dot(l, r) / m);
    }

How it works…

This formula relies on the geometric definition of the dot product:

How it works…

This formula states that the dot product of two vectors is the cosine of the angle between them multiplied by both of their lengths. We can rewrite this formula with the cosine being isolated if we divide both sides by the product of the lengths of How it works… and How it works…:

How it works…

We can now use the inverse of cosine, the arc cosine (acos), to find the angle theta:

How it works…

There's more…

The acos function we used to find the angle between vectors comes from the standard C math library. This implementation of acos returns radians, not degrees. It's much more intuitive to think of angles in terms of degrees than radians.

Radians and degrees

Add the following macros to the top of the vectors.h header file:

#define RAD2DEG(x) ((x) * 57.295754f)
#define DEG2RAD(x) ((x) * 0.0174533f)

Using these macros you can convert between radians and degrees. For example, if you wanted to get the angle in degrees between vectors Radians and degrees and Radians and degrees, you could use the following code:

float degrees = RAD2DEG(Angle(A, B));

If you are interested in the math used to derive these numbers, I suggest watching the following Khan Academy video:

https://www.khanacademy.org/math/algebra2/trig-functions/intro-to-radians-alg2/v/introduction-to-radians

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