#Quaternion.Inverse equivalent for DOTS mathematics quaternion

1 messages · Page 1 of 1 (latest)

tranquil pasture
#

I'm trying to manually translate the transform of a second entity (with collider component) with a first entity and use the first entity in effect as a 'parent'. Both entities are root level. And in doing the manual calculation for syncing position and rotation, I need to get the Quaternion.Inverse(parent.rotation), but mathematics quaternion does not seem to have the "Inverse" function available. My thoughts in this direction is based on this approach with UnityEngine.Quaternion:
//calculate offsets
Vector3 offsetPos = child.position - parent.position;
Quaternion offsetRot = child.rotation * Quaternion.Inverse(parent.rotation);
offsetPos = Quaternion.Inverse(parent.rotation) * offsetPos;

//apply the relative transformation
child.position = parent.rotation * offsetPos + parent.position;
child.rotation = parent.rotation * offsetRot;

What is the equivalent of this with Unity.Mathematics.quaternion, or is there a more preferred way as it goes with DOTS?

cursive yoke
kind coral
#

math.inverse(quaternion q)

tranquil pasture
#

math.inverse is the Correct approach for translating the code above (there may be another way to perform the manual calculation/translation between a detached child from a parent entity). Credit goes to @kind coral for the right recommendation, and thank you to @cursive yoke for trying to help.
To get the offset position and rotation (done once):
quaternion inverse = math.inverse(rootTransform.ValueRO.Rotation);
offsetPos = childtransform.Position - rootTransform.ValueRO.Position;
offsetPos = math.mul(inverse, offsetPos);
offsetRot = math.mul(childtransform.Rotation, inverse);

then simply apply the offset when the parent transform is updated (after every update):
childtransform.ValueRW.Position = math.mul(rootTransform.ValueRO.Rotation, offsetPos) + rootTransform.ValueRO.Position;
childtransform.ValueRW.Rotation = math.mul(rootTransform.ValueRO.Rotation, offsetRot);

cursive yoke
#

I mean, all of that is literally just one operation in LocalTransform struct itself

tranquil pasture
#

I was not able to figure it out with using LocalTransform. Can you point me to an example code, maybe?

cursive yoke
#

LocalTransform tf = someTf;
var worldPoint = tf.TransformPoint(localToParent);

#

Same for rotation or whole transform

#

Also same methods but inverse

tranquil pasture
#

Is worldPoint equivalent to the offset of the detached child from the parent? I think "someTf" is the detached child in your example?

cursive yoke
#

I don't really remember name

#

but I did use it several times

#

quite convinient

tranquil pasture
#

I see. it would be useful if you have a code snippet, by any chance. The way I'm doing it today is I capture the offsets of the detached child from parent while as prefab, then when I instantiate the prefab, the parent moves around and I manually apply the offsets to the detached child so it follows the parent. Two separate steps. I was trying to do it directly with TransformPoint() approach and I could not get the right offset results.

cursive yoke
#

just use TransfromTransform

#

to have full transformation with rotation