Advice on particles

I’ve been creating a particle system, but I honestly don’t know what I’m doing. Sometimes it works, other times it doesn’t. Currently, it doesn’t even render (and apparently it doesn’t get randomized, even though the randomize function is called). What I currently have is a struct of a single particle, and an array of those particles. I’m using the random function to randomize some properties (x, y, size, etc) and another function to move the particles. I’d appreciate any advice or help with this, thanks!

I haven’t looked very thoroughly, but I’ve spotted at least one of your problems: random doesn’t work on floats.

Your floats are being implicitly converted into longs, and since long is an integer type that means 0.1 and 0.3 are both being converted to 0, which means you’re effectively calling random(0, 0), which will end up returning 0 because of how random is implemented.

(Remember me mentioning the other day how C++ inherited C’s conversion rules and how they’re a bit daft? This is a prime example of how and why implicit conversion is usually a bad idea.)

There’s a few other things that appear a bit muddled, like the fact you’re calling render twice, and you have both a direction and velocity.


You might have an easier time if you have a look at my Physix demo and Stars demo.

The Stars demo isn’t very well organised because it was something I threw together quickly for the sake of demonstration.

(The offsets array exists because I did this with fixed points instead of floating points and thus didn’t have sin (sine) and cos (cosine) functions. Also there’s a few places where I’m cheating a bit in ways that wouldn’t work with floats.)


I may be able to offer more specific advice if you explain what the particles are supposed to be doing/what effect you want to achieve. E.g. whether they’re supposed to be like fireworks (explode out in a circle then slowly fall), or rain (just fall slowly downwards).

1 Like

That’s my bad, everything is just messy and all muddled up and I was just trying to find what works. Is this maybe a case for static_cast<>()? (If so, then that’s funny how it works)

Sort of like an explosion in the sky, but the particles fall downward after gaining a bit of height. The sides also do come out slightly

That seems both oddly specific and an unusual pattern to want to achieve.

I’m struggling to imagine how it would look. Is it supposed to emulate anything in particular?

To find what works, start by finding the simplest thing that could possibly work.

Pick your big problem apart into smaller problems and tackle the smaller problems one at a time.

That way your solutions won’t interfere with each other while you’re working out if they’re correct or not.


Edit:

In case that’s a big vague…

Don’t scrap your current code, stash it away somewhere for now.

Create some new code, but this time start it simple with just position and velocity, and don’t worry about randomising them for now. Get the particles moving with velocity. Then add gravity. Then gradually build up other things as you need them.

Don’t presume that you will need something, just add things as a need for them arises.

Make a note of what your problems are and how your code solves those problems.

You may find the solutions simpler than expected.


That depends on how you’re thinking you might use it.

If you’re thinking static_cast<float>(random(static_cast<long>(0.1), static_cast<long>(0.3))) then no, that’s what’s happening already.

If you’re thinking that static_cast might be needed for implementing a version of random that accepts floats, then you’d be on the right track.

1 Like

Good idea. I sometimes do that when the code gets so impossibly messy and makes no sense.

How could I do something like that?

Worry about getting the logic working for now.

I’ll provide you with some random functions that can handle floats when you need them, or when I next have chance to type them up.

If you’re feeling daring then you can look it up yourself, but the randomisation should be pretty low down on your priority list compared to actually getting the logic working.

(Truth be told I had to look it up myself because the solution is a bit mathsy and not exactly the kind of thing I do very often. Desktop C++ actually has built-in functions for getting a random floating point, which Arduino lacks.)


If you can’t get your particles working then I’ll help you get them working after you’ve had a go at it, but I don’t want to hand you too many answers when it comes to the actual problem solving side of things because problem solving is actually what programming is really about.

(Programmers are problem solvers first and foremost. The program is merely how you express the solution to the problem in a way that will be understood by both the computer and other programmers.)

1 Like