Search code examples
iosswiftscenekit

Connecting multiple SCNObjects in Swift Scenekit with cylinders/paths


I'm working on an app in Swift that will display 3D animations of chemical reactions. I'm modeling the individual atoms with SCNSpheres and I'm trying to figure out what will be the best way to model the bonds between the spheres. I've tried using SCNCylinders and UIBezierPaths, but I'm hitting a wall with both.

With the cylinders, I feel like the best way to do it would be to constrain the top and bottom of the cylinder to the two spheres it's supposed to connect, but I haven't found a good way to do that. Also, dealing with the rotation of the cylinder is proving to be a lot more complicated than I was expecting.

With the UIBezierPath, this seemed to work more like what I was wanting, but it only functions along a 2D plane.

Is there some way I should be doing either of these approaches differently? Or is there another way that would be better? The bonds will need to move as the atoms move and potentially undergo visual changes as they make/break.


Solution

  • This post: How to draw a line between two points in SceneKit? (Answer Cylinder line) should help you draw cylinder lines between two 3D points points. It handles the rotation for you, so there is less to worry about.