I'm very new and I'm studying this ARKit code from RPasecky/CubesInSpace on github where he/she convert the point of view 's rotation vector into a rotation matrix


func calculateCameraDirection(cameraNode: SCNVector4) -> SCNVector3 {
    let x = -cameraNode.x
    let y = -cameraNode.y
    let z = -cameraNode.z
    let w = cameraNode.w
    let cameraRotationMatrix = GLKMatrix3Make(cos(w) + pow(x, 2) * (1 - cos(w)),
                                              x * y * (1 - cos(w)) - z * sin(w),
                                              x * z * (1 - cos(w)) + y*sin(w),

                                              y*x*(1-cos(w)) + z*sin(w),
                                              cos(w) + pow(y, 2) * (1 - cos(w)),
                                              y*z*(1-cos(w)) - x*sin(w),

                                              z*x*(1 - cos(w)) - y*sin(w),
                                              z*y*(1 - cos(w)) + x*sin(w),
                                              cos(w) + pow(z, 2) * ( 1 - cos(w)))

It was called from here

    let rotation = self.sceneView.pointOfView!.rotation //else { return (SCNVector3Zero) }
    let direction = calculateCameraDirection(cameraNode: rotation)

I understand the rotation of pointOfView that is passed in has x, y, z as the coordinate of the axis and w is the rotation angle. But how do I arrive at the code in the function? Which concepts should I learn about here?

I read about homogeneous vector, find rotation matrix between 2 vectors, and transformation matrix but this doesn't look like it


  • \$\begingroup\$ If you want to know how to derive the formulas above, it might be a better question for the Math StackExchange. There's nothing particularly game-specific about constructing a 3x3 rotation matrix from an axis vector and rotation angle. \$\endgroup\$ – DMGregory Sep 7 '17 at 23:29
  • \$\begingroup\$ oh, so based on this meta.stackexchange.com/questions/85017/… I guess I need to create a brand new question? Is it OK to have the same question on 2 stackexchange? \$\endgroup\$ – Motoko Sep 7 '17 at 23:41
  • \$\begingroup\$ Cross-posts are discouraged. I think you can delete your question here and then ask a new one. Though there's also a good chance it's already been asked and you can just search out the answer without needing to wait for replies at all. \$\endgroup\$ – DMGregory Sep 7 '17 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.