5
votes

I have 2 questions in mind to place 3D object.

  1. If a 3D object(for example Car) created to face door side of the car as camera's front face, how can I rotate the 3D object to make it normal? Like, rotate the car to make engine side of a car as camera's front face.

2.How to place the 3D object on Plane Surface by facing the object towards the camera. Move the Phone camera to see the backside of the object.

I am using XCode 9.1 with iOS 11 and above, ARKIT

1

1 Answers

7
votes

enter image description here

To rotate the car node you can just adjust the eulerAngles until the car is facing the camera.

  node?.eulerAngles = SCNVector3Make(0,Float(degToRadians(degrees:180 )),Float(degToRadians(degrees: 90)))

rotated this van around to front facing.

enter image description here

eulerAngles are in radians, so a handy conversion to degrees:

public func degToRadians(degrees:Double) -> Double
 {
    return degrees * (M_PI / 180);
  }

You can also do the same rotation on the van node using an animation

    let rotate = SCNAction.rotateBy(x: CGFloat(degToRadians(degrees: -90)), y: 0, z: 0, duration: 1)
    node.runAction(rotate)

You can use SCNLookAtConstraint which follows the pointOfView along the negative z-axis of the parentNode.

So if the car’s negative z-axis is the camera facing the side doors... as you rotate the camera, around the car.... the car will swivel always maintaining that view.

  let constraint = SCNLookAtConstraint(target:sceneView.pointOfView)
    constraint.isGimbalLockEnabled = true
    node.constraints = [constraint]

Inside Xcode, the screenshot below shows how you can change the rotation of the van using the euler angles (highlighted in red). The view should be “front” camera (also highlighted in red). The animation can also be done inside Xcode via the animation panel, select “rotate Action” & drag into the Actions timeline in the bottom -middle

enter image description here