Get relative Location from other component?

Hello,

does anyone know how i can get the relative location from my camera and use it in the coordinate system of my hand?

1 Like

You can get a components world/local transform, so you could just grab both, take the difference as your “To Hand Space” transform, and go from there.

Sry for my bad english. Which Commands do i need for this?

To transform from world location to relative location use [FONT=courier new]InverseTransformPosition:



FVector relativeLoc = GetTransform().InverseTransformPosition(myComponent->GetComponentLocation());


To transform from relative location to world location use [FONT=courier new]TransformPosition:



FVector someRelativeLoc;
FVector worldLoc = GetTransform().TransformPosition(someRelativeLoc);


Hope to have helped :slight_smile:

But is it also possible to convert the world position for example from component a to relative position from component b?


FVector relativeLoc = GetTransform().InverseTransformPosition(GetTransform().TransformPosition(ComponentA));


If I understand you correctly you want the position of A relative to B. Here’s how you do it:


FVector aPosRelativeToB = b->GetComponentTransform().InverseTransformLocation(a->GetComponentLocation());

[FONT=courier new]a->GetComponentLocation() will get the world position of A and then we do an [FONT=courier new]InverseTransformLocation on B’s transform to get the position of A relative to B.

So if A would be my camera and B a ball your script would transform b “inside” the camera?

If I understand you correctly, no, it will be the other way around. You would be getting the position of the camera relative to the ball.

Here’s an example and how to get the position of the ball relative to the camera (position of B relative to A):

Imagine we’re on a 2D world (UE4 is 3D but the same concept applies, it’s just easier to write and understand the example in 2D).
Our camera is on position (X=2, Y=2)
Our ball is on position (X=5, Y=5)

Naturally, the world origin is (X=0, Y=0)

The world position of the ball is (X=5, Y=5)
The world position of the camera is (X=2, X=2)
The position of the ball relative to the camera is (X=3, Y=3) <= this is what [FONT=courier new]camera->GetComponentTransform().InverseTransformLocation(ball->GetActorLocation()); will return
The position of the camera relative to the ball is (X=-3, Y=-3) <= this is what [FONT=courier new]ball->GetActorTransform().InverseTransformLocation(camera->GetComponentLocation()); will return

Ok, thank you perfect