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

Cross product

The cross product is written as a X between two vectors, Cross product. It returns a new vector that is perpendicular to both vectors Cross product and Cross product. That is, the result of the cross product points 90 degrees from both vectors.

The cross product is defined only for three-dimensional vectors. This is because any two non-parallel vectors form a plane, and there will always exist a line perpendicular to that plane. As such, we will only be implementing the cross product for the vec3 structure.

The equation of the cross product is as follows:

Cross product

Getting ready

The formula behind the cross product seems large and complicated. We're going to implement a pattern in code that hopefully will make remembering this formula easy.

How to do it…

The cross product is only well defined for three dimensional vectors. Follow these steps to implement the cross product in an intuitive way:

  1. Add the declaration for the cross product to vectors.h:
    vec3 Cross(const vec3& l, const vec3& r);
  2. Start the implementation in vectors.cpp:
    vec3 Cross(const vec3& l, const vec3& r) {
       vec3 result;
       // We will add more code here
       return resut;
    }
  3. Start by listing out the x, y, and z components of the result in a column:
    vec3 Cross(const vec3& l, const vec3& r) {
       vec3 result;
       result.x = /* Will finish in step 6 */
       result.y = /* Will finish in step 6 */
       result.z = /* Will finish in step 6 */
       return resut;
    }
  4. Flesh out the first row by multiplying l.y and r.z. Notice how the first column contains x, y, and z components in order and so does the first row:
    vec3 Cross(const vec3& l, const vec3& r) {
       vec3 result;
       result.x = l.y * r.z /* Will finish in step 6 */
       result.y = /* Will finish in step 6 */
       result.z = /* Will finish in step 6 */
       return resut;
    }
  5. Follow the x, y, z pattern for the rest of the rows. Start each row with the appropriate letter following the letter of the first column:
    vec3 Cross(const vec3& l, const vec3& r) {
       vec3 result;
       result.x = l.y * r.z /* Will finish in step 6 */
       result.y = l.z * r.x /* Will finish in step 6 */
       result.z = l.x * r.y /* Will finish in step 6 */
       return resut;
    }
  6. Finally, complete the function by subtracting the mirror components of the multiplication from each row:
    vec3 Cross(const vec3& l, const vec3& r) {
       vec3 result;
       result.x = l.y * r.z - l.z * r.y;
       result.y = l.z * r.x - l.x * r.z;
       result.z = l.x * r.y - l.y * r.x;
       return resut; // Done
    }

How it works…

We're going to explore the cross product using three normal vectors that we know to be perpendicular. Let vector How it works…, How it works…, and How it works… represents the basis of How it works…, three-dimensional space. This means we define the vectors as follows:

  • How it works… points right; it is of unit length on the x axis: How it works…
  • How it works… points up; it is of unit length on the y axis: How it works…
  • How it works… points forward; it is of unit length on the z axis: How it works…

Each of these vectors are orthogonal to each other, meaning they are 90 degrees apart. This makes all of the following statements about the cross product true:

  • Right X Up = Forward, How it works…
  • Up X Forward = Right, How it works…
  • Forward X Right = Up, How it works…

The cross product is not cumulative, How it works…is not the same as How it works…. Let's see what happens if we flip the operands of the preceding formulas:

  • Up X Right = Backward, How it works…
  • Forward X Up = Left, How it works…
  • Right X Forward = Down, How it works…

Matrices will be covered in the next chapter, if this section is confusing, I suggest re-reading it after the next chapter. One way to evaluate the cross product is to construct a 3x3 matrix. The top row of the matrix consists of vector How it works…, How it works…, and How it works… . The next row comprises the components of the vector on the left side of the cross product, and the final row comprises the components of the vector on the right side of the cross product. We can then find the cross product by evaluating the pseudo-determinant of the matrix:

How it works…

We will discuss matrices and determinants in detail in Chapter 2, Matrices. For now, the preceding determinant evaluates to the following:

How it works…

The result of How it works… is a scalar, which is then multiplied by the How it works… vector. Because the How it works… vector was a unit vector on the x axis, whatever the scalar is will be in the x axis of the resulting vector. Similarly, whatever How it works… is multiplied by will only have a value on the y axis and whatever How it works… is multiplied by will only have a value on the z axis. The preceding determinant simplifies to the following:

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 €18.99/month. Cancel anytime