How to animate a multiple connected shape

Hi everyone,
I have some questions, because I cannot figure out how to start my projects best.

First of all, I would like to program a selfbalancing (binary) tree. Daniel Shiffman tried it too, but the solution was not the best ( Video ) . My idea is to give every node, except the root, a specific type of negative electric charge, so the nodes repel each other. What do you think about this idea? (Mmmmmhh…May be I should fix the y-coodinate…, so they can move only horizontaly…)

My second problem: How do I get, let’s say the first two child nodes, move correctly, while they are stiff connected to the root with a fix length. Like a pendulum. So the complete visualisation is some kind of “more than double” pendulum?

This last thought leads to my third question: How would I, in general, animate a multiple connected shape, which i.e. falls an the floor and rebounds from it.

Do I define “one master node” with an array of conneced nodes? How can I guarantee, even when all nodes are falling down, they are having the correct distances to each other. Or do I better use a shape?

I searched a lot on the internet but couldn’t find a solution for the last problem. But I remember to have seen some animations on youtube. They are about generic algorithms and how a “worm” learns to “walk”. It’s less complex, but similar.

Any tip or help would be great,


I would start with a tree that has nodes separated by rigid bars of fixed lengths, like pendulums.Nodes connected in this way are then forced to be a fixed distance from their parent, and so movement would only be possible by rotating around the parent node.

Then work out the forces exerted on a node. Gravity pulls it down (except for the root node!). Other nodes repel it. Once you have calculated the sum of all the forces on that node, take the component of it that is perpendicular to the link connecting it to its parent, and then use that as the acceleration for its rotational velocity.

… That sounds super confusing. Maybe this diagram will help:

If you work this out for every node that needs to move, you can simulate them. Try it yourself! Post the code of what you come up with.


Thank you very much for your detailed description. I’ll try that.

1 Like