⬆️ ⬇️

Processing 1.0 and almost the law of the world

I heard about Processing a long time ago, but I didn’t get around to see what it was all about. And here I had an idea to make my own world with gravity and forces (there are dots and particles fly around them). In the future, you can somehow beat it up beautifully and make a cool screensaver. It was decided to make a prototype with the help of the hero of the occasion, namely Processing 1.0.7 .



We begin by creating an algorithm that describes the physical interactions between objects. The points possessing gravity are randomly located in space, and since they do not move yet (only particles move in the gravitational field of points), I decided to create a list of vectors located in space with a given step in advance, and proudly call it a “vector grid” :) Vectors are considered as the sum of the attractive force vectors created by each “gravitational point” at the point of application of the vector.





Grid spacing 20.





Grid step 10.



class GravGrid

{

int w = 0; /* .*/

int h = 0; /* .*/

int step = 0; /* .

. */

float G =0; /* .





*/

/* */

public ArrayList points = new ArrayList();

ArrayList vectors; /*

*/

/* . w, h, step G */

GravGrid( int w, int h, int step, float G)

{

this .w = w;

this .h = h;

this .step = step;

this .G = G;

}

public void CreateGrid() /* */

{

vectors = new ArrayList(); /* CreateGrid()



*/

GravVector v; /* */

GravPoint p; /* */

float xSum; /* X */

float ySum; /* Y */

float a = 0; /*

*/

for ( int x = 0; x < w; x += step) /*

*/

for ( int y = 0; y < h; y += step)

{

xSum = 0;

ySum = 0;

v = new GravVector(x, y); /*

GravVector*/

/* */

for ( int i = 0; i < points.size(); i++)

{

/* i - */

p = (GravPoint)points.get(i);

a = atan2((px - x), (py - y)); /* */

/* X .

dist(x1, y1, x2, y2) -

*/

xSum += p.Grav(dist(x, y, px, py), G) * sin(a);

/* Y .*/

ySum += p.Grav(dist(x, y, px, py), G) * cos(a);

}

v.Set(xSum, ySum); /*

*/

vectors.add(v); /* */

}

}



public void DrawGrid() /* */

{

float arrowsize = 4; /* */

float len = 0; /* */

GravVector v;

/* */

for ( int i = 0; i < vectors.size(); i++)

{

/* i - */

v = (GravVector)vectors.get(i);

/* ,

File -> Examples ->

Topics -> Simulate -> SimpleParticleSystem

pushMatrix() -

.

, .*/

pushMatrix();

translate(v.x0, v.y0); /*



v.x0, v.y0 -

*/

stroke(255, 0, 0); /* */

/*

v.Get() - PVector */

rotate(atan2(v.Get().y, v.Get().x));

len = v.Get().mag(); /* */

/* */

line(0,0,len,0);

line(len,0,len-arrowsize,+arrowsize/2);

line(len,0,len-arrowsize,-arrowsize/2);

popMatrix();

}

}



/* */

public PVector GetVector( float x, float y)

{

GravVector v;

for ( int i = 0; i < vectors.size(); i++)

{

v = (GravVector)vectors.get(i);

if ((v.x0 <= x)

&& (x < (v.x0 + step))

&& (v.y0 <= y)

&& (y < (v.y0 + step)))

return v.Get();

}

return new PVector(0, 0);

}

}



class GravPoint /* */

{

public int x = 0; /* X */

public int y = 0; /* Y */

float mass = 0; /* .



*/

GravPoint( int x, int y, float mass) /* */

{

this .x = x;

this .y = y;

this .mass = mass;

}

/* .

d() */

public float Grav( float d, float G) { return mass * G / d; }

}



class GravVector /* */

{

public int x0 = 0; /* X */

public int y0 = 0; /* Y */

public float x = 0; /* X +

X */

public float y = 0; /* Y */

GravVector( int x0, int y0) /* */

{

this .x0 = x0;

this .y0 = y0;

}

/* */

public void Set( float x, float y)

{

this .x = x0 + x;

this .y = y0 + y;

}

/* PVector GravVector*/

public PVector Get() { return new PVector(x - x0, y - y0); }

}




* This source code was highlighted with Source Code Highlighter .


')

Using:



int w;

int h;

GravGrid gg;

Particle[] s;

void setup()

{

size(800, 600, JAVA2D); /* P2D,

JAVA2D,

- */

background(255);

w = width;

h = height;

s = new Particle[50]; /* */

gg = new GravGrid(w, h, 20, 40); /* */

for ( int i = 0; i < s.length; i++)

s[i] = new Particle();

gg.points = new ArrayList();

/* */

for ( int i = 0; i < 7; i++)

gg.points.add( new GravPoint(

( int )random(0, w), /* X */

( int )random(0, h), /* Y */

/*



! :) */

( int )random(-120, 120)));

gg.CreateGrid(); /* */

}



GravPoint p;

void draw()

{

/* 20

:)

alpha, */

fill(255, 20);

noStroke();

rect(0, 0, w, h);

for ( int i = 0; i < s.length; i++)

{

s[i].DrawParticle(gg); /* i - */

/* , */

if (s[i].x > w

|| s[i].x < 0

|| s[i].y > h

|| s[i].y < 0)

{

s[i] = new Particle();

}

else

{

for ( int t = 0; t < gg.points.size(); t++)

{

/* */

p = (GravPoint)gg.points.get(t);

fill(255);

stroke(0);

/* */

ellipse(px, py, abs(p.mass), abs(p.mass));

/* , */

if ((((px - (p.mass / 4)) <= s[i].x)

&& (s[i].x < (px + (p.mass / 4)))

&& ((py - (p.mass / 4)) <= s[i].y)

&& (s[i].y < (py + (p.mass / 4)))))

{

s[i] = new Particle();

}

}

}

}

if (keyPressed) if (key == '1' ) setup();

if (keyPressed) if (key == '2' ) gg.DrawGrid();

}



class Particle

{

float t = 0;

float m = random(3, 6);

float y = random(m, h);

float x = random(m, w);

float v0x = 0;

float v0y = 0;

public void DrawParticle(GravGrid gg)

{

t += 0.005;

x += v0x * t + (gg.GetVector(x, y).x / m) * pow(t, 2) / 2;

y += v0y * t + (gg.GetVector(x, y).y / m) * pow(t, 2) / 2;

fill(0, 0, 0, t * 300);

noStroke();

ellipse(x, y, m, m);

}



}




* This source code was highlighted with Source Code Highlighter .




By the way, if you set a small grid step and play around with the display of vectors in DrawGrid (), then you can get a lot of blots like this:







Source: https://habr.com/ru/post/69617/



All Articles