How to get started with Particles?

So, I know this is a pretty broad question, but is there a super simple example of how I could create a particle class and then spawn it so that it updates it’s position on it’s own and eventually despawns?

Any references or examples online I should be looking at?

(Good questions to ask 2/3 way through a game jam)

Lol, I did a shortcut but you could try first to simulate parabolic movement. 1 particle, normal gravity, the standard movement equation from school (just newton :stuck_out_tongue: invalid for speed of light velocities, but safe for now). And then add multiple of these being generated and killed.

Something like …

struct Particle {

    private:

        int16_t x = 0;
        int16_t y = 0;
        uint8_t counter = 0;
        uint8_t direction = 0;

    public:

        int16_t getX()                          { return this->x; }
        int16_t getY()                          { return this->y; }
        int8_t getCounter()                     { return this->counter; }
        uint8_t getDirection()                  { return this->direction; }

        void setX(int16_t val)                  { this->x = val; }
        void setY(int16_t val)                  { this->y = val; }
        void setCounter(int8_t val)             { this->counter = val; }
        void setDirection(uint8_t val)          { this->direction = val; }

    public:

        void update() {

           // move your particle based on direction, whatever ..
          ...

          if (this->counter > 0) this->counter--;

        }

        bool render() {

            return (this->counter > 0);

        }

};

Create an array of particles, randomly set the X, Y, counter and direction.

Call update() every frame.

Render the particle every frame if render() returns true;

I have made the x and y big enough that the particle can move outside of the screen. It takes more memory though.

You can also change the update() function to render the particle itself. This will allow you to remove the render property.

If they ever taught us that I undoubtedly wasn’t listening. :P


Ideally you don’t really want each particle to be responsible for updating its own position.

Particles should just be ‘bags of data’, it should be the particle system that dictates how the particles behave, that way you can use different systems on the same particles to achieve different behaviour, and perhaps even do crazy things like combine systems e.g. by sequencing their behaviours or picking behaviours at random.

How the behaviour should actually be implemented depends on what kind of a particle system you’re going for. A lot of particle systems tend to involve a physics simulation in which each particle has a position, velocity, mass and possibly acceleration, and they will probably be affected by external forces such as gravity or a force generated by the particle emitter. Typically particles also have a lifetime to restrict how long they run for.

It’s also worth noting that particles tend to be pooled so that dead particles and be recycled and there’s no time wasted allocating and deallocating particles.


See also:

  • Physix - A simple physics demo without collisions
  • Stars - A simple particle system demo
  • Objects Should Not Draw Themselves - Several references discussing the advantage of having a ‘controller’ class/object that implements the behaviour for multiple other objects that all behave according to the same rules

I think once I can get the code base to generate and iterate through the particles, I can ham-fist the movement code.

It looks like @filmote has given me the tools I need!

My game is AWESOME, I love it. I stayed up until 1am last night making it. I hope I can finish it. I’m supposed to be doing other things right now!

UPDATE: We have working particles, now to do something interesting with them.

Anyone want to jump in with a hint on how to this with a simple example? :grin:

it is like @Pharap Physix code, but on every update you alter x and y velocity with new_y_velocity = initial_vy - gravity * some_time_variable

But dont worry about that for now…

Homework I:
Get a point moving on the screen, but using some class structure like the one proposed by @filmote

2 Likes

I’m way past that I’m moving it around just with simple maths now.

Next: figure out how to do LINES

Shouldn’t it be more like particle.velocity -= gravity * deltaTime;?

(I always find it confusing when equations talk about ‘initial’ but actually mean ‘previous’.)

In which case that’s almost exactly what I’m doing with gravity in Physix,
except I’m doing it without the delta time, and I’m factoring in the object’s mass.

Accumulating the delta time and then using a fixed physics step instead tends to get more stable results, or at least according to what seems to be the holy book on the matter.

(Or just assuming that each frame is going to be close enough to 1/60s that it won’t really matter, as I lazily did. :P)

There probably ought to be a coefficient of friction in there somewhere as well, but maybe that’s going overboard?


You’re attempting hair particles?

Bro we got particles!

Speed lines bro!

Thank you for the help!

Gonna shelf lines for now (really it’s just connecting 2 particles on the same path)

And go for… camera shake!! add offset variables to every drawing operation, GO!

Uh oh, my shitty programming is running me out of memory… ok maybe 50 particles might be too many for right now…

UPDATE: Camera shake is added.

Back to that line idea… Almost done with the game. It’s … the greatest Arduboy game ever created…

UPDATE: Hey this Arduboy thing is pretty cool have you ever tried making a game with it?

1 Like

It’s always nice to see newbies enjoying programming their Arduboy for the first time. I’m sure you’ll find lots of tutorials, info and help in this forum! :wink:

1 Like

OK! My game is FINSHED!

HUUUUGE thanks to @pharap @filmote and @MLXXXp!

Gonna work on the post now. The game is called “New Blocks on the Kid”

3 Likes

So is it a Jam entry?

Yeah and you can vote on it but it’s not eligable for a prize, whatever the rankings are it will proceed as if the game wasn’t actually there. (assuming it doesn’t come in last, in which case it will overflow the rankings and I will come in first)

A terrible and often quoted joke on computer forums:

Kevin find a Magic Lamp. He rubs it and a genie pops out.
The genie says “You have three wishes.”
Kevin thinks for a few seconds and then says “I wish I have zero more wishes.”
Genie replies “You have 255 more wishes.”

Shouldn’t that be “I wish for four less wishes” (or maybe “three less”)?

+ 0 is a nop.


There are two difficult problems in programming:

  • Naming things
  • Cache invalidation
  • Off by one errors

wishes = 0; isn’t a NOP.

The important thing in this joke is that it is the genie that forces the overflow, not Kevin.

Would it you if help if I changed the text to read “I wish I had zero wishes.” ? :slight_smile:

That makes more sense.

Check out the results of this thread here: New Blocks On The Kid

1 Like