Maths

Help a newbie get through the basics.
How to find the distance between two points in 3D space?
How do I write (calculate) a mathematical expression? For example: var1 * cos (var2) + var1 * sin (var2).

Vector - vector then get Vector length

1 Like

thereā€™s also a built in function which does the same thing
Annotation 2021-08-30 162432

2 Likes

You can actually just write it:

image

3 Likes

What is a vector? Point coordinates?

yes 3D coord, you can read this doc for ue4 basic terms: https://subscription.packtpub.com/book/game_development/9781784394905/1/ch01lvl1sec18/the-2d-and-3d-coordinate-systems

1 Like

In UE, a vector is just a point. But there is also the implied distance from the origin. So it is also a vector in the traditional senseā€¦

1 Like

Now I understand, thanks.

1 Like

Another stupid math question. I just canā€™t understand. There are two variables of the rotator type
R1 = (0, 0, 0)
R2 = (90, 0, 0)
As you can see, they are perpendicular to each other. I apply a random (I donā€™t know which) transformation to the first rotator and it becomes, for example
R1 = (23, -78, 55)
How do I calculate the transformation for the second rotator so that it remains perpendicular to the first?

Donā€™t try and do it in your head, it will explode, use the compose rotators node ( order matters ).

1 Like

I do not understand, you can tell in more detail.

image

1 Like

Thank you, this is what I need. Now my AAA project is saved.

Geez. Stakes are high here! :wink:

2 Likes

If youā€™re concerned about the actual math behind the distance between two Transforms, then hereā€™s that educational answer.

The Pythagorean distance between two Translation Vectors is:

āˆš ( ( v2_x - v1_x )Ā² + ( v2_y - v1_x , 2) Ā² (v2_z - v1_z )Ā² )

to actually get a float value d out of this, is very tricky because FTransform is a struct, and well, contains some other data, but it can be done, because youā€™ve to apply both scale and rotation to bring that down to what you need, which is an Vector, resulting in a Real Vector, and then do the distance calculation.

Hereā€™s that solution in raw blueprint nodes, taking in two actors as a reference, and calculating the Pythagorean distance between them (d), returning a float that explicitly tells you how far away two objects are from each other. If thatā€™s all the information you need, than this oneā€™s for you.

GetDistance Blueprint

How to break out the transform, then apply the scale and the rotator to get the real vector.

And finally, Pythagorean Theorem, with all 6 components from the output of the real vector calculations.

This probably overcomplicates matters, but my answer here is more of a ā€œneed to knowā€ thing, about the math going on under the hood. You should get acquainted/comfortable with trigonometry, it is of course used a lot in calculations like this.

2 Likes

If, additionally, youā€™d like to get super technical and actually use the center of an actor and not its root componentā€™s pivot to do the distance calculation, you can:

Drop the original Actorā€™s transform, and use a GetActorBounds node, which returns the center of an Actor with all of its components in world space using a bounding box, and use that.

In most ā€œAm I too far away, or close enough?ā€ state checks as well, the negative sign isnā€™t needed. My last post for accuracy defines how, but in here, is probably the way youā€™d use (d), as an absolute value:

image

But do you see where our new problem is? Weā€™re now applying FRotator and FScale3D to the center of the FBoxExtents. Should we do this? No. Because GetBounds is a collission-friendly and does that for us! So, can we skip it? **** yes.

Which does result in this bad boy:

No longer need to bother with FRotator or FScale3D at all thanks to Box Extents doing all that internally during a GetBounds call.

Heā€™s not the most efficient (better ways, line traces being one) but he makes the mathematician happy.

Final Notes

Collision does not have to be enabled to do a GetBounds call, so these calculations stay independent of Physics, which is nice, long as you leave the booleans on the input nodes alone.

I would advise leaving ā€œGet Child Actorsā€ alone unless you have actors chained together for something special, and need the child actors to be taken into account, too. This is usually only for edge cases.

2 Likes

Of course, when I post this, I donā€™t set up my exec nodes correctly, so hereā€™s that ā€œdonā€™t yell at meā€ post (lol)

1 Like

Thank you, this is very interesting. I am familiar with the basics of trigonometry. I understand that the Pythagorean theorem is used to calculate the distance between points. I just donā€™t understand what the GetActorBounds function does?

1 Like

This actor has 2 scene components (white sphere + white box), its bounds look like so - the yellow frame:

image

Visualising things with debug nodes helps a lot - worth exploring.

4 Likes

Origin is the coordinates of the center of the box.
Box Extent - coordinates of what? Or is it the dimensions of the box?
If my actor is a mannequin, then the center will depend on the position of the body and will change all the time. Right?

1 Like