<p>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:</p>

<pre><code>// 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;
</code></pre>

<p>In this case <code>timeDelta</code> 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…</p>

<p>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.</p>

CC BY-SA 4.0
2018-05-05T15:00:21.200Z
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.
user1118321
CC BY-SA 4.0
2018-05-05T15:59:08.337Z
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...
HappyR
CC BY-SA 4.0
2018-05-05T18:17:29.627Z
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.
user1118321
CC BY-SA 4.0
2018-05-05T18:42:15.237Z
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?
HappyR
CC BY-SA 4.0
2018-05-06T15:35:53.050Z
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.
user1118321
CC BY-SA 4.0
2018-05-08T11:35:32.403Z
Nice, I just had a mistake, they now go off in a shape, now I need somehow to randomize all particles such that it creates a nice effect like fire/smoke or something... Is there any way to manipulate the color to a fire color/smoke color?
HappyR
CC BY-SA 4.0
2018-05-08T15:51:41.887Z
That would be a separate question. Why don't you write that up and explain what you're looking for in more detail?
user1118321
CC BY-SA 4.0
2018-05-08T22:07:00.487Z
Okay, I will do so. one more question concerning the gravity:
It now drags my particles to one side, how can I randomize the direction in which it draws in, for example, I want few to drag to the right as well not only to the left.
HappyR
CC BY-SA 4.0
2018-05-04T05:21:19.980Z
2018-05-04T05:21:19.980Z
user1118321
<p>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?</p>

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

<p><code>uniform float spread;
vec2 vel = ...;
gl_FragColor = vec4(spread*vel, 0.0, 0.0);</code></p>

<p>(Part of my code)</p>

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

CC BY-SA 4.0
2018-05-03T02:02:38.400Z
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?
user1118321
CC BY-SA 4.0
2018-05-03T19:32:44.380Z
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.
HappyR
<glsl><webgl>
Gravitational calculation of a particle via GLSL / WebGL
HappyR
# Gravitational calculation of a particle via GLSL / WebGL

(Asked by: username_0 on 2018-05-01)

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...

**Tags:** glsl, webgl

> **Comments:**
> **username_1**: 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?  
> **username_0**: 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.  

---

## 1 answers

### Answer by username_1 on 2018-05-04. Score: 0 | Accepted answer

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.

> **Comments:**
> **username_1**: 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.  
> **username_0**: 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...  
> **username_1**: 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.  
> **username_0**: 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?  
> **username_1**: 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.  
> **username_0**: Nice, I just had a mistake, they now go off in a shape, now I need somehow to randomize all particles such that it creates a nice effect like fire/smoke or something... Is there any way to manipulate the color to a fire color/smoke color?  
> **username_1**: That would be a separate question. Why don't you write that up and explain what you're looking for in more detail?  
> **username_0**: Okay, I will do so. one more question concerning the gravity: It now drags my particles to one side, how can I randomize the direction in which it draws in, for example, I want few to drag to the right as well not only to the left.  

---

