Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert matrix_float4x4 to x y z space

Tags:

swift3

arkit

I'm using ARKit and trying to get the position of the camera as a rotation and (x,y,z) coordinates in real world space. All I can manage to get is a matrix_float4x4, which I don't really understand, and euler angles only show the rotation.

Here's what I currently have:

let transform =  sceneView.session.currentFrame?.camera.transform
let eulerAngles =  sceneView.session.currentFrame?.camera.eulerAngles

Here's the output I'm getting:

eulerAngles: float3(-0.694798, -0.0866041, -1.68845)

transform: __C.simd_float4x4(columns: (float4(-0.171935, -0.762872, 0.623269, 0.0), float4(0.982865, -0.0901692, 0.160767, 0.0), float4(-0.0664447, 0.640231, 0.765304, 0.0), float4(0.0, 0.0, 0.0, 1.0)))

Is there a way to convert the matrix_4x4, or is there an easier way to get this information?

Thanks!

like image 801
Jonathan Plackett Avatar asked Jul 20 '17 10:07

Jonathan Plackett


3 Answers

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
)
like image 65
Vasilii Muravev Avatar answered Nov 07 '22 07:11

Vasilii Muravev


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/
like image 10
Christopher Oezbek Avatar answered Nov 07 '22 07:11

Christopher Oezbek


Two ways you can get the position of the camera:

  1. By using the currentFrame property of session you can get the transform of the camera and then use that to get the position:

    matrix_float4x4 cameraTransform = self.sceneView.session.currentFrame.camera.transform;
    SCNVector3 cameraPosition = SCNVector3Make(cameraTransform.columns[3].x, 
    cameraTransform.columns[3].y, cameraTransform.columns[3].z);
    
  2. Or get the position of the camera using the pointOfView property of the sceneView:

    SCNVector3 cameraPosition = self.sceneView.pointOfView.worldPosition;
    
like image 5
SilentK Avatar answered Nov 07 '22 08:11

SilentK