# Math behind Transform.TransformPoint?

**URL:** https://discussions.unity.com/t/math-behind-transform-transformpoint/199343
**Category:** Questions & Answers
**Created:** [November 28, 2017, 12:17am UTC](https://discussions.unity.com/t/math-behind-transform-transformpoint/199343 "2017-11-28T00:17:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Dray](https://avatars.discourse-cdn.com/v4/letter/d/f0a364/32.png) [@Dray](https://discussions.unity.com/u/Dray)
#### Post date: [November 28, 2017, 12:17am UTC](https://discussions.unity.com/t/math-behind-transform-transformpoint/199343/1 "2017-11-28T00:17:04Z")

</div>

So I need the functionality of [Transform.TransformPoint](https://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html) and [Transform.InverseTransformPoint](https://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html) inside a thread. I can’t call those functions directly since they are not thread safe.

I do know safely that the transform won’t change while the thread is running so I figured I could achieve the same results if I can figure out the math behind those two functions and do some magic with the transform.worldToLocal and transform.localToWorld matrix.

Unfortunately I’m not a magician, especially when it comes to complex vector and matrix math. Can anybody help me with this? I tried something like `transform.localToWorldMatrix * p`  
but that doesn’t seem to produce the same results as `transform.TransformPoint(p)`

_edit: actually that was not thread safe too, but that keeps me from just copying the Matrix4x4 values into the threads memory_

* * *

Long story short, I’m trying to figure the formula behind the two transform functions and rebuild them in a thread safe method that I can use for my heavy calculations.

---

<div class="post-metadata">

### Author: ![Bunny83](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@Bunny83](https://discussions.unity.com/u/Bunny83)
#### Post date: [November 28, 2017, 2:39am UTC](https://discussions.unity.com/t/math-behind-transform-transformpoint/199343/2 "2017-11-28T02:39:07Z")

</div>

Well you almost solved your problem ^^. The problem here is that you most likely pass in a Vector3 position. However a 4x4 matrix represents a [homogeneous coordinate space](https://en.wikipedia.org/wiki/Homogeneous_coordinates). A position / point / direction in homogeneous coordinates has 4 elements and not 3. Unity has implicit conversion operators for it’s Vector2 / 3 / 4 structs. So it implicitly converts your Vector3 into a Vector4. However the “w” coordinate is initialized to 0 in this case. This would turn your vector into a “direction” vector. Directions are not affected by the translation that the matrix might apply. So it only scales and rotates the vector.

To fix this you have to pass in a Vector4 with w set to “1”.

```
Vector3 pos;

Vector4 v = pos;
v.w = 1;
Vector4 wPos = localToWorld * v;

```

Keep in mind that you have to read the localToWorld property on the main thread. Once you have the matrix you can use it in another thread.

Another way is to use [MultiplyPoint3x4](https://docs.unity3d.com/ScriptReference/Matrix4x4.MultiplyPoint3x4.html). This would be probably the better solution ^^. It does the direct multiplication with the matrix and assumes that the matrix is a 3 by 4 matrix.
