# Vector Cross Product Uses?

So I came across vector cross products again today (after forgetting they exist for a few years) and realized that they work in a similar way as dot products do. The latter are known to be extremely useful in a wide variety of cases which made me wonder about the cross products.

The way I understand is that it gives you a vector up or down depending on where the a and b vectors are in relation to each other. I’m not sure what happens if they’re not in the same plane though.

So, do you guys know any good uses or tricks where a cross product comes in handy?

Exactly two vectors are always in one single plane. What do you mean?

Right :chimpanzee_facepalm:, of course. I was thinking in world coordinates. I suppose the result vector just ends up pointing up or down in that local plain.

Yep BTW: The dot product is related to the cosine between two vectors, and the cross product is related to the sine between two vectors and the area of the parallelogram that is made of these two vectors.

The wiki page did mention something about the parallelogram, but I didn’t think it was important. I suppose you could do some speedy surface area calculations? Probably isn’t that much faster than doing the regular calculations though.

You mainly use the dot product to get the angle between to vectors (e.g: How much is this surface facing the sun → dot product of face normal and lighting direction)

You use the cross product to get a vector which is perpendicular to vectors a and b. You can use this to calculate a normal if you know two points of your surface (e.g. a triangle, you have three vectors to choose from).

You usually start with a tangent vector which well, follows the line, triangle or whatever. Then you have to pick the right normal vector from an unlimited set of possible vectors. Then you can use both with a cross product to generate the binormals (That’s what a TangentBinormalGenerator is for :)).

Then you have a three-axis, model relative local coordinate system to operate on.

Do you want to calculate the area of a polygon or what do you mean by surface area calculation?

1 Like

Hmmm… Let’s put it this way. If you ever want to calculate a surface area, either the parallelogram’s or the triangle’s area, and you have the two vectors already, then you have two choices:

I. Calculate the vectors’ length (requires the square root), calculate the height of the parallelogram (requires the sine) and multiply these two values.
The square root and the sine will be numerically calculated using a Taylor expansion, where each involves numerous multiplications with certain values, e.g.: II. Use the cross product, it only requires 6 multiplications and 3 subtractions. There would be one more multiplication by a factor of 0.5, if you want to calculate the area of the triangle.
EDIT: You also need the length of the cross product, i.e. another square root is required here too, to be precise.

Which option do you think is faster?

1 Like

Well then I’d think about a sin/cos lookup table as a texture, then it’s a texture lookup and a square root But the cross product might be easier anyway

1 Like

Yes, we need a megatexture for mathematical approximations 1 Like
• Gpu billboarding
• procedual path following mesh generation

To list a few usecases i know i used the cross

1 Like

I recently experimented with 2D polygons and turning one giant concave polygon into a set of convex sub-polygons. The cross-product (or more detailed the “determinant of a 2D vector”) helped to identify those vertices of the 2D polygon that are convex or concave (there is another expression for that which I can’t remember right now). You can use that principle in 3D - then it’s the 3D cross product and not the 2D cross product.

As others said: to construct normals (used that back in 2006 when I made a little planet in 3D and wanted to know the normals for all surface vertices).

1 Like

Can you elaborate this please ?

I think it’s tunnels and “swinging sword made visible” stuff: Character swings the sword and you see a trail made of polygon following the weapon / a tunnel or road may follow a path. Just a guess.

1 Like

I use it for roads, rivers and fence building. It gives you the “left” and “right” vector assuming you know Up and Forward.

2 Likes

And projection along the vector. Or distance to an object in parallel-to-viewer-space. The dot product has a bunch of uses. Even a matrix transform is basically three dot products.

Cross product is useful but not as useful. It gives you the vector orthogonal to the two vectors… up or down depending on order and handedness. In JME/OpenGL’s default coordinate system, I try to remember it as left arm cross right arm = neck.

The length of the cross product is the sine of the angle between the two vectors * the length of each vector. So if they are unit vectors then you get a vector of length sine. In 2D this was a little more useful… I almost never use the length of the cross product in 3D. (90% of the time just normalize it out.)

1 Like

Any quick start guide around? A math cheat sheet or something like that?

Right! @Apollo once showed me this technique but I didn’t pay enough attention at the time to realize it contained a cross product. In case anyone wants to try it out:

You make a simple quad, but you only pass the center as the vertex position and set the scale (in world units) as texCoord2, like so:

``````texCoord2=new Vector2f[]{
``````
``````  new Vector2f(-100.0f, -100.0f),
new Vector2f(100.0f, -100.0f),
new Vector2f(100.0f, 100.0f),
new Vector2f(-100.0f, 100.0f),
};
``````

And this goes into the vertex shader:

`````` // Calculate left and up vectors of the billboard quad.
``````
`````` vec3 u=normalize(cross(m_CameraUp, g_CameraDirection));
``````
`````` // Drag the vertex away from the billboard center.
vec4 wPos=g_WorldMatrix*inPosition;
wPos.xyz+=-u*inTexCoord2.x+v*inTexCoord2.y;
gl_Position=g_ViewProjectionMatrix*wPos;
``````

and voilà, billboard quad from the vertex shader. Neat trick Btw, does a version of this happen to be integrated in the Particle.j3md? Sounds like something that the emitter could fully control.

2 Likes

Better try “right arm cross left arm = neck”.

I use the right hand: “thumb cross index finger = middle finger”.

How about that: x cross y = z
(in a RHS)

Yeah, I was thinking left hand coordinate system… I always get it backwards the first time no matter which mnemonic I use. :-/

In JME, I’ll remember the X cross Z = -Y (and that feels backwards to me) and then I double-backwards it in some number of months. 