Caleb
Caleb

Reputation: 305

How to place 3D object correctly in ARSCNView using ARKit?

I am long time trying to place 3D object in correct position in SceneView, the object adding in SceneView correctly, but its position turning right or left side,

func addObject(x: Float = 0, y: Float = 0, z: Float = -0.5) {

    guard let shipScene = SCNScene(named: "art.scnassets/house.scn"),
        let shipNode = shipScene.rootNode.childNode(withName: "house", recursively: false)
    else {
        return
    }
    shipNode.scale = SCNVector3(0.01, 0.01, 0.01)
    shipNode.position = SCNVector3(x,y,z)
    sceneView.scene.rootNode.addChildNode(shipNode)
}

This is the code I added, whether this issue related to camera position, I have changed that too, it doesn't seem work. Kindly refer attached image for example.

enter image description here

How to place the object correctly?

Upvotes: 2

Views: 2203

Answers (1)

PongBongoSaurus
PongBongoSaurus

Reputation: 7385

Based on your question, it seems that you need to adjust the rotation of your model so that it will be positioned correctly.

Seeing as it is an scn file you should be able to do this in in the SceneKit Editor within Xcode by adjusting the Euler Angles of the model.

Which for your information refer to the following:

Pitch (the x component) is the rotation about the node’s x-axis.

Yaw (the y component) is the rotation about the node’s y-axis.

Roll (the z component) is the rotation about the node’s z-axis.

enter image description here

Here is an example, illustrating the effects of changing the X Eular Angle within the SceneKit Editor:

enter image description here

enter image description here

If you wanted to do this programatically you can do this a number of ways:

(a) By Modifying the Eular Angles Of The Model e.g.

shipNode.eulerAngles.x = 90
shipNode.eulerAngles.y = 45
shipNode.eulerAngles.z = 90

(b) By Using An SCNVector4 which is a:

four-component rotation vector specifying the direction of the rotation axis in the first three components and the angle of rotation (in radians) in the fourth.

As such an example of using this would be as follows:

///Rotate The Ship 45 Degrees Around It's Y Axis
shipNode.rotation = SCNVector4Make(0, 1, 0, .pi / 4)

In order to make life a bit easier, you can also use the following extension so that you can write your desired rotation in degrees and then convert to radians which is needed for an SCNVector4 e.g:

shipNode.rotation = SCNVector4(0, 1, 0, Float(Int(45).degreesToRadians)))

extension  CGFloat{

  /// Converts Degrees To Radians
  var degreesToRadians:CGFloat { return CGFloat(self) * .pi/180}

}

Hope it helps...

Upvotes: 5

Related Questions