简体   繁体   中英

3D (iOS SceneKit), switch transforms of camera and another object

I have a scene with a camera and one other object: a 3D character model.

Using orientation information from a pose estimation algorithm (a vec3 for translation, and a vec4 quaternion for rotation) my camera's transform is being translated and rotated to focus on this character, which is at (0,0,0) and with 0 rotation . This works correctly and precisely, but I want to 'reverse' these values so that I'm influencing the character object with the input, having the camera placed at 0,0,0 with 0 rotation instead.

I feel like I should be rotating the supplied camera transformation matrix about the origin to create myObjectTransform , then zeroing the camera values and simply applying myObjectTransform as the transform of the character object. If I do this, though, my input translation and rotation values don't influence the scene as I'd like - rotation seems to go in the wrong directions, and even the translation position seems off.

This sounds like it should be easy, but I'm obviously missing something. Can anyone help?

I solved this using SCNMatrix4Invert - the inverse of the camera's matrix is exactly what I was looking for. I forgot, though, that I was applying an extra X rotation of 180 degrees upon the initial given translation and rotation.

So my process, albeit a little untidy-looking, is:

  1. Create an SCNMatrix4, and set its position and rotation from the pose estimation input
  2. Rotate the matrix in the X axis by 180 degrees (my custom step)
  3. Invert the matrix
  4. I also arbitrarily scale my character object up, so I take a note of its scale vector.
  5. Apply the matrix as the 'transform' property of my character object's SCNNode
  6. Set the scale of the SCNNode obtained in #4. There are many ways I could do this, like applying the same scale to the SCNMatrix4 - that would probably better practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM