# Generating 2D Trees

As usual, I am writing about something I did a while ago, but just now got motivation to write about.

Recently, when I had some spare time on my hands I decided to create a 2d model of a tree.

The final result looks like this:

## Spring Mass Model

I used a spring mass model, which, as the name suggests, uses points of mass, and springs. It is a simple idea: There are a number of discrete mass points on which the forces of the system are applied. Then, there are springs connecting pairs of points, and, depending on the distance between them, apply a force to push them apart or pull them together.

With only this simple system it is already possible to simulate soft body physics.

### Angular Springs

The usual spring mass model only has simple point-to-point springs. To model a stiff rod, you'd need to create a lattice of springs connected to each other.

I was too lazy to generate lattices in various shapes, so I instead added *angular springs*,
which control the angle between three mass points.
It also simplifies the whole physical model and improves its performance.

## Forces

### Gravity

As expected, it needs some kind of gravity such that the springs don't just randomly float around.

With only that and the springs, it is possible to simulate n-pendulums!

By adding some angular springs at each joint, it is quite easy to change the pendulum into a rod that is kind of stiff, but still bendable:

### Wind

Wind was also quite fun to add to the simulation:

The basic idea was that wind acts on connections between two mass points, which are kind of like a sail. The more perpendicular the sail is to the direction of the wind, the stronger the wind acts on it. Also, the wind only creates forces in the direction perpendicular to the sail.

Since there are already a whole bunch of connections between mass points, namely the springs, I decided that those should also be the sails.

The result looks like this (green dots representing wind):

Now the pendulum looks like a towel flapping in the wind.

## Generating trees

The tree generation is quite naive. It starts with a root stem, and recursively creates a number of smaller branches that are attached to this branches end. The branches are just springs, and the connection between branches is additionally supported by angular springs, so the tree stays roughly in the initial shape.

## Graphics - It's all squares!

The graphics of the trees are rather simplistic: I create a bunch of squares, each storing its color, and their relative position to some spring. Then, when the tree moves, it is really easy to calculate the new position where to draw the square.

For the branches I create squares close to the spring, and in some brownish color. For the leaves, I generate squares further away from the spring, and only for branches that are below a certain size, which are only the upper ones.

The background is a simple gradient as it looks rather bland otherwise.

## Some more Trees!

## Source

As so often with programming projects of mine, this project too is on GitHub.

https://github.com/JM4ier/rods

## Potential future work

This was really fun to program, but there are a few flaws in the program.

Rendering all squares is *really* slow, I struggle to get a constant 60fps.
To render more trees it would surely be beneficial to do actual GPU rendering.

Also, the physics simulation is far from perfect, sometimes it looks like every point of mass in the simulation gets "shocked", and everything moves really fast suddenly. Sometimes that even removes the entire tree! I think this is because I do a really poor job solving the differential equations that arise from the physics system. Something like the Runge-Kutta Methods might help.

**Tags:**CODING