Vector Math in Unreal

Any math gurus out there? It’s been a while since Calculus/Trig and even longer since Algebra. Since I deal with vectors all the time in Unreal, I thought it’d be a good idea to spend a day doing some by hand. In doing so I realized two things: The way I think about vector multiplication in Unreal and the way Unreal handles vector multiplication differs from how it’s handled in physics or other math. This got me thinking, are we doing them wrong?

According to Linear Algebra, there are only two ways to multiply vectors: Dot Product and Cross Product. This of course is excluding things like float x vector or getting length/magnitude.

The Dot Product multiplies two vectors and adds the coordinates together to get a single float value.

A = (x, y, z)
B = (x, y, z)
C = (Ax * Bx + Ay * By + Az + Bz)

The Cross Product converts two vectors into matricides and finds the determinant of each coordinate group to produce a new vector.

A = (x, y, z)
B = (x, y, z)
C =
[x Ax Bx]
[y Ay By]
[z Az Bz]

which is…

x =
[Ay Az]
[By Bz]

(minus)

y =
[Ax Az]
[Bx Bz]

(plus)

z =
[Ax Ay]
[Bx By]

Both the Dot Product and Cross Product are optional in Unreal, but natively it simply multiplies the coordinate pairs together to produce a new vector like so:

A = (x, y, z)
B = (x, y, z)
C = (Ax * Bx, Ay * By, Az * Bz)

This is basically ordered pairs, which makes sense because a 3D environment is just a graph. Now, I can get pretty much anything I want from this: the distance between two vectors or the angle, etc, and I don’t have to do any trig.

I suppose the question is why does this still work and is it technically wrong? Or are Unreal vectors not really vectors?

I’m no guru… but seems you have some misconceptions about how UE does math. That or I wasn’t able to understand your question.


What do you mean by optional?

They do not… unless you are refering to this node specifically?
image

Your confusion is largely about the language binding, not the math.

Unreal has chosen the * operator to mean “elementwise multiplication.”
To get the dot product, or the cross product, you use the VectorDot() or VectorCross() functions.

You should almost never have to do any trigonometry. If you’re doing character- or camera-relative movement, the character, or the camera, has a world transform, which has inherent “forward” and “right” and “up” vectors that you can use for movement. If you need to know whether two objects are “pointing at” each other, calculate the dot product between their “forward” vectors and compare the magnitude (negative means “pointing at each other,” positive means "pointing in the same direction.)
And so on.

Trigonometry is only really needed when your input is in “compass units” (either radians or degrees,) and you should almost never need to actually use compass units.

I’m not guru either. I very well could be off the map.

As in, when you multiply two vectors, it doesn’t do Dot Product or Cross Product automatically. There are specific operations for choosing the one you want. That’s not a bad thing, it’s actually a convenience, but it does leave me confused about the way UE handles vector multiplication otherwise.

I’ve done it by hand, with the BP node you’ve specified, and through code:

FVector A = FVector(1, 2, 3);
FVector B = FVector(4, 5, 6);
FVector C = A * B;

/* C = (1 * 4, 2 * 5, 3 * 6); */

The answer will be (4, 10, 18), which means, the native or default multiplication operation of two vectors if you do not choose Dot Product or Cross Product is just to multiply the coordinate pairs.

Thank you, that is very helpful. I’m still a little confused, but closer than I was.