This site uses strictly necessary cookies. More Information

X- Home /

# How to transform a gameobject based on 3d 3d registration matrix

I am trying to do a 3d-3d point based registration. I have six points that I used to determine the registration matrix. When i do RegMat.multiplyPoint(point), it gives correct answers. However, when I try to transform a gameobject based on the matrix (code below), it gives me some strange results as shown in the image. What is the correct way to transform a gameobject in Unity?

Note1: The blue points on the virtual object should coincide with green (groundtruth) and red (RegMat.multiplyPoint(point)) sphere positions.

Note2: The object is placed at origin so multiplying the current position with RegMat gives the same result.

Note 3: The object centre position seems to be at the right position but the rotation is totally wrong.

```
obj.transform.localPosition = MatrixHelper.GetPosition(RegMat);
obj.transform.localRotation = MatrixHelper.GetQuaternion(RegMat);
obj.transform.localScale = MatrixHelper.GetScale(RegMat);
public class MatrixHelper
{
// Start is called before the first frame update
public static Quaternion GetQuaternion(Matrix4x4 matrix)
{
Vector3 forward = new Vector3(matrix.m02, matrix.m12, matrix.m22);
Vector3 upwards = new Vector3(matrix.m01, matrix.m11, matrix.m21);
return Quaternion.LookRotation(forward, upwards);
}
public static Vector3 GetPosition(Matrix4x4 matrix)
{
return new Vector3(matrix.m03, matrix.m13, matrix.m23);
}
public static Vector3 GetScale(Matrix4x4 matrix)
{
Vector3 scale;
scale.x = new Vector4(matrix.m00, matrix.m10, matrix.m20, matrix.m30).magnitude;
scale.y = new Vector4(matrix.m01, matrix.m11, matrix.m21, matrix.m31).magnitude;
scale.z = new Vector4(matrix.m02, matrix.m12, matrix.m22, matrix.m32).magnitude;
return scale;
}
}
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

this.transform.FindChild(x) and GameObject.Find(x) returning (apparently?) different results 1 Answer

How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers

Why does this wont work? transfor.find() 1 Answer

Are .gameObject and .transform both using GetComponent() in the background? 1 Answer

Convert a GameObject to a transform? 1 Answer