Pages CS231 - Project 3 - Simulating a Galaxy with Cells

The task for this project was to use a linked list of cells to simulate different behaviors. The cells this time had decimal coordinates instead of integer. This allowed for much greater precision when simulating cell behavior. The double coordinates were converted to integer coordinates when the landscape was printed to the screen. The main goal was to create a cell that simulated clumping behavior. The cells moved randomly when not near other cells. When they were, they had a 1 in 100 chance of moving randomly. One challenge was keeping the cells within the boundaries. This could be solved either by keeping the cells themselves within the boundaries, or by only drawing the cells when they were within the boundaries. Another, third solution would have been to make the surface like a torus, like pacman where they would have gone out the right and come in on the left, the same with the top and bottom. I chose the easier second method. It simply involved checking each cell to see if it was within the bounds before drawing it. I know it sounds lazy, but it works out for the best in the end.

There's a lot to this project but I'll get to the interesting parts.

I created a graphical version of the simulation by extending JFrame. I wrote a paint method and used a buffered image to keep the image smooth.

For my Cell 3 I created altered the update state method to be attracted to all other cells. The cells also have a velocity in the x and y direction. I used trig to calculate all the relationships between these factors. The attractive effect of the cells decreased exponentially just as in real life. I had to remove this attractive effect if they were too close otherwise they would all fling off into space. This is due to the fact that it is an approximation.

In my graphic sim class I made a method that approximated a galaxy, the cells on the had an initial velocity tangent to the center.

Initially when I made this project the cells had an interesting rubber-banding effect. They would move outwards then back inwards in a uniform motion. I realised this was because I had the gravitational force between cells increase proportionally with the distance, instead of being inversely proportional. Although this effect was interesting, it was not what I was going for. I wanted to simulate gravity.

I've attached some cool pictures of the sumulation.

This one looks like a spiral galaxy!

This is a higher resolution photo of a galaxy that I took I realized the cells could be painted as a pixel when the "rectangles" are painted with a length and width of zero.

This one sort of looks like a helix

When the cells didn't have a limit to when they would stop attracting the center of the galaxy would often violently erupt in a random direction. This is because the simulation isn't continuous.

Labels