I would like to move the camera around a SCNNode nodeA.
What I'have done so far is:
self.cameraNode.constraints = [SCNLookAtConstraint(target: nodeA)]
now if I increment the position.x of the camera the camera still look toward the nodeA. How to move the camera around the nodeA (like the moon around the hearth)?
The camera should move along a circular orbit (just a circle, I'm not interested in elliptical orbit)
Add the moon as the child node of the earth. That way translating the earth won't affect the moon's relative position. You can find an example in the SceneKit Slides for WWDC 2014 sample code.
Related
I am working on 3D project/unproject logic and I am learning some of the fundamentals. I went over this question:
Scene Kit: projectPoint calculated is displaced
In that question part of the shown code is:
//world coordinates
let v1w = topSphereNode.convertPosition(v1, toNode: scene.rootNode)
let v2w = topSphereNode.convertPosition(v2, toNode: scene.rootNode)
My question is, why is that needed? Why not just use v1 and v2 as points since they are already valid points 3D points in the scene? Why does the top sphere node's position need to be converted with respect to the root node's position?
since they are already valid points 3D points in the scene?
They are not necessarily valid 3D points in the scene, but 3D points in the local space of the sphere. The code you showed converts them to the world space, i.e. the scene space. This is important when the sphere is a child object, rotated, scaled, and/or simply not in the center of the scene.
Rather then using code from a question, check out the answers here:
How to use iOS (Swift) SceneKit SCNSceneRenderer unprojectPoint properly
I have a camera node in my SceneKit scene that is setup to allow the user to orbit by offsetting it's pivot:
self.cameraNode.pivot = SCNMatrix4MakeTranslation(0, 0, -100);
The user then rotates the node to orbit the camera.
What I would like to work out is
A) how to get the world space direction the camera is facing at any given time
and then
B) how to convert that direction to x,y,z components I can use to move the camera node relative to the way the camera is facing (forward/back/left/right from the camera's perspective).
Thanks!
I need to display some view with notification in my scene at given position. This notification should stay the same size, no matter what is the distance. But most important is that it should look like 2d object, no matter what rotation camera has. I don't know if I can actually insert some 2d object, this would be great. So far I'm experimenting with SCNNodes containing Box. I don't know how to make them always rotate towards camera (which rotates in every axis). I tried to use
let lookAt = SCNLookAtConstraint(target: self.cameraNode)
lookAt.gimbalLockEnabled = true
notificationNode.constraints = [lookAt]
This almost works, but nodes are all rotated in some random angle. Looks like UIView with rotation applied. Can someone help me with this?
Put your 2-D object(s) on an SCNPlane. Make the plane node a child of your camera node. Position and rotate the plane as you like, then leave it alone. Anytime the camera moves or rotates, the plane will move and revolve with it always appearing the same.
Ok I know how to do it now. Create empty node without geometry, and add a new node with SCNLookAtConstraint. Then I can move this invisible node with animation, and subnode stays looking at camera.
So I have a camera in my SceneKit project that is able to rotate and move around freely, and I have an object that, in some cases, needs to stay at a constant distance away from the camera and always be in the center of its view no matter how the camera rotates. Unfortunately, I'm new to SceneKit and don't know how to accomplish this.
So the key things I'm looking for are:
How to have the object always at the same distance from the camera
How to have the object always be in the center of the camera's view no matter what direction it's looking
At the moment, both the camera and the object (an SCNNode with a box geometry) are children of the same scene.
I'm coding in swift so I'd prefer an answer using that, but if you have a solution in objective-c, that works too.
Thanks a bunch!
Think about how you might solve this in the real world. Grab a two by four of the appropriate length. Use duct tape to attach the ball to one end, and to attach the camera (aimed at the ball) to the other end.
Now you can carry that rig around with you. The ball will always be in the center of the camera view, and will be a constant distance away from the camera.
You can build the same rig, virtually, in SceneKit. Create a new SCNNode to be the rig (taking the place of the two by four). Add the ball as a child node, at (0, 0, 0). Add the camera as a child node too, at (0, 0, 5) (camera looks down the -Z axis, so this position should put the ball in the center of the view). Now you can move the rig node anywhere in the scene you want, and you'll have a consistent ball position.
The user controls a snake moving on the surface of a sphere. I have the camera and the head of the snake attached to an imaginary sphere the same size as the visible one:
headNode.addChildNode(cameraNode)
controlNode.addChildNode(headNode)
scene.rootNode.addChildNode(controlNode)
The user taps two buttons to rotate the snake left and right. I use some basic trig to convert this "2D" rotation into its components which I use to rotate the controlNode every frame:
velocity.x = cos(rotation)
velocity.y = sin(rotation)
controlNode.eulerAngles.x += velocity.x
controlNode.eulerAngles.y += velocity.y
This works very smoothly when moving over the majority of the sphere but at the two poles of the sphere (along the y axis) the snake behaves strangely. Here is a gif that shows the normal movement along the majority of the sphere and then the strange movement at the poles:
the gif was too large to upload so it is on imgur
The way it moves is like it bounces off the poles and I cannot for the life of me work it out. If anyone has a suggestion of an easier way to do the task it would be much appreciated!
Gimbal Lock was the issue. I fixed it by using the transform component of the node.