Particle Engine!

So, I want to share a cool little project I’ve been working on for the past few days. A particle Engine!
pastebin.com/vAs2ThAn Paste the code at this link into your code, and you’re ready to go!

The engine supports as many types of particles as you want, different ways of spawning in particles, different “behaviors” for what each type of particle does, “triggers” that will run a behavior only when a certain thing happens (such as a particle being deleted), ways to draw the particle on to the screen, and ways to randomize the particle’s look!

Here’s a firework simulation I made with it: pastebin.com/dMSKhxi7

This engine is also self-contained in a single class. If you really want, you can have multiple simulations running at once! If you want an explanation of how to use this, I’ll help.

It is very cool, thanks for sharing Valgo! :smile: :blush: How do I get started with customising some different behaviours and looks for the particles? :smiley: I see there are a lot of things you’re setting in setup()

@t1_ first thing: you can add a draw layer to a particle using
p:addDrawLayer(name of particle, color (can have alpha), size, Z layer (higher is above lower), color vary, size vary)

name of particle is the name of the particle type this draw layer is for. If the particle type doesn’t exist, it will automatically create it.

color is the color of the draw layer.

size is the width (in pixels) of the ellipse that the Draw layer creates.

Z layer determines what particle layers show up on top of others.

color vary (optional) is the amount the draw layer color can vary.

size vary (optional) is the amount the draw layer size can vary.

Here are some essentials you need to do for this to work:

  • Set a variable in your code to ParticleSim. In my examples, that variable is p.
  • In your Draw function, make sure to use p:draw()

That’s pretty much all the setup you need to do before you start adding stuff!

Next is how to spawn particles:

Use p:addSpawner(type, spawner type, parameter table)

type is the type of particle this spawner will affect.

spawner type is the type of spawner you are adding to the particle. The types are listed below.

parameter table is the table of paremeters given to the spawner. Every parameter will default to something if you don’t define it. It is always good to have a table of some sort (even an empty one) here.

Parameter table format:
{[name of parameter 1 (string)] = value 1, [name of parameter 2 (string)] = value 2, etc…}

Types of spawners:

touch_began: will spawn particles when the screen is first touched, at the place where you touch it. No parameters.

touch_move: will spawn particles when the touch is moved, at the place of the touch. No parameters.

touch_end: will spawn particles when you stop touching the screen, at the place of the touch. No parameters.

loop_random_pos: will spawn particles on a time interval at a random position.
Parameters:
Time: the amount of frames between particle spawns. Defaults to 20.

loop_set_pos: will spawn particles on a time interval at a set position.
Parameters:
Time: the amount of frames between particle spawns. Defaults to 20.
X, Y: the position of the particles that will spawn. Defaults to the center of the screen.

chance_random_pos: will spawn particles with a chance each frame at a random position.
Parameters:
Chance: the chance of a particle spawning in any given frame is 1 / this. Defaults to 15.

chance_set_pos: will spawn particles with a chance each frame at a set position.
Parameters.
Chance: the chance of a particle spawning in any given frame is 1 / this. Defaults to 15.
X, Y: the position of the particles that will spawn. Defaults to the center of the screen.

Now you know how to spawn particles. Next will be how to make particles do things!

Particle behaviors (particles doing things):

Use p:addBehavior(particle type, behavior type, parameters, trigger)

particle type is the type of particle this behavior will happen in

behavior type is the type of behavior the particles will do.

parameters is the parameter table (look above if you don’t know what that is)

trigger I’ll go into more detail about this later, but this allows you to only have certain behaviors happen when certain events happen.

Behaviors:

timeout: This behavior will make a particle delete itself after a set amount of time.
Parameters:
time: the amount of frames that the particle will delete itself after.

chance_timeout: This behavior will make a particle have a chance to delete itself every frame.
Parameters:
chance: Each frame, the particle has a 1 / this chance to delete itself.

spawn_particle: This behavior will make a particle spawn another particle after a set amount of time.
Parameters:
type: the type of particle that will be spawned in. Defaults to the type of particle that spawned it in.
time: the amount of time from the first particle’s creation to the spawning in the next particle.
amount: how many particles will be spawned in

chance_spawn_particle: This behavior will make a particle have a chance to spawn another every frame.
Parameters:
chance: the chance that another particle will spawn in a given frame is 1 / this.
type: this is the type of particle that will spawn in. Defaults to the same type of particle that spawned it in.
amount: how many particles will be spawned in

loop_spawn_particle: This behavior will make a particle spawn in another on a time interval.
Parameters:
time: the amount of frames between particle spawn.
type: this is the type of particle that will spawn in. Defaults to the same type of particle that spawned it in.
amount: how many particles will be spawned in.

NOTE: When a particle is spawned from another, it will keep the same velocity. To disable this, use the “init_velocity” behavior.

wander: this will make a particle jiggle around in a smooth way.
Parameters:
amount: how intense the jiggling is.

move_straight: this will make the particles move in a straight line.
Parameters:
dir: the direction of the particles’ movement. 0 degrees is right, 90 degrees is up, etc. defaults to 90 degrees (up)
speed: how fast the particles will move.

gravity: this will cause gravity to be applied to a particle.
Parameters:
x, y: the forces that will be applied to the particle.

timeout_radius: will cause the particle to despawn if it goes too far away from the center of the screen.
Parameters:
radius: the max distance a particle can be without despawning.

loop_change_direction: Will make the particle “zig-zag” around the screen, changing directions on a time interval
Parameters:
speed: the speed that the particles will move at.
time: the amount of frames between direction changes.

chance_change_direction: Will make the particle “zig-zag” around the screen, changing direction randomly.
Parameters:
chance: the chance that the particle will change direction on any given frame is 1 / this.
speed: the speed that the particles will move at.

init_velocity: Causes a particle to start at a specific velocity when it spawns.
Parameters:
x, y: the x and y velocity the particle will start at.

Extra stuff:

Random per-particle properties: If you want to randomize a property for each particle (for example, wander Speed), instead of using a number for a parameter, you can use a table of 2 numbers. Each particle will be given a random number in that range for its parameter.
For example, you could do parsim:addBehavior(“my_particle”, “wander”, {[“amount”] = {3, 5}}). Each particle of type “my_particle” will now wander with a speed somewhere from 3 to 5. It will be different for each particle.

Triggered events: There is a 4th parameter to the p:addBehavior() function. This is called a trigger. When left blank, the event will happen every frame. But for example, if you put in “spawn”, the behavior will only happen when the particle first spawns. Keep in mind that triggered events only work when there is at least 1 non-triggered event.

Types of triggered events:

spawn: will trigger when the particle first spawns

duplicate: will trigger when the particle spawns another

despawn: will trigger when the particle times out. This is good for replacing a particle with another.

Wow that is awesome! I can really see how much customisation you’ve put – so much to play with! And you must have put a lot into documenting all this; it’s very helpful and clear the way you organised it :blush: Haven’t gotten to play around with it much at the moment, but you bet I would love to :tongue: