DevLog

Galaxy

by BlueThen on Dec.04, 2009, under Processing App

Galaxy is another one of my Processing Apps. It’s another particle engine, but with the particles all rotating around the center on their own axis. Each particle’s speed is determined by the distance from the center, and can be effected by the user dragging the cursor left or right.

  • The particle count is about 5,000. The code uses a custom class object for each particle, for easy updating and management. In the class for the particles, a number of variables are used to define the properties of each particle.
  • The angle, to keep track of the position of the particle around the center
  • The radius, or distance from the center the particle is
  • dec and turnVelocity, both to decide the speed. dec is constant speed, where the particles closer to the center are faster (simply by multiplying by its distance inverted, 200-radius. We use 200 since that’s the max radius), and turnVelocity the speed of the particles controlled by the cursor’s x position, for when it’s dragged. turnVelocity is reduced by 5% every frame to simulate friction.
  • tilt is pretty much the tilt of the particle’s orbit.

When each particle is initialized, the angle is set to in between 0 and 6.28 (2Pi), since 2Pi is about a full circle, this should distribute particles somewhat evenly. Radius is randomly in between 70 and 200. The 70 allows a visible gap about 140 pixels in diameter at the center.  The tilt is between -60 and 60 (60 below and 60 above the origin), and dec is (200-radius) * 0.00014. Since dec is the rotational speed, and the rotation is measured in Radians, our values will be pretty small (again, in between 0 and 6.28). So we multiply by 0.00014 to get a desired speed. Multiplying by radius will have the speed determined by distance from center, and by subtracting radius from 200, it will invert the speeds, meaning that the particles closer will be faster than ones on the outside.

Every frame, the particle is updated. Here is where we apply the rotational algorithms, and update the coordinates of the particles. Before the update function is called, a turn rate is determined by the cursor. The program finds out whether or not the user is clicking, and if so, it finds the change in x coordinate, and plugs it in to the update function. This is done so the program can run a lot more smoothly. If we were to have these 2 lines of code in the update function, then it’d have to be recalculated for every particle.

The first thing the update function does is, update the coordinates. It uses the formula:
x = radius * cos(angle)
y = tilt + 20 * cos(angle + 3.5)
z = radius * sin(angle)

In the x part of the formula, cosine is used to find the x coordinate around the center the particle is, and radius is multiplied on to apply distance. In the y part, tilt is added on to move the origin to (0,tilt,0).  20 shows that the y varies by about 20 pixels. In the cosine of the y value, 3.5 is added to the angle. The reason here is simply to orientate the swirl to face the camera at a correct angle. The calculation for z is the same as x, but uses sin to achieve a circular orbit.

The next part checks to see if turn is 0. Turn variable is the value plugged in earlier. The value was determined by the mouse’s effect on Galaxy’s spin. If it isn’t 0, it is multiplied by (200-radius), then set to turnVelocity. This makes the values so it’s larger if the particle is closer to center, rather than farther away. If it were 0, which is prevented, then (200 – radius) * turn would return 0. We don’t want this because we want to apply our own stopping algorithm, which will make it seemingly slow to a halt, but that is applied after angle is recalculated.

We use the formula: angle -= dec + turnVelocity to determine the angle. dec is the constant rotation the Galaxy goes through, and turnVelocity determined by the user.

turnVelocity is decreased by about 5% to simulate friction, and to make it seemingly slow down to a halt.

The previous coordinate is set if the current coordinate isn’t already (for the first frame).

A line is then rendered isometrically using the previous and current coordinate. I explain my isometric algorithm in the Explode post. Afterwards, the previous coordinate is set to the current, for drawing the tail in the next frame.

:, , , , ,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!