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

Dot product

The dot product, sometimes referred to as scalar product or inner product between two vectors, returns a scalar value. It's written as a dot between two vectors, Dot product. The formula for the dot product is defined as follows:

Dot product

The sigma symbol Dot product means sum (add) everything up that follows. The number on top of the sigma is the upper limit; the variable on the bottom is the lower limit. If n and i is 0, the subscripts 0, 1, and 2 are processed. Without using the sigma symbol, the preceding equation would look like this:

Dot product

The resulting scalar represents the directional relation of the vectors. That is, Dot product represents how much Dot product is pointing in the direction of Dot product. Using the dot product we can tell if two vectors are pointing in the same direction or not following these rules:

  • If the dot product is positive, the vectors are pointing in the same direction
  • If the dot product is negative, the vectors point in opposing directions
  • If the dot product is 0, the vectors are perpendicular

How to do it…

Follow these steps to implement the dot product for two and three dimensional vectors:

  1. Add the declaration for the dot product to vectors.h:
    float Dot(const vec2& l, const vec2& r);
    float Dot(const vec3& l, const vec3& r);
  2. Add the implementation for the dot product to vector.cpp:
    float Dot(const vec2& l, const vec2& r) {
       return l.x * r.x + l.y * r.y;
    }
    
    float Dot(const vec3& l, const vec3& r) {
       return l.x * r.x + l.y * r.y + l.z * r.z;
    }

How it works…

Given the formula and the code for the dot product, let's see an example of what we could use it for. Assume we have a spaceship S. We know its forward vector, How it works… and a vector that points to its right, How it works…:

How it works…

We also have an enemy ship E, and a vector that points from our ship S to the enemy ship E, vector How it works…:

How it works…

How can we tell if the the ship S needs to turn left or right to face the enemy ship E?

We need to take the dot product of How it works… and How it works…. If the result of the dot product is positive, the ship needs to turn right. If the result of the dot product is negative, the ship needs to turn to the left. If the result of the dot product is 0, the ship does not need to turn.

There's more…

Our definition of the dot product is fairly abstract. We know that the dot product gives us some information as to the angle between the two vectors, There's more… and There's more…. We can use the dot product to find the exact angle between these two vectors. The key to this is an alternate definition of the dot product.

Geometric definition

Given the vectors Geometric definition and Geometric definition, the geometric definition of the dot product is the length of Geometric definition multiplied by the length of Geometric definition multiplied by the cosine of the angle between them:

Geometric definition

The || operator in the above equation means length and will be covered in the next section. We will cover the geometric definition and other properties of the dot product later in this chapter.

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