1.0とほぼ重力の法則の処理

処理についてかなり前に聞いていましたが、これが何であるかを把握できませんでした。 それで、重力と力を使って自分の世界を作るというアイデアを得ました(それらの周りに点や粒子が飛んでいます)。 将来的には、どういうわけかこれを美しく打ち負かし、クールなスクリーンセーバーを作ることができます。 機会の主人公、すなわちProcessing 1.0.7の助けを借りてプロトタイプを作成することが決定されました。



オブジェクト間の物理的な相互作用を記述するアルゴリズムを作成することから始めましょう。 重力のあるポイントはランダムに空間に配置され、それらはまだ移動しないので(ポイントの重力場で移動するのは粒子のみです)、事前に指定されたステップで空間に配置されたベクトルのリストを作成し、誇らしげに「ベクトルグリッド」と呼びます:)ベクトルは、ベクトルが適用されるポイントで各「重力ポイント」によって作成された重力ベクトルの合計としてカウントされます。





グリッドピッチ20。





グリッドピッチ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 .








使用法:



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 .








ちなみに、小さなグリッドステップを設定してDrawGrid()でベクトルの表示をいじると、次のような多くのしみを取得できます。










All Articles