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$ – user1118321 May 3 '18 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 May 3 '18 at 19:32

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$ Hey, thank you for this answer, I will accept this answer soon, by any idea, do you know how do I make my particles make a curve-fly like? For example I want it to be a fountain particle system. It now just emits upwards and fades out. I want them to fly a curve and lose energy over time as it almost hits the ground for example or half way to the ground. $\endgroup$ – HappyR May 5 '18 at 11:24
  • $\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$ – user1118321 May 5 '18 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 May 5 '18 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$ – user1118321 May 5 '18 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 May 5 '18 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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