0

I am trying to rotate an object (essentially a cube from Maya) 90 degrees one way when tapped, then when tapped again it rotates -90 degrees.

The rotation when tapped is working, however the pivot seems to be located at the origin (0,0,0), so it always rotates around the origin.

I want to be able to manipulate the anchor point but keep the geometry in the same place.

Ideally the anchor point would be moved to the back left of the object if possible?

This is what I have tried so far:

  1. Using : result.node.pivot = SCNMatrix4MakeTranslation(-30.0, 0.0, -20) but that seems to just move the pivot point and the geometry together.

  2. Bringing in my geometry again from Maya with the pivot in the correct place but Xcode seems to reset all pivots to origin.

  3. Making an empty node moving the pivot to the desired location (roughly) and putting my object in the empty node and that didn't work.

Any help would be great! Thank you in advance :)

Here is my code to rotate the object:

   var objectHit = false
        var firstTimePressed : Bool = false
        var hitTestOptions = [SCNHitTestOption: Any]()
        hitTestOptions[SCNHitTestOption.boundingBoxOnly] = true
        let results: [SCNHitTestResult] = sceneView.hitTest(latestTouchLocation, options: hitTestOptions)

        // The user has touched the virtual object
        for result in results {
            let object = VirtualObject.isNodePartOfVirtualObject(result.node)

            if object != nil {
                objectHit = true
            }
            if object?.childNode(withName: "door", recursively: true) == result.node{                   
                if(firstTimePressed == false){
                    print("object should open")
                    result.node.pivot = SCNMatrix4MakeTranslation(-30, 0, -20)
                    result.node.runAction(SCNAction.rotateBy(x: 0, y: -1.5708, z: 0, duration: 1))
                    firstTimePressed = true
                }
                else{
                    print("object should close")
                    result.node.pivot = SCNMatrix4MakeTranslation(-30, 0, -20)
                    result.node.runAction(SCNAction.rotateBy(x: 0, y: 1.5708, z: 0, duration: 1))
                    firstTimePressed = false
                }

            }
       }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
spoax
  • 471
  • 9
  • 29
  • Best thing you can do is use maya to set pivot. And from xcode you can place a sphere as your node's child to check if pivot translation is set correctly – Alok Subedi Feb 20 '18 at 04:21
  • @AlokSubedi the sphere is located at the origin too. I definitely exported it with the correct pivot. I am exporting the files as a .dae and converting in in Xcode to a .scn file. Is it it being lost there? Is there not a way to change the pivot in Xcode? – spoax Feb 20 '18 at 08:38
  • I do not know maya and setting pivot point for .dae is difficult, need to delete history, freeze and stuff. In xcode it is hit and trial method like I said above. Place sphere as child and translate pivot untill your sphere is approximately where you want your pivot in respect to box. – Alok Subedi Feb 20 '18 at 10:52
  • i still suggest you to set pivot in maya. because above method gives you approximate value – Alok Subedi Feb 20 '18 at 10:52
  • I have checked my pivot from an exported .dae file in a new Maya scene and the pivot is in the correct place. So it must be lost when brought into Xcode and covered to a .scn file. Can you not change the pivot inside Xcode using a pivot function? – spoax Feb 20 '18 at 16:25
  • 2
    “that seems to just move the pivot point and the geometry together.” No it doesn’t move the pivot, only the geometry. Move the node itself in the opposite direction of which you moved the pivot and you should get the desired results. – Xartec Feb 20 '18 at 18:20
  • 2
    An alternative option is to place an empty node at where you want the pivot to be, place your model where you want it to be, and then make it a child node of the empty node and then rotate that empty helper node instead of the model. – Xartec Feb 20 '18 at 18:28
  • @Xartec that worked great thanks. I had to switch my geometry to world space instead of local when parenting it to the empty node so I didn't get some weird double transformation. Thanks again! – spoax Feb 20 '18 at 22:18

0 Answers0