Introduction If you are coming from the Dot Product math tutorial, I thank you for going through it! The understandings of the previous tutorials are just the tools in your toolbox to becoming a game developer. The cross product is just another one of these tools! Let’s see what the cross product can tell us and how it can be […]

Introduction

If you are coming from the Dot Product math tutorial, I thank you for going through it! The understandings of the previous tutorials are just the tools in your toolbox to becoming a game developer. The cross product is just another one of these tools! Let’s see what the cross product can tell us and how it can be useful in practice!

What is a Cross Product?

This is one of the other most important tools that game developers might need in their toolbox. The cross product is a mathematical operation applied on two input vectors that returns a perpendicular vector in relation to the two input vectors. A perpendicular vector, sometimes known as a normal vector, is a vector that is at exactly 90 degrees from another.

Below we see that the green vector is perpendicular to both the red and blue vectors respectively known as vector A and vector B.

How to Calculate the Cross Product?

There are a lot of ways to calculate the cross product. To be honest no one really cares how you do it as long as you do so I will show you one way. DO NOT memorize this. No one cares if you know how to calculate it. They care if you know what it means. I am showing you one way to calculate it just for the purposes of this being a tutorial.

Let vector A be defined as <a1 a2 a3> and let vector B be defined as <b1 b2 b3>. Let the perpendicular vector be defined in terms of S as <s1 s2 s3>.

So given your two input vectors being A and B, we get the perpendicular vector S. Most mathematicians shorthand this equation to operation seen as a cross. It overloads the X symbol. So the above looks like S = A X B, which is generally how you will see it. This does not mean multiplication! It means the cross product! I guess it made sense to them at the time but they forgot the multiplication symbol existed or something.

Some Rules to Know About The Cross Product?

There are 6 rules about the cross product and you should be a good game developer and know them all but I won’t hold you to it. Just remember the first and third rule.

First of all, the order of the vectors of the cross product totally matters! U X V does not equal V X U! That is the simplicity of it. The first rule tells us something important. V X U is also a perpendicular vector but in the opposite direction of V X U. This makes sense since there are two possible vectors that can be perpendicular two the input vectors of U and V. One that goes in one direction and the other that goes in the opposite!

The third rule is just showing us the distribution property! Notice that U is being distributed to both v and w. The key thing here is that U is on the left of V and left of W! The order of the vectors of the cross product matters! If you don’t remember these two rules, just remember the order of the cross product matters! Please!

Direction of the Cross Product

Let’s quickly talk about the direction of the perpendicular vector. There isn’t something in doing the cross product that tells you the direction of the perpendicular vector! I know right. But our big boy mathematicians have got you covered. It is called the right-hand rule!

Your index and middle finger show your two input vectors A and B respectively. Your thumb shows you the perpendicular vector and its direction!

Last thing to visualize the direction. Imagine curling your hand from the direction of the A vector towards the B vector. The thumb in this case will also show you the perpendicular vector. Now if in the right-hand rule, the a and b vector were to swap, how will your hand curl? If you understood this correctly, then we would see that the thumb is pointing down and we are curling the hand upside down from the A vector to the B vector.

What Does The Cross Product Tell You?

The cross product returns a perpendicular vector to the two input vectors. We know that. So why is that important? Well for one, this is used to figure out the normal of a triangle! If you imagine a triangle being defined with 3 points respectively labeled as ABC, we can figure out the perpendicular vector of that triangle. This is one of the primary use cases of the cross product. So let’s see how we do this. We first create a vector AB and vector AC from simple vector rules. Remember to make vector AB we subtract B – A. Then we take the cross product between the vectors AB and AC. The resultant vector is the normal vector to the triangle!

The magnitude of the cross product tells us the area of the parallelogram spanned by the two vectors of the cross product. Not many people know this one but it actually came up in an interview and I nailed it so I am writing about it here.

So notice how vectors A and B can create a parallelogram. The area of that is ||A X B|| or the magnitude of A cross B.

∥a∥ ∥b∥sinθ is another way to calculate the area of the parallelogram. Notice how if the angle between vector A and vector B decreases. the area will shrink while if they are perpendicular you will have the largest area that a parallelogram can be! That means the sin of the equation controls the area’s scalability. If θ is 0, the area will be 0! If θ is 90, the area will be at its largest! You won’t need to memorize this but it is a cool thing to note!

One Last thing you want to note! What happens when you cross product two vectors and you get the 0 vector, what does that mean, specifically what does that tell you about the two input vectors?

It means that the two input vectors were parallel or colinear to each other!

Some of the things I can think of are pseudo-attraction forces, physics, and maybe wall-running techniques. I am coming with these on the spot so I am just going to talk about them and how I am coming up with them.

Pseudo-Attraction Forces

This is just an idea but bear with me. Imagine you have the ability to stick to a specific object’s face, so with a click of a button, you maybe can get dragged towards an object in the sky or some platform. We can calculate the normal of the face of the triangle that we care about. Then we can reverse the direction of that normal and apply a force to our character in the opposite direction of the normal! So now your player is moving in the direction of that face! This one mechanic can be the whole idea of a phone game. Grab the normal of a face and traverse a level using random objects’ faces to get through the level! Ok, that was kinda cool. Oh, this might be how we can do grappling hooks but there is probably a better way for that.

Physics

Working on any game that wants to simulate rotational forces, we need to go through a property known as torque. Torque is a measure of how much a force acting on an object causes that object to rotate. So you might want this to simulate real-life forces of applying to an object to make that object rotate.

Wall-Running

Again this is another idea and I totally don’t know if this works but I just want to simulate the idea of maybe a way we can use the cross product. You can imagine that a wall has a face and consecutive walls that are connected but slightly rotated also have faces.

So imagine seeing something like this in a game and you want to wall run on it. You can use the normals of these walls to apply the force of the player towards the opposite direction of the normals of these wall faces. That way the player is sticking onto the wall regardless of the orientation of the wall. One thing we might want to do to polish this idea is to check the angle between the two walls, and if it is a very small angle then stop the wall running. This allows us to stop wall-running around corners or something. Again this is just a rough pass on an idea I came up with on the spot.