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.
Answer by useralpha · Mar 30, 2020 at 05:35 AM
Well, I found a workaround using a single rigidbody on the parent and using AddForceAtPosition(), I think that will also work.
Answer by streeetwalker · Mar 30, 2020 at 05:59 AM
@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.
Your answer
Follow this Question
Related Questions
child moving with parent and moving with friction ? 0 Answers
Unity Bug? Parent object rotates around child object. 1 Answer
Child with Rigidbody doesn't move with Parent with Rigidbody 3 Answers
3D Trigger collider slows player moved by forces 0 Answers
C# Changing a PARENT object's sprite with OnMouseEnter (on it's child object) 0 Answers