0

I put a SCNNode to a scene. I want to provide proper rotation in space, because this node is a pyramid. I want Z axis to be pointed to V2 point, and X axis to be pointed to V1 point (these V2 and V1 point are calculated dynamically, and of course in this case the angle between axis will be 90 degrees, because I calculate them properly).

The problem: I can't point X axis, because SCNLookAtConstraint(target: nodeWithV2) points only Z axis, and what I see is that Z axis is OK, but X axis is always random, that's why my Pyramid orientation is always wrong. How can I point X axis?

Here is my code:

let pyramidGeometry = SCNPyramid(width: 0.1, height: 0.2, length: 0.001)
pyramidGeometry.firstMaterial?.diffuse.contents = UIColor.white
pyramidGeometry.firstMaterial?.lightingModel = .constant
pyramidGeometry.firstMaterial?.isDoubleSided = true
let nodePyramid = SCNNode(geometry: pyramidGeometry)
nodePyramid.position = SCNVector3(2, 1, 2)
parent.addChildNode(nodePyramid)
let nodeZToLookAt = SCNNode()
nodeZToLookAt.position = v2
parent.addChildNode(nodeZToLookAt)
let constraint = SCNLookAtConstraint(target: nodeZToLookAt)
nodePyramid.constraints = [constraint]

But that only sets the direction for Z axis, that's why X axis is always random, so the rotation is always random. How can I set X axis direction to my point V1?

Paul T.
  • 4,938
  • 7
  • 45
  • 93

1 Answers1

2

starting iOS 11 SCNLookAtConstraint has a localFront property that allows you to change which axis is used to orientate the constrained node.

mnuages
  • 13,049
  • 2
  • 23
  • 40