# Get relative Location from other component?

**URL:** <https://forums.unrealengine.com/t/get-relative-location-from-other-component/119274>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [November 27, 2018, 6:28am UTC](https://forums.unrealengine.com/t/get-relative-location-from-other-component/119274 "2018-11-27T06:28:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ejakulatje](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ejakulatje](https://forums.unrealengine.com/u/ejakulatje)\
**Post date:** [November 27, 2018, 6:28am UTC](https://forums.unrealengine.com/t/get-relative-location-from-other-component/119274/1 "2018-11-27T06:28:45Z")

</div>

Hello,

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

---

<div class="post-metadata">

**Author:** ![ExtraLifeMatt](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/extralifematt/32/918695_2.png) [@ExtraLifeMatt](https://forums.unrealengine.com/u/ExtraLifeMatt)\
**Post date:** [November 27, 2018, 2:32pm UTC](https://forums.unrealengine.com/t/get-relative-location-from-other-component/119274/2 "2018-11-27T14:32:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ejakulatje](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ejakulatje](https://forums.unrealengine.com/u/ejakulatje)\
**Post date:** [November 27, 2018, 5:22pm UTC](https://forums.unrealengine.com/t/get-relative-location-from-other-component/119274/3 "2018-11-27T17:22:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0ba2eb3c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0ba2eb3c](https://forums.unrealengine.com/u/anonymous_user_0ba2eb3c)\
**Post date:** [November 27, 2018, 6:59pm UTC](https://forums.unrealengine.com/t/get-relative-location-from-other-component/119274/4 "2018-11-27T18:59:39Z")

</div>

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

```auto

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

```

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

```auto

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

```

Hope to have helped 🙂

---

<div class="post-metadata">

**Author:** ![ejakulatje](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ejakulatje](https://forums.unrealengine.com/u/ejakulatje)\
**Post date:** [November 27, 2018, 8:52pm UTC](https://forums.unrealengine.com/t/get-relative-location-from-other-component/119274/5 "2018-11-27T20:52:43Z")

</div>

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

```auto

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

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0ba2eb3c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0ba2eb3c](https://forums.unrealengine.com/u/anonymous_user_0ba2eb3c)\
**Post date:** [November 28, 2018, 2:16am UTC](https://forums.unrealengine.com/t/get-relative-location-from-other-component/119274/6 "2018-11-28T02:16:37Z")

</div>

> [@ejakulatje](#):
>
> But is it also possible to convert the world position for example from component a to relative position from component b?

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

```auto

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.

---

<div class="post-metadata">

**Author:** ![ejakulatje](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ejakulatje](https://forums.unrealengine.com/u/ejakulatje)\
**Post date:** [November 28, 2018, 4:15pm UTC](https://forums.unrealengine.com/t/get-relative-location-from-other-component/119274/7 "2018-11-28T16:15:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0ba2eb3c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0ba2eb3c](https://forums.unrealengine.com/u/anonymous_user_0ba2eb3c)\
**Post date:** [November 29, 2018, 12:07am UTC](https://forums.unrealengine.com/t/get-relative-location-from-other-component/119274/8 "2018-11-29T00:07:02Z")

</div>

> [@ejakulatje](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![ejakulatje](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ejakulatje](https://forums.unrealengine.com/u/ejakulatje)\
**Post date:** [November 29, 2018, 5:18am UTC](https://forums.unrealengine.com/t/get-relative-location-from-other-component/119274/9 "2018-11-29T05:18:55Z")

</div>

Ok, thank you perfect
