Unti test for position and rotation gets wrong after setParent

I do unit test for setting mesh position and rotation from data, for example, expect(mesh.position._x).toBe(2);

I’m doing something like this to mock the parent mesh
jest.spyOn(scene, ‘getNodeByName’).mockImplementation(() => new Mesh(‘ParentMesh’));

But after mesh.setParent = ParentMesh, the mesh position and rotation has been changed, this cause my unit test to fail, does I do the unit test in the correct way?

I think that it should be testing mesh.absolutePosition.x which should be expected to be the same before and after calling setParent (within a small amount of error). I.E. the mesh’s position and rotation are changed in order to keep its absolute (world) position and rotation the same.

1 Like

Thank you, I will try that, but how about the rotation change? Or the forward

Hmm the rotation should look the same on the screen but IDK how to unit test it because diff values can achieve the same result… :thinking:

1 Like

position/rotation/scaling are local values.
There are combined with parent’s one once attached.

1 Like

Yes, the position and rotation are the same in inspector, but the unit test value changed

what are the values before and after parenting?

The position is still very close, it just from, for example, x: 1.5 to x: 1.55555555, but for rotation, it changes a lot, for example, could from y: 0.5 to y: 5

Hmm, your’e using setParent right, not just setting the property?

Because if you’re calling setParent then the child’s position, rotation, and scaling are supposed to be changed, that’s how it keeps the parent’s current transform from affecting the child.

OTOH if you set the parent property directly, using child.parent = parent, then the child’s position, rotation, and scaling values should still be exactly the same since they aren’t altered in that case.

1 Like

Useful doc: Parents and Children | Babylon.js Documentation (babylonjs.com)

2 Likes