Camera doesn't point to the place i want using SceneKit

Chao Li

i'm learning scenekit these days.but there's some problem.

i create an .scn file and put a sphere at (0,0,0) and i use these codes to put a camera on a node

let frontCamera = SCNCamera()
frontCamera.yFov = 45
frontCamera.xFov = 45
let topNode = SCNNode()
topNode.camera = frontCamera
topNode.position = SCNVector3(4, 0, 0)
topNode.orientation = SCNQuaternion(0, 0, 0, 0)
scene!.rootNode.addChildNode(topNode)
topView.pointOfView = topNode
topView.allowsCameraControl = true

when i run i cannot see anything until I click on my simulator and use this property , allowsCameraControl I set.

could you tell me where is wrong about my code?Thanks a lot

Hal Mueller

Creating an SCNQuaternion of all zeroes doesn't really mean anything. You've specified a rotation of 0, along an axis specified by an all-zero "unit" vector. If you try this modified version of your code, you'll see nothing has really changed after you try to change topNode's orientation. You're still rotating around an axis that is zero in all 3 components:

let topNode = SCNNode()
topNode.camera = frontCamera
topNode.position = SCNVector3(4, 0, 0)
print(topNode.orientation, topNode.rotation)
-> SCNVector4(x: 0.0, y: 0.0, z: 0.0, w: 1.0) SCNVector4(x: 0.0, y: 0.0, z: 0.0, w: 0.0)

topNode.orientation = SCNQuaternion(0, 0, 0, 0)
print(topNode.orientation, topNode.rotation)
->SCNVector4(x: 0.0, y: 0.0, z: 0.0, w: 1.0) SCNVector4(x: 0.0, y: 0.0, z: 0.0, w: 3.14159)

You've moved out the X axis 4 units to place your camera (topNode.position). In the usual orientation, that means 4 units to the right, with positive Y running from bottom of your screen to the top, and positive Z running out of the screen to your eye. You want to rotate around the Y axis. The camera's orientation is down its parent node's minus-Z axis. So let's rotate 1/4 way clockwise, and try setting the rotation instead (easier for me to think about than quaternions):

topNode.rotation = SCNVector4Make(0, 1, 0, Float(M_PI_2))
print(topNode.orientation, topNode.rotation)
-> SCNVector4(x: 0.0, y: 1.0, z: 0.0, w: -4.37114e-08) SCNVector4(x: 0.0, y: 1.0, z: 0.0, w: 3.14159)

You will probably find it helpful to log out, or even display, the camera node's rotation, orientation, and eulerAngles (they all express the same notion, just using different axes) as you manipulate the camera manually.

For completeness, here's the entire viewDidLoad:

@IBOutlet weak var sceneView: SCNView!

override func viewDidLoad() {
    super.viewDidLoad()
    sceneView.scene = SCNScene()

    let sphere = SCNSphere(radius: 1.0)
    let sphereNode = SCNNode(geometry: sphere)
    sceneView.scene?.rootNode.addChildNode(sphereNode)

    let frontCamera = SCNCamera()
    frontCamera.yFov = 45
    frontCamera.xFov = 45
    let topNode = SCNNode()
    topNode.camera = frontCamera
    topNode.position = SCNVector3(4, 0, 0)
    sceneView.scene?.rootNode.addChildNode(topNode)
    print(topNode.orientation, topNode.rotation)
    topNode.orientation = SCNQuaternion(0, 0, 0, 0)
    print(topNode.orientation, topNode.rotation)

    topNode.rotation = SCNVector4Make(0, 1, 0, Float(M_PI_2))
    print(topNode.orientation, topNode.rotation)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using iOS storyboarding I can't place label in spot I want on view controller

Moving a point in direction i want using Ncurses

I want to change song name by using forEach method but it doesn't change and doesn't show any error

Lightbox Doesn't work if i place it in a subfolder

CoordinatorLayout doesn't work like I want it to

Gitignore file doesn't work as I want

pushd with rd doesn't work as I want

Powershell doesn't work like I want it to

Can't get the value from a jagged array in the place I want but I get it on an other place

I'm using JavaFX and my combobox doesn't want to populate from an sql database

I want to pass variables into my VBA with python yield using xlwings but doesn't seem to work

I want to write ORC file using Flink's Streaming File Sink but it doesn’t write files correctly

I am using sdl in golang and want to get pixel data from window, but that doesn't work

why it doesn't work when I want to replace text by using hash keys& join function?

I want to take the input value from an HTML input block using PyScript, but it doesn't work. Why?

I want to place a script tag

I want to replace "-" for "\" but it doesn't work as I expected

Camera doesn't turn on until manually page is clicked using Puppeteer

Why doesn't 'perl -i' change a file in-place?

Animating background doesn't show up at the place I wish

How do I place a UI button on image target using AR Camera and Vuforia

why perl doesn't recognize i want to create an arrayRef with "split"?

Duration.between() doesn't work the way I want to

Python primefinder doesn't work the way I want it to

Why git rebase doesn't show commits I want to squash?

appendTo doesn't put my element where I want

Dependency Injection doesn't know about type that I want to inject

Why for-in loop doesn't print what I want?

CSS Hover Effect doesn't working as I want