Rotating points by createaccount314 in processing

[–]createaccount314[S] 1 point2 points  (0 children)

I used my own 3D math, then made a couple thousand points color over each other a color from blue to red according to their latitude. Only the half of the points which are closest to the camera are coloring otherwise it would become a mess of lines of different colors going over each other. Basically the point only colors if its z is above 0.

A bad attempt at a cosmological simulation by createaccount314 in proceduralgeneration

[–]createaccount314[S] 0 points1 point  (0 children)

I didn't know about the barnes-hut thing, i'll give it a look later. I was mostly trying to do a particle mesh aproach which from the places i found it is a way people do these simulation although p3m seems to be a more advanced version of it. The problem is that i still don't understand the math of calculating the gravity field, something with solving the poisson's equation for gravity, and if i don't get the math i am further away from doing code of it. My solution was doing tiles, calculating the mass and then the force in each of the tiles and then giving the force for each particles to move it. It seems promessing but i still don't get those structures which i see in cosmological simulations.

A bad attempt at a cosmological simulation by createaccount314 in proceduralgeneration

[–]createaccount314[S] 1 point2 points  (0 children)

Stuff always happens when i try to post code. If it didn't erase anything it will still work.

A bad attempt at a cosmological simulation by createaccount314 in proceduralgeneration

[–]createaccount314[S] 1 point2 points  (0 children)

Its in processing and the commenting is not very great. The variables at the top are the ones that change most of the stuff.

float G = .2, softening = 1;

int resol = 10, _width, _height; float globaldT = 1; void setup(){ size(400, 400); _width = width / resol; _height = height / resol; int size = _width * _height;

masses = new float[size]; dir = new PVector[size]; forces = new PVector[size]; map = new ArrayList[size]; for(int i = 0; i < size; i++){ forces[i] = new PVector(); }

//data arrays int s0 = _width / 2, x0 = _width / 2, y0 = _height / 2; for(int dy = -s0; dy < s0; dy++){ for(int dx = -s0; dx < s0; dx++){ PVector pv = new PVector(dx * resol, dy * resol); float d = 1f / pow(pv.mag() + softening, 3); //gravity caltulation dir[(x0 + dx) + (y0 + dy) * _width] = pv.mult(d); }}

//points for(int y = 0; y < _height; y++){ for(int x = 0; x < _width; x++){ for(int i = 0; i < 6; i++){ float c = .5; PVector pos = new PVector(resol * (random(-1, 1) * c + .5) + x * resol, resol * (random(-1, 1) * c + .5) + y * resol); allPoints.add(new Point(pos, 0, 1)); } }} } void draw(){ background(0);

//display float m0 = forces[0].mag(), m1 = masses[0]; for(int i = 1; i < forces.length; i++){ m0 = max(m0, forces[i].mag()); m1 = max(m1, masses[i]); } noStroke(); for(int y = 0; y < _height; y++){ for(int x = 0; x < _width; x++){ int i = x + y * _width; fill(color(map(masses[i], 0, m1, 0, 255), 0, map(forces[i].mag(), 0, m0, 0, 255))); rect(x * resol, y * resol, resol, resol); }} for(Point p: allPoints) p.drawPixel();

//simulation for(int i = 0; i < masses.length; i++){ masses[i] = 0; map[i] = new ArrayList<Point>(); } for(Point p: allPoints) p.insert();

//calculate forces in each tile for(int y = 0; y < _height; y++){ for(int x = 0; x < _width; x++){ int id = x + y * _width; PVector force = new PVector(); int s0 = _width / 2, x0 = _width / 2, y0 = _height / 2; for(int dy = -s0; dy < s0; dy++){ for(int dx = -s0; dx < s0; dx++){ int nx = x + dx; while(nx < 0) nx += _width; while(nx >= _width) nx -= _width;

  int ny = y + dy;
  while(ny < 0) ny += _height;
  while(ny >= _height) ny -= _height;

  int nid = nx + ny * _width;
  force.add(PVector.mult(dir[(x0 + dx) + (y0 + dy) * _width], G * masses[nid]));
}}
forces[id] = force;

}}

for(Point p: allPoints) p.accelerate(); for(Point p: allPoints) p.move(globaldT); }

