• News

• Industries

• Learning & Support

• Community

• Marketplace # 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 2 Likes

You can actually just write it: 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.

CombineRotators: The A input is the first rotation and the B input should match R2 above.
If these are 2 actors in the world and testing for 90 degrees, then use DotProduct instead.
Basically you pass 2 actor vectors to DotProduct and check for 0.5 or -0.5 ( = 90 degrees)! 1 Like 1 Like

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

Geez. Stakes are high here! 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: 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: Visualising things with debug nodes helps a lot - worth exploring.

4 Likes