0

I am very new to SceneKit and your help will be really appreciated!
I have a 200x200 sized SCNView in my UIView, which is at the centre of super view. I want to put a SCNCylinder inside, such that the SCNCylinder covers full SCNView. I read that all these views of Scenekit are defined in meters, so how do I form a relationship between the dimensions of my screen and the SCNCylinder.

I tried:

var coinNode = SCNNode()
let coinGeometry = SCNCylinder(radius: 100, height: 2)
coinNode = SCNNode(geometry: coinGeometry)
coinNode.position = SCNVector3Make(0, 0, 0)
coinScene.rootNode.addChildNode(coinNode)

let rotate90AboutZ = SCNAction.rotateByX(-CGFloat(M_PI_2), y: 0.0, z: CGFloat(M_PI_2), duration: 0.0)
coinNode.runAction(rotate90AboutZ)
ibOutletScene.scene = coinScene

But this leaves a margin between my coinScene and the ibOutletScene. How do I remove this space?

I also tried adding Camera:

let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3Make(0, 0, 100)
coinScene.rootNode.addChildNode(cameraNode)

But I see random behaviour with this and the coinNode gets hidden! How should I position my camera? Or is there any other way to remove extra space from my ibOutletScene?

Edit: This is how it looks if I don't add camera. There is a margin between red scene and green coin. I tried multiple sizes for the coin, but I am unable to remove this margin unless I add a camera. But, If I add camera, I get another problem, mentioned below this screenshot. Red color is the scene and green coin, how can I remove the margin between the two

If I don't add the camera, The rotation animation on coin works perfectly, but If I add camera,the rotation enlarges the and then becomes small again with the animation. How can I rotate it on its axis, without increasing the size? I am using following code to rotate the coin: The same code works fine without camera, but enlarges the coin after adding camera. Checkout the snapshot.

let rotate = SCNAction.rotateByX(0, y: -2 * CGFloat(M_PI_2), z: 0,    duration: 2)
    coinNode.runAction(rotate)

enter image description here

Anshuman
  • 46
  • 6

1 Answers1

0

The random behavior might be caused by the last line in your first code snippet. You're starting an animation, and then adding the scene to the view.

Instead, build your scene, attach it to the view, and then start your animation. Setting a non-zero duration for the action will give you a more pleasing transition.

As for the extra space, it would help us understand if you post a screenshot. But you're going to have to do a bit of trigonometry.

It looks like you have a scene that you want to be blocked by a coin, that then rotates out of the way? Simulate that yourself with real objects. Put your eye down at the edge of your desk. Put a coin out a ways from your eye. How far does that coin have to be in order to block particular objects farther away on your desk?

In SceneKit, you can query the field of view of the SCNCamera. You know the size of your coin and the size of the view. Calculate the distance from the camera needed for the projected diameter of your coin to equal the width of your view. Put the coin there.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
  • That animation is for aligning the cylinder on XY plane, hence 0 duration and that line is working fine. – Anshuman Oct 22 '16 at 13:13