2

I am importing a simple dae file. I want some of the nodes to be a subclass of SCNNode - MySCNNode.

 MySCNNode *node = [scnView.scene.rootNode childNodeWithName:@"Box1" recursively:YES];
 //additional initialization goes here 

Tried casting to (MySCNNode *) too.
But this is not working. "node" is still an SCNNode. Why?

I need to add a few properties and methods to SCNNode. So I subclassed SCNNode. I want the nodes from the scene(imported from a dae) to have the properties and behaviour. The nodes from the scene is always SCNNode. I want it to be of class MySCNNode.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
sambro
  • 816
  • 5
  • 12
  • That is not how casting works. You are not modifying the object that is casted. – David Rönnqvist Apr 14 '15 at 07:49
  • also, subclassing `SCNNode` is not a common pattern. What are you trying to do? – mnuages Apr 14 '15 at 12:47
  • I need to add a few properties and methods to SCNNode. So I subclassed SCNNode. I want the node from the scene to have the properties and behaviour. The node from the scene is always SCNNode. I want it to be of class MySCNNode. – sambro Apr 14 '15 at 14:04
  • Why is subclassing a SCNNode not a common pattern ? where does it say is not a good practice ? Where can I read more about this ? – omarojo May 04 '18 at 01:18
  • It's extremely normal to subclass SCNNode. I have never seen one not subclassed. – Fattie Nov 11 '22 at 19:47

1 Answers1

3

I understand needing a subclass. And I understand why it's atypical. In my case I'm making a RTS and am creating it's "Mission editor" so I can take 1 scene filled with the various objects created in blender and build custom scenes in the editor. So I need to know when tiles are buildable, passable (and on which level), etc. This may not be perfect but it should work:

+(instancetype)mySCNNodeWithNode:(SCNNode*)node{

SCNVector3 min,max;
[node getBoundingBoxMin:&min max:&max];
MySCNNode *newNode = [MySCNNode node];
newNode.position = node.position;
newNode.rotation = node.rotation;
newNode.transform = node.transform;
[node setBoundingBoxMin:&min max:&max];
newNode.geometry = [node.geometry copy];
SCNMaterial * material = [node.geometry.firstMaterial copy];
newNode.geometry.firstMaterial = material;

return newNode;

}

April
  • 66
  • 3
  • Hi I am also trying to make an RTS like 3D game, Bit I am encountering new ans new performance issues - Is SceneKit enough for RTS ? – Coldsteel48 Dec 29 '16 at 21:51