<p>I'm trying to create a galaxy simulation using opengl with the compute shader and when I start my simulation, the galaxy with the spiral arms doesn't form, instead it goes into chaos. I noticed that the particles in the center are not spinning fast enough
Does anyone had this problem before ?</p>
<p>I tried to:</p>
<ul>
<li>change mass and smooth value</li>
<li>using verlet integration</li>
<li>change velocity initial value</li>
<li>change the number of particles</li>
</ul>
<p>here is the code:</p>
<pre><code>#version 430 core

layout (local_size_x = 128, local_size_y = 1, local_size_z = 1) in;

struct Particle
{
    vec4 position;
    vec4 velocity;
    vec4 acceleration;
};

layout (std430, binding = 0) buffer ParticleBuffer {
    Particle p[];
};

uniform float dt;
uniform float accuracy;

  const float mass = 1e-5;
  const float csmooth = 1e-6;

vec3 compute_force(vec3 posi){
    vec3 f = vec3(0);
    for(int i = 0; i&lt;accuracy*p.length(); i++){
        if(posi != p[i].position.xyz){
            vec3 v = p[i].position.xyz - posi;
            float d = length(v);
            f+= mass*v/(pow(d,3)+csmooth);
        }
    }
    return f;
}

void main() {
    uint index = gl_GlobalInvocationID.x;

    vec3 pos = p[index].position.xyz;
    vec3 vel = p[index].velocity.xyz;
    vec3 accel = p[index].acceleration.xyz;

    vec3 newPos = pos+vel*dt + accel*(dt*dt*0.5);
    vec3 newAccel = compute_force(pos);
    vec3 newVel = vel + (accel+newAccel) *(dt*0.5);

    p[index].position.xyz = newPos;
    p[index].velocity.xyz = newVel;
    p[index].acceleration.xyz = newAccel;
}
<span class="math-container">```</span>
</code></pre>

CC BY-SA 4.0
2022-11-04T23:04:18.840Z
I am not sure, but please try to delete the accelerations term out of the newPos calculation...
Thomas
CC BY-SA 4.0
2022-11-06T12:10:03.270Z
seems like nothing really changed
Sedenion
CC BY-SA 4.0
2022-11-06T13:40:24.843Z
Can you go more into detail, how the particles behavior is? Are they 'jumping' or are they all go through a single position?
Thomas
CC BY-SA 4.0
2022-11-06T13:45:33.970Z
The velocity therm also looks strange to me: only add one acceleration value. I think 'accel' is the correct one.
Thomas
CC BY-SA 4.0
2022-11-06T13:49:56.950Z
BTW you are using the Euler numeric integration algorithm. It's accuracy is very bad... A better integrater is the 'Runge-Kutta' algorithm
Thomas
CC BY-SA 4.0
2022-11-11T14:27:25.890Z
the particles are mostly spinning (with initial velocity) around a center at different speed however it's unstable and goes into chaos
Sedenion
CC BY-SA 4.0
2022-11-11T14:30:57.247Z
I used the code
`
void update(double dt)
    {
        Vec3d new_pos = pos + vel*dt + acc*(dt*dt*0.5);
        Vec3d new_acc = apply_forces(); // only needed if acceleration is not constant
        Vec3d new_vel = vel + (acc+new_acc)*(dt*0.5);
        pos = new_pos;
        vel = new_vel;
        acc = new_acc;
    }
`
from [link](https://en.wikipedia.org/wiki/Verlet_integration)
Sedenion
CC BY-SA 4.0
2022-11-28T05:51:22.590Z
Does light speed propagation of gravity have some effect here? If an object is 1000 light years away, you feel its gravity as if it was in a position 1000 years ago rather than it's current position. Not sure if this effect is large enough to influence the whole simulation, but shouldn't be too hard to implement (may need some history buffer for each particle)
PaulHK
CC BY-SA 4.0
2023-08-22T16:46:47.223Z
I kind of solved it a long time ago by putting a black hole in the middle
Sedenion
<glsl><simulation>
My n-body simulation doesn't form a galaxy
Sedenion
# My n-body simulation doesn't form a galaxy

(Asked by: username_0 on 2022-11-04)

I'm trying to create a galaxy simulation using opengl with the compute shader and when I start my simulation, the galaxy with the spiral arms doesn't form, instead it goes into chaos. I noticed that the particles in the center are not spinning fast enough
Does anyone had this problem before ?

I tried to:

* change mass and smooth value
* using verlet integration
* change velocity initial value
* change the number of particles

here is the code:

```
#version 430 core

layout (local_size_x = 128, local_size_y = 1, local_size_z = 1) in;

struct Particle
{
    vec4 position;
    vec4 velocity;
    vec4 acceleration;
};

layout (std430, binding = 0) buffer ParticleBuffer {
    Particle p[];
};

uniform float dt;
uniform float accuracy;

  const float mass = 1e-5;
  const float csmooth = 1e-6;

vec3 compute_force(vec3 posi){
    vec3 f = vec3(0);
    for(int i = 0; i<accuracy*p.length(); i++){
        if(posi != p[i].position.xyz){
            vec3 v = p[i].position.xyz - posi;
            float d = length(v);
            f+= mass*v/(pow(d,3)+csmooth);
        }
    }
    return f;
}

void main() {
    uint index = gl_GlobalInvocationID.x;

    vec3 pos = p[index].position.xyz;
    vec3 vel = p[index].velocity.xyz;
    vec3 accel = p[index].acceleration.xyz;

    vec3 newPos = pos+vel*dt + accel*(dt*dt*0.5);
    vec3 newAccel = compute_force(pos);
    vec3 newVel = vel + (accel+newAccel) *(dt*0.5);

    p[index].position.xyz = newPos;
    p[index].velocity.xyz = newVel;
    p[index].acceleration.xyz = newAccel;
}
```

```

**Tags:** glsl, simulation

> **Comments:**
> **username_1**: I am not sure, but please try to delete the accelerations term out of the newPos calculation...  
> **username_0**: seems like nothing really changed  
> **username_1**: Can you go more into detail, how the particles behavior is? Are they 'jumping' or are they all go through a single position?  
> **username_1**: The velocity therm also looks strange to me: only add one acceleration value. I think 'accel' is the correct one.  
> **username_1**: BTW you are using the Euler numeric integration algorithm. It's accuracy is very bad... A better integrater is the 'Runge-Kutta' algorithm  
> **username_0**: the particles are mostly spinning (with initial velocity) around a center at different speed however it's unstable and goes into chaos  
> **username_0**: I used the code ` void update(double dt)     {         Vec3d new_pos = pos + vel*dt + acc*(dt*dt*0.5);         Vec3d new_acc = apply_forces(); // only needed if acceleration is not constant         Vec3d new_vel = vel + (acc+new_acc)*(dt*0.5);         pos = new_pos;         vel = new_vel;         acc = new_acc;     } ` from [link](https://en.wikipedia.org/wiki/Verlet_integration)  
> **username_2**: Does light speed propagation of gravity have some effect here? If an object is 1000 light years away, you feel its gravity as if it was in a position 1000 years ago rather than it's current position. Not sure if this effect is large enough to influence the whole simulation, but shouldn't be too hard to implement (may need some history buffer for each particle)  
> **username_0**: I kind of solved it a long time ago by putting a black hole in the middle  

---

## 0 answers

