I'm trying to clone an entity that's somewhere deeper in hierarchy and I want it together with transform that takes into account parents.
Initially I made something that would go back through parents, get their transforms and then reduce them to single one. Then I realized that what I'm doing is same as .transformMatrix(relativeTo: rootEntity), but to validate that what I made gives same results I started to print them both and I noticed that for some reason the last row instead of stable (0,0,0,1) is sometimes (0,0,0,0.9999...). I know that there are rounding errors, but I'd assume that 0 and 1 are "magical" in FP world.
The only way I can try to explain it, is that .transformMatrix
is using some fancy accelerated matrix multiplication and those produce some bigger rounding errors. That would explain slight differences in other fields between my version and function call, but still - the 1 seems weird.
Here's function I'm using to compare:
func cloneFlattened(entity: Entity, withChildren recursive: Bool) -> Entity {
let clone = entity.clone(recursive: recursive)
var transforms = [entity.transform.matrix]
var parent: Entity? = entity.parent
var rootEntity: Entity = entity
while parent != nil {
rootEntity = parent!
transforms.append(parent!.transform.matrix)
parent = parent!.parent
}
if transforms.count > 1 {
clone.transform.matrix = transforms.reversed().reduce(simd_diagonal_matrix(simd_float4(repeating: 1)), *)
print("QWE CLONE FLATTENED: \(clone.transform.matrix)")
print("QWE CLONE RELATIVE : \(entity.transformMatrix(relativeTo: rootEntity))")
}
else {
print("QWE CLONE SINGLE : \(clone.transform.matrix)")
}
return clone
}
Sometimes last one is not 1
QWE CLONE FLATTENED: [
[0.00042261832, 0.0009063079, 0.0, 0.0],
[-0.0009063079, 0.00042261832, 0.0, 0.0],
[0.0, 0.0, 0.0010000002, 0.0],
[-0.0013045187, -0.009559666, -0.04027118, 1.0]
]
QWE CLONE RELATIVE : [
[0.00042261826, 0.0009063076, -4.681872e-12, 0.0],
[-0.0009063076, 0.00042261826, 3.580335e-12, 0.0],
[3.4256328e-12, 1.8047965e-13, 0.0009999998, 0.0],
[-0.0013045263, -0.009559661, -0.040271178, 0.9999997]
]
Sometimes it is
QWE CLONE FLATTENED: [
[0.0009980977, -6.1623556e-05, -1.7382005e-06, 0.0],
[-6.136851e-05, -0.0009958588, 6.707259e-05, 0.0],
[-5.8642554e-06, -6.683835e-05, -0.0009977464, 0.0],
[-1.761913e-06, -0.002, 0.0, 1.0]
]
QWE CLONE RELATIVE : [
[0.0009980979, -6.1623556e-05, -1.7382023e-06, 0.0],
[-6.136855e-05, -0.0009958589, 6.707254e-05, 0.0],
[-5.864262e-06, -6.6838256e-05, -0.0009977465, 0.0],
[-1.758337e-06, -0.0019999966, -3.7252903e-09, 1.0]
]
0s in last row seem to be stable.
It happens both for entities that are few levels deep and those that have only anchor as parent.
So far I've never seen any value that would not be "technically a 1", but my hierarchies are not very deep and it makes me wonder if this rounding could get worse.
Or is it just me doing something stupid? :)