2
votes

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.

1
That is not how casting works. You are not modifying the object that is casted. - David Rönnqvist
also, subclassing SCNNode is not a common pattern. What are you trying to do? - mnuages
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
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

1 Answers

3
votes

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;

}