Channels ▼
RSS

Open Source

Writing a Particle System on the Raspberry Pi


The ParticleSystem Class

The ParticleSystem class is a container for the particle system. It contains three MutableArrays lists; a list of active particles, a list of manipulators, and a list of emitters. This class has the add methods:

- (void) addParticle: (BaseParticle *) particle;

- (void) addManipulator: (BaseManipulator *) manipulator;

- (void) addEmitter: (BaseEmitter *) emitter;

for adding (registering) these object types to the system. When the update method in ParticleSystem is called it:

1. Calls the update method on all emitters registered with the system possibly creating new particles in the process.

2. Passes the list of active particles to every manipulator registered with the system.

3. Iterates through the list of active particles and calls their update methods. If the particle indicates that it died during this update, its emitter is informed of its demise.

The ParticleSystemTest Class

The ParticleSystemTest class is a driver that shows how the Particle System code is used. It parses the command line it receives for a number (1 to 6) and runs the corresponding demo on the RPi. To try this out, when running in a remote SSH terminal window, first change to the RPi_ParticleSystem directory, then run the command "sudo ./pstest N" where N is the number of the demo you want to run. It is necessary to run this command as root to be able to create a frame buffer on the RPi remotely.

The available demos (in m4v format) are:

  • A shape emitter is positioned in the center of display window emitting pixels, circles, rectangles, and triangles of various colors. Dynamic sizing and coloration are utilized. Particles are emitted in all directions, at various speeds and with variable lifetimes.
  • A colorful explosion using the ONESHOT emitter mode with a rectangle emitter. A XYForceManipulator is used to simulate gravity. Particles are emitted within a -45 to +45 degree angle.
  • A circle emitter in ONESHOT mode used in conjunction with a boundary manipulator to cause the colorful circle particles to bounce around the display as they shrink in size, morph in color and eventually die.
  • A pixel emitter simulating a star burst.
  • An image emitter in METERED mode producing spheres that get larger and change color the closer they get.
  • A fireworks display with a moving Moon, a comet, fireworks bursts and roman candles. Not very realistic, but fun.

The following is code for the shape emitter extracted from ParticleSystemTest.m:

- (void) ps1Pattern {
        // Create shape emitter
        ShapeEmitter *se = [[ShapeEmitter alloc]
        initWithParticleSystem:particleSystem
        EmitterX:MIDX EmitterY:MIDY
        EmitterWidth:20 EmitterHeight:20];
        [particleSystem addEmitter:se];
        [se setMode:EMITTER_METERED];
        [se setBlank:true];
        [se setFilled:false];
        [se setColorVariability:true];
        [se setColorMode:CM_SPECTRUM75];
        [se setParticlesPerFrame:40];
        [se setStartSize:1];
        [se setEndSize:100];
        [se setMinAngle:0];
        [se setMaxAngle:360];
        [se setMinSpeed:2];
        [se setMaxSpeed:20];
        [se setMinTotalLifeTime:100];
        [se setMaxTotalLifeTime:150];
}

Can you see how this code creates what you see in the video? For a slightly more complex example, check out this code for the colorful explosion.

- (void) ps2Pattern {
        // Create a force manipulator to simulate gravity
        XYForceManipulator *fm = [[XYForceManipulator alloc]
        initWithXForce:0 YForce:.3];
        [particleSystem addManipulator:fm];
        // Create particle emitter
        RectEmitter *re = [[RectEmitter alloc]
        initWithParticleSystem:particleSystem
        EmitterX:MIDX EmitterY:MAXY
        EmitterWidth:20 EmitterHeight:20];
        // Create emitter/particle association with manipulator
        [re setManipulatorAssociation:fm];
        [particleSystem addEmitter:re];
        [re setMode:EMITTER_ONESHOT];
        [re setBlank:false];
        [re setFilled:false];
        [re setColorVariability:true];
        [re setColorMode:CM_SPECTRUM75];
        [re setParticlesPerFrame:40];
        [re setStartSize:14];
        [re setEndSize:1];
        [re setMinAngle:-45];
        [re setMaxAngle:+45];
        [re setMinSpeed:2];
        [re setMaxSpeed:25];
        [re setMinTotalLifeTime:10];
        [re setMaxTotalLifeTime:100];
}

Beautiful pattern, isn't it ?

Graphics Software Development on the Raspberry Pi

I came to enjoy programming in Objective-C after having developed apps for the iPhone. I decided early on that I wanted to use Objective-C on the RPi, especially after I found out how easy it was to set up.

The table below details the software, in addition to the OS image itself, required to compile and run the particle system code contained in the downloadable code archive. I assume here that you know how to setup a RPi for use.

Component

Version

Description

To Download / Install

SDL

"1.2.15"

A cross-platform multimedia library designed to provide fast access to the graphics frame buffer and audio devices.

sudo apt-get -y install libsdl1.2-dev then

sudo usermod -a -G video pi

SDL_image

"1.2.12"

An image loading library extension to SDL.

sudo apt-get -y install libsdl-image1.2-dev

SDL_gfxBlitFunc.c SDL_rotozoom.c SDL_gfxPrimitives.c

"2.0.24"

Component C modules from the SDL_gfx library which provides 2D graphics drawing primitives for SDL 1.2.

http://sourceforge.net/projects/sdlgfx/files/

objc

latest

Objective C compiler front end for gcc

sudo apt-get -y install gobjc

Note that I'm using Objective-C without any supporting frameworks like NeXTSTEP, GNUstep, or iOS, so as to keep this software lean. As I identify the need for some code normally provided by a framework, like MutableArray, I have coded it myself.

My Raspberry Pi Graphics Workstation

Because I'm always experimenting with graphics on the RPi, I decided to build a standalone workstation to facilitate my experiments. Figure 1 shows what I came up with. My workstation consists of a RPi Model B with 8G SD card and USB Wifi adapter, a bare bones 10" HDMI LCD monitor from adafruit.com, a forward-facing JPEG video camera (also from adafruit.com), and two power supplies — one for the monitor, and the other for the RPi controlled from a single switch on the back. I sandwiched the electronics between two pieces of lexan clear plastic to make the whole free standing and almost an object of art itself. I used 2" walnut spacers in each corner to hold the lexan pieces together.

Rapsberry Pi
Figure 1: My Raspberry Pi Workstation displaying a ray traced image.

Using the workstation, I log in to my RPi remotely via SSH (sometimes with 5 to 7 sessions) and do all of my editing, compiling, linking, debugging, and running while sitting in a chair with my MacBook across the room. This is really quite convenient for development as I can glance over at the display to see if my experiments worked. Enjoy!


Craig Lindley lives in the mountains of Colorado. When not messing around with electronics and computer projects, he plays in a rock-and-roll band.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video