7

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?

slimboy
  • 1,633
  • 2
  • 22
  • 45

2 Answers2

10

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.

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • Tommy I know this old and maybe I'm missing the point (having stumbled on a similar tutorial). But without inspecting the vector how would one know that it's column 3 you need? Does it come down to a standard around how these vectors are constructed in OPENGL or other 3d engines? – TommyBs Mar 28 '18 at 13:31
-2

That has to do with how simdtransform is laid out. One of them is rotate, one is scale, and one is translate. Column 3 pertains to the translate matrix

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44