Keeping rigidbody-objects connected to their parents

I am trying to create a water molecule simulation. In it, I have o atoms and h atoms as separate objects under a parent object. Each atom has a rigidbody and a control script. Atoms attract other atoms of opposite charge and repel same-charge atoms. The problem is that the atoms don’t stay connected, i.e., the molecule falls apart. I haven’t found anything online that works for this case.

Well, I found a workaround using a single rigidbody on the parent and using AddForceAtPosition(), I think that will also work.

@useralpha, interesting idea and problem!

It is not advised to have rigidbodies as children of other rigidbodies. I think you run into trouble as soon as you start arranging rigid bodies in parent child relationships because forces act on the rigidbodies individually, even if you apply a force to the parent. For example, inertia.

If I were approaching this - and this is spitballing an idea - I would keep all atoms as separate with no children. I’d look at using spring joints to keep atoms together. It seems to me that each atom has both a negative and positive attractive force, with some sphere of influence for each that would need to be represented, and I think you can use a spring joint with that idea, or create your own spring joint using those forces.

I also think there would need some kind of class or data structure - maybe a linked list or some kind of tree structure, or something - to represent each molecule, and when you apply a force to the overall molecule you traverse the structure to apply the forces to the molecule’s element atoms.