I have a running particle system which emits particles in a given radius. Now I want to add some gravity to it. The particles and velocities of each particle is saved in a 2D Texture, now I want another 2D Texture, where I can save the gravity, but i'm missing the knowledge to do so. What's the actual formula on creating some gravitation or how can it be simulated?

For example to save particle's vel, I have uniform sampler2D pv; which is just an image which outputs an image and takes the output as input into the shader. (Kind of transform feedback)

uniform float spread; vec2 vel = ...; gl_FragColor = vec4(spread*vel, 0.0, 0.0);

(Part of my code)

And now I simply want another uniform sampler2D pg; which holds gravity and right here, I'm missing the math...

  • $\begingroup$ Do you want there to be a particular object that has gravity in the scene, or do you want all the particles to be gravitationally attracted to each other? Also, is your particle system 2D or 3D? $\endgroup$ Commented May 3, 2018 at 2:02
  • $\begingroup$ Hi, all of the particles should be affected by gravity, for example my particles just emits up right now, but I want them to fall off like if you fire water out of a hose. The system is currently in 2D. $\endgroup$
    – HappyR
    Commented May 3, 2018 at 19:32

1 Answer 1


Gravity is a form of acceleration. So what I've done in the past is continually add it into the velocity. On each frame, for each particle, I'd do something like this:

// Gravitational acceleration, adjust as necessary
const vec2 gravity = vec2(0.0, -9.8);

// Calculate the new velocity for the particle 
velocity = (particleVelocity + gravity) * timeDelta;

// Calculate the new position given the new velocity
newParticlePosition = particlePosition + velocity;

// Update our parameters for the next time around
particleVelocity = velocity / timeDelta;
particlePosition = newParticlePosition;

In this case timeDelta is the fraction of a second between the previous frame and the current frame. So if you're running your animation at 60 fps, it would be 1.0 / 60.0 = 0.01666…

I'm not sure that using a texture for the gravity would help you here as gravity is usually a constant when you're close to the surface of the planet, as it sounds like your particles are from the description given.

  • $\begingroup$ I think the first step would be to emit them with an initial angle that's just slightly off from directly up. So instead of an initial velocity of <0, 1> (or whatever), you could move the x part of the velocity vector by a small amount. Maybe a random amount between -0.1 and 0.1, for example, so the initial velocity vector is maybe <0.05, 1>, so there's some movement to the right for one particle, and <-0.02, 1> so there's leftward movement for another particle. Once you have gravity in there, they'll naturally form a parabolic arc. $\endgroup$ Commented May 5, 2018 at 15:00
  • $\begingroup$ I can't get the gravity to work, maybe you could take a closer look to my code... but I am working on it right now... Is there any way I could contact and chat with you about this? I think it would simplify things more for both of us... $\endgroup$
    – HappyR
    Commented May 5, 2018 at 15:59
  • $\begingroup$ If you want people to look at your code, please edit your question and add it in. That way when other people have the same question, they can find a complete answer in one place. $\endgroup$ Commented May 5, 2018 at 18:17
  • $\begingroup$ How can I see if my gravity thing is working?And how can I do what you suggested? Move the x-part of the velocity vector by a small amount...Does it work via Noise Texture? $\endgroup$
    – HappyR
    Commented May 5, 2018 at 18:42
  • $\begingroup$ I would start by making your particle birth rate really low, like only a few particles per second. I'd set the velocity vector for each particle to a constant like <0.2, 1.0>, and run the simulation. If you see the particles arcing off to the right in a parabolic arc, then you know it's working. And yes, to get random velocities, you'll either need to seed their initial velocities with random values or use some sort of noise technique to generate the randomness, such as a noise texture. $\endgroup$ Commented May 6, 2018 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.