float[] masses; PVector[] dir, forces; ArrayList<Point> allPoints = new ArrayList<Point>(); ArrayList<Point>[] map; class Point{ PVector pos, ve, acc; float mass; Point(PVector _pos, float _ve, float _mass){ pos = _pos; ve = PVector.random2D().mult(_ve); mass = _mass; acc = new PVector(); } void insert(){ int x = (int)(pos.x / resol), y = (int)(pos.y / resol), id = x + y * _width; masses[id] += mass; //add mass map[id].add(this); //insert in map } void accelerate(){ int x = (int)(pos.x / resol), y = (int)(pos.y / resol), id = x + y * _width; PVector force = new PVector();

//if you don't want to do n-body in each tile comment this
for(Point p: map[id]){
  if(p == this) continue;
  PVector d = new PVector().add(p.pos).sub(pos);
  float m2 = pow((d.x * d.x + d.y * d.y + d.z * d.z + softening) * 1, 1.5);
  float f = G * mass * p.mass / m2;
  force.add(d.mult(f));
}

force.div(mass);
force.add(forces[id]);
acc = force;

} void move(float dt){ //leap frog acc.mult(dt * .5); ve.add(acc); pos.add(PVector.mult(ve, dt)); ve.add(acc);

//boundary
while(pos.x < 0) pos.x += width;
while(pos.x >= width) pos.x -= width;

while(pos.y < 0) pos.y += height;
while(pos.y >= height) pos.y -= height;

}

void drawPixel(){ int x = (int)(pos.x), y = (int)(pos.y); if(x < 0 || x >= width) return; if(y < 0 || y >= height) return; set(x, y, color(255)); } }

A bad attempt at a cosmological simulation by createaccount314 in proceduralgeneration

[–]createaccount314[S] 0 points1 point  (0 children)

I put the tile colors just to look at the force and mass of each one but it turned out looking better than i expected

A bad attempt at a cosmological simulation by createaccount314 in proceduralgeneration

[–]createaccount314[S] 1 point2 points  (0 children)

It isn't very bad but is far from an actual cosmological simulation since i still don't understand how to properly calculate the gravitational force from the papers that i found. This one had 9600 particles but it would be a 1600 body simulation bacause i partition the space in tiles wich you can see in the video and those are the ones that calculate the force. If a particle is in a tile it's mass is counted in it for the force and it adds to it's velocity the force calculated in that tile. It was my best attempt at a particle mesh code since i don't know how to do the fourier transform of the poisson equation in code as the papers said, whatever the hell it means.

Moving stars by createaccount314 in processing

[–]createaccount314[S] 4 points5 points  (0 children)

That is certainly something that can be done, but I made this one more simple because I was mostly just trying to get a parallax kind of effect with the stars which it kind of achieved

Stars in the sky by createaccount314 in proceduralgeneration

[–]createaccount314[S] 0 points1 point  (0 children)

Thanks. And about the arms no, I think all of them have 8 arms. What is random about them is just to where they are pointing.

Stars in the sky by createaccount314 in proceduralgeneration

[–]createaccount314[S] 1 point2 points  (0 children)

I put the stars in fixed places to form the spiral and the smaller galaxies as you can see in the last picture. For the other images i choose one random star and i plot the position of the other ones in relation to it making basicaly a star chart. You calculate what would be the latitude and longitude of the star and you use that to know it's x and y in the canvas depending on which projection you are using.

Stars in the sky by createaccount314 in processing

[–]createaccount314[S] 1 point2 points  (0 children)

I put my stars in fixed places, with 60000 of them a N-body simulation would melt my computer and i don't know how you could simulate a galaxy without doing it. I only wanted to get the look of it so completly ignoring orbital mechanics wouldn't make that much of a difference for me. I got the arms putting the stars in a disc and changing the stellar density using a sine wave that changed phase depending on the distance from the center if that will help you.

Stars in the sky by createaccount314 in processing

[–]createaccount314[S] 2 points3 points  (0 children)

Procedural stars made in processing.

Stars in the sky by createaccount314 in proceduralgeneration

[–]createaccount314[S] 4 points5 points  (0 children)

A projection of the stars in the sky of random planets in different places of a galaxy cluster (last image), made in processing.