Convert matrix_float4x4 to x y z space

A good way to figure this out is to learn about 4x4 matrices as they are really the basis for all 3D visualization and mathematics.

  • To get the position from a 4x4 matrix use the entries m41, m42, m43.
  • To get the rotation use the 3x3 matrix in m11 ... m33.

Some resources to get you started:

  • Brush up on that linear algebra first in 2D, then 3D: I like http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
  • Or if the game programming stuff turns you off: https://betterexplained.com/articles/linear-algebra-guide/
  • Then straight to GL: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/

I'm using matrix_float4x4 extension for that:

extension matrix_float4x4 {
    func position() -> SCNVector3 {
        return SCNVector3(columns.3.x, columns.3.y, columns.3.z)
    }
}

Using:

let position = transform.position()

Or you can convert position directly in code:

let position = SCNVector3(
    transform.columns.3.x,
    transform.columns.3.y,
    transform.columns.3.z
)

Tags:

Swift3

Arkit