Cross Product Uses?

Ive come to understand that the dot product is the similarities between however many vectors and the cross product is the difference between however many vectors

But what Im confused on is anyway I can really put it too use in Roblox

The dot product and cross product are two of the most versatile operations when working with vectors. the dot and cross product are to vectors like addition and multiplication are to scalars.

The cross product is used for three different reasons:

  1. Finding perpendicular vectors. Lets say you have two vector, and need to find a vector perpendicular to both. Just use the cross product to get that third vector. If you only have one vector and need to find a perpendicular, you can use a constant vector, like (1,0,0), and cross it with that. (Be sure to check if the vector you’re testing isn’t collinear with the constant vector). This application is especially useful when dealing with torques, angular velocity, and angular acceleration, which all point normal to the plane of rotation.

  2. Calculating orientation. the cross product can be used to determine where the rotation between two vectors is clockwise when viewed from. Because roblox uses a left-handed coordinate system, if you were to look at your pair of vectors at the position of their dot-product, the rotation between the first and second will be clockwise. If the vectors are the other way around, the cross product would be pointing in the opposite direction to make it point clockwise.

  3. Finally, the cross product is commonly used to find area and volume. Because the cross product is the sine of the angle of the vectors multiplied by the magnitudes of the vectors, the magnitude of the cross product will give the area of the parallelogram that includes both those vectors as sides. To find area of the triangle that the vectors create, just divide the magnitude by 2. The cross product can also tell you volume. if you take the cross product between two vectors, the resulting vectors has the property that if you dot it with any other vector, it will give the volume of the parallelepiped created by the two original vectors and the third dotted vector. It can be used to determine how much a specific matrix transformation stretches or squishes space.

These are just the 3 main ideas that come to mind when working with the cross product, but these show up everywhere in vectors, so it’s a good tool to have.

1 Like

But what can I use it for in roblox though?

Let’s say you want a something to spin between two positions relative to itself. if you have those positions as vectors u and v, to get an angular velocity vector that points in the correct direction, use u:cross(v), and then change the magnitude of the resulting vector for speed.

Let’s say if you want to test if two vectors are pointing in the same direction, one way to do this is to normalize the vectors and take the dot product, and check if the answer is 1 or -1. However, the process is much simpler with cross products, as the magnitude of the cross product is the area of the parallelogram the vectors make, just check if the cross of the vectors is (0,0,0), no normalizing or multiple valid results to check, because if the vectors point in the same direction, the parallelogram will have no area.

Lets say you have three points, and need to find the normal of the plane they create (maybe to orient the up vector of a part or the camera based on where other parts are). pick one point as the new origin, subtract that point from the other two points to make them relative, then take the dot product of those two new relative positions to get the normal.

All this is really great stuff and I like the thought of getting the normal using the cross product, but I still cant really wrap my head around why I would use the cross product since I dont think ill try to be ever getting an angular velocity vector

Also about checking if the vectors point in the same direction why would I use the cross product over the dot product for that when normalizing the vectors is just as easy as putting .Unit behind one of them? Does one offer less calculations and therefore boost performance by a margin?

The cross product is easier to calculate than the normal of the dot product. This is the calculation of the normal of the dot product with vectors u and v:

ux/sqrt(ux^2 + uy^2 + uz^2) * vx/sqrt(vx^2 + vy^2 + vz^2) + uy/sqrt(ux^2 + uy^2 + uz^2) * vy/sqrt(vx^2 + vy^2 + vz^2) + uz/sqrt(ux^2 + uy^2 + uz^2) * vz/sqrt(vx^2 + vy^2 + vz^2) 

divisions and square roots are a lot expensive to a CPU than multiplication and addition. Just the crossproduct, however, is just this calculation:

(uy*vz - uz*vy, uz*vx - ux*vz, ux*vy - uy*vx)

Just multiplication and addition (subtraction is basically just addition but with negatives). Also, the conditional is slightly simpler with the crossproduct because you’re only checking Cross = 0, instead of Math.abs(Dot) = 1.

For the normal stuff, if you’re working with CFrames, it’s important to know. All CFrames have three vectors that are perpendicular with each other, and the cross product takes two vectors and makes another vector perpendicular to the two inputs (Be aware, the inputs don’t have to be perpendicular to each other). This makes the cross product extremely useful when constructing and modifying CFrames.

1 Like

Thanks for taking time to explain all this about the cross product to me, I’ll probably be revisiting this post occasionally if I forget anything, thanks alot!