1

I need to rotate a SCNText object from the place in the sceneView I initially set it to. When I create the SCNText I adjust its pivot point so that it rotates on it's center. The issue is after I do that it moves it self to its leftAlignment corner.I used a red dot to create the point

enter image description here

Without this code below it rotates incorrectly (from its leftAlignment corner) as shown in picture 1. With this code it does rotate correctly from its center but its original location is now wrong as shown in picture 2.

let min = textNode.boundingBox.min
let max = textNode.boundingBox.max
textNode.pivot = SCNMatrix4MakeTranslation(
    min.x + (max.x - min.x)/2,
    min.y + (max.y - min.y)/2,
    min.z + (max.z - min.z)/2
)

I'm looking for the 3rd picture. How can I achieve this?

code:

let squareNode = SCNNode()
squareNode ...
squareNode.position = SCNVector3(0.0, 0.6, -0.7)

let material = SCNMaterial()
material.diffuse.contents = UIColor.yellow

let geometry = SCNText(string: "SCNText", extrusionDepth: 0.1)
geometry.flatness = 0
geometry.font = UIFont.systemFont(ofSize: 6.3)
geometry.materials = [material]

let textNode = SCNNode()
textNode.geometry = geometry
textNode.geometry?.firstMaterial?.isDoubleSided = true

// *** code to use its center ***
let min = textNode.boundingBox.min
let max = textNode.boundingBox.max
textNode.pivot = SCNMatrix4MakeTranslation(
    min.x + (max.x - min.x)/2,
    min.y + (max.y - min.y)/2,
    min.z + (max.z - min.z)/2
)

textNode.position = SCNVector3(0.1, 0.55, -0.7)
sceneView.scene.rootNode.addChildNode(textNode)

I also tried not adding the 'code to use its center' when I first created the textNode and instead added the code at the time the rotation occurs inside the respondToSwipeGesture but once the rotation is finished it ends up/moves itself to the same leftAlignment corner as shown in picture 2.

func respondToSwipeGesture(recognizer: UIGestureRecognizer) {

    case UISwipeGestureRecognizer.Direction.right:

    // ...

    let node = hitResults.first.node

    let rotate = SCNAction.rotateBy(x: 0, y: CGFloat(degToRadians(degrees: -68.5)), z: 0, duration: 0.33)

    let min = node.boundingBox.min
    let max = node.boundingBox.max
    node.pivot = SCNMatrix4MakeTranslation(
        min.x + (max.x - min.x)/2,
        min.y + (max.y - min.y)/2,
        min.z + (max.z - min.z)/2
    )

    node.runAction(rotate)
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256

0 Answers0