4

I need to associate SceneKit Node objects with arbitrary objects in my program and am looking for an optimal solution.

Here is an example of what I mean: Say I have a program that renders atoms in a molecule using SceneKit. I have classes Molecule and Atom that model my data. I then render the molecule using SceneKit. When I click on a sphere node in the Scene View, I need to know which Atom object that sphere represents (the Molecule contains an array of Atoms)

I could create a Dictionary that maps Node to Atom object, but wonder if there is a way to add an Atom object reference to the sphere node. Should one use Key-Value bindings?

I am very new to Cocoa programming and am looking for a nudge in the right direction for an approach. I can then research the implementation specifics.

Matt H
  • 155
  • 4

2 Answers2

2

How about starting with a couple of different SCNNode subclasses? The first is for your Atom, the second for your Molecule. Each MoleculeNode has one or more AtomNodes as children. AtomNode and MoleculeNode have weak references back to the Atom or Molecule they represent.

Now you can move or rotate a MoleculeNode easily, and all of the AtomNodes will move with it. Each AtomNode's geometry will stay fixed, relative to the parent MoleculeNode.

Hit testing will return both AtomNodes and MoleculeNodes. You can filter that result if you want, by either the node's class, or by setting the node's name to "Atom" or "Molecule". The SCNHitTestBoundingBoxOnlyKey might be useful if you want to be lenient about the precision of clicks required.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
  • Thanks, Hal for taking the time to read and reply. I'm frankly a bit embarrassed for not having thought of that approach. Since I am writing this program using a functional programming approach, had written my model classes first, and am new to SceneKit/Cocoa/Mac/iOS programming, it all conspired against me. I suppose I just needed to "get back to basics" and think this one through using fundamental OOP. I'll definitely follow your proposed approach. – Matt H Jan 01 '16 at 22:35
0

Just as a small alternative you can you a Map from SCNNode to ModelObject.

Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85