I'm learning from a tutorial online on ARKit to place an object on a plane.
boxNode.position = SCNVector3(hitResult.worldTransform.columns.3.x,hitResult.worldTransform.columns.3.y + Float(boxGeometry.height/2), hitResult.worldTransform.columns.3.z)
he uses the code above to place it at the location where you tap on the screen
what does this mean:
hitResult.worldTransform.columns.3.x
why is it columns.3 and not columns.0 for example?
ARHitTestResult.worldTransform
is of type matrix_float4x4
. So it's a 4x4 matrix. .columns
are numbered from 0
, so the vector (hitResult.worldTransform.columns.3.x, hitResult.worldTransform.columns.3.y, hitResult.worldTransform.columns.3.z)
is the three things at the top of the final column of the 4x4 vector.
You can safely assume that the bottom row of the matrix is (0, 0, 0, 1)
and that positional vectors are of the form (x, y, z, 1)
. So then look at what the matrix does when applied to a vector:
a b c d x a*x + b*y + c*z + d
e f g h y e*x + f*y + g*z + h
i j k l * z = i*x + j*y + k*z + l
0 0 0 1 1 1
The (d, h, l)
don't get multiplied and are just added on as if they were a separate vector. It's the same as:
a b c x d
e f g * y + h
i j k z l
So, the top-left 3x3 part of the matrix does something to (x, y, z)
but doesn't move it. E.g. if (x, y, z)
is (0, 0, 0)
at the start then it'll definitely still be (0, 0, 0)
at the end. So the 3x3 matrix might rotate, scale, or do a bunch of other things, but can't be a translation.
(d, h, l)
though, clearly is just a translation because it's just something you add on at the end. And the translation is what you want — it's how you get to the plane from the current camera position. So you can just pull it straight out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With