Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the real position of a sub node in SceneKit after rotation?

I am developing a scene with SceneKit. I have a main node with a subnode:

// Main node
SCNNode* planet = [SCNNode node];
planet.geometry = [SCNSphere sphereWithRadius:2];
planet.position = SCNVector3Make(0, -3, 5);
// sub-node
SCNNode* satellite = [SCNNode node];
satellite.geometry = [SCNSphere sphereWithRadius:0.4];
satellite.position = SCNVector3Make(4, 0, 0);
[planet addChildNode:satellite];
[scene.rootNode addChildNode:planet];

I use a NSTimer to make some actions and some animations. In the timer event I do that:

planetRotation += 0.1; planet.rotation = SCNVector4Make(0,1,0, planetRotation);

But if i try to get the position of the satellite node I always get the same value. I tried to get the positionnode to know the real position of the satellite node but nothing changes.

How can I get the real position of a sub-node when I change the rotation of the parent node?

Thanks in advance

like image 456
user2963707 Avatar asked Nov 06 '14 17:11

user2963707


2 Answers

the position of a node is expressed in its parent coordinate system. Just like for views in UIKIt/AppKit. If you change the frame of a view, the frame of its subviews does not change.

What you want is what we call the world transform of the subnode (i.e. its transform expressed in the coordinate system of the scene's root node).

You can have a look at worldTransform and -convertPosition:toNode:.

like image 163
mnuages Avatar answered Oct 22 '22 18:10

mnuages


The node's position won't change due to physics, that is why you aren't seeing it change. You need to call the node's presentationNode to get the position of the node as it is presented onscreen:

node.presentationNode.position
like image 7
Tom Pelaia Avatar answered Oct 22 '22 18:10

Tom Pelaia