Code :
float gravity = 0.3; // three variables
float damping = 0.99;
float repulsion = 0.3;
particle[] Z = new particle[32]; // one array named z containing 32 elements
void setup() {
smooth();
size(500, 500, P2D);//P2D is best used with pixel data for 2 dimensional objects
background(0);
//frameRate(60);
// initialize particles randomly
for (int i = 0; i < Z.length; i++) {
Z[i] = new particle(random(width), random(height), random(1) - 0.5, random(1) - 0.5, 16 + random(32));
}
}
void draw() {
float r;
// clear
background(0);
// display particles
for (int i = 0; i < Z.length; i++) {
for (int j = i + 1; j < Z.length; j++) {
Z[i].connect(Z[j]);
}
Z[i].display();
}
// update particle position
for (int i = 0; i < Z.length; i++) {
for (int j = 0; j < Z.length; j++) {
if (j != i)
Z[i].gravitate(Z[j]);
}
Z[i].update();
Z[i].dampen();
}
}
class particle {
float x;
float y;
float px;
float py;
float vx;
float vy;
float mass;
float forcex;
float forcey;
particle(float dx, float dy, float Vx, float Vy, float M) {//constructor
//ASSIGNING VALUES TO THE VARIABLES .. it is a contructor
x = dx; // eg x is mutator
y = dy;
px = dx;
py = dy;
vx = Vx;
vy = Vy;
mass = M;
forcex = 0;
forcey = 0;
}
// calculate the gravitational force contribution by particle Z
void gravitate(particle Z) {
float F, r2;
r2 = sq(x - Z.x) + sq(y - Z.y);
if (r2 != 0) {
F = mass*Z.mass*gravity - mass*Z.mass*repulsion*100/r2;
F /= r2 * sqrt(r2);
forcex += F * (Z.x - x);
forcey += F * (Z.y - y);
}
}
void update() {
// update particle speed
vx += forcex / mass;
vy += forcey / mass;
if (vx > 32) {
vx = 32;
}
else if (vx < -32) {
vx = -32;
}
if (vy > 32) {
vy = 32;
}
else if (vy < -32) {
vy = -32;
}
// update particle position
x += vx;
y += vy;
// check for boundaries
if (x < 0) {
x = 0;
vx = -vx;
} else if (x > width) {
x = width;
vx = - vx;
}
if (y < 0) {
y = 0;
vy = -vy;
} else if (y > height) {
y = height;
vy = -vy;
}
// reset forces
forcex = 0;
forcey = 0;
px = x;
py = y;
}
void dampen() {
vx *= damping;
vy *= damping;
}
void display() {
float trans = 16+144*atan(sqrt(sq(vx)+sq(vy)))/HALF_PI;
stroke(255, 255, 255, 92);
fill(125, 175, 255, trans);
ellipse(x, y, mass*1.5, mass*1.5);
fill(255, 255, 0);
noStroke();
ellipse(x, y, mass / 4, mass / 4);
}
void connect(particle Z) {
stroke(200, 200, 200, 64);
line(x, y, Z.x, Z.y);
}
}
Tutorial:
This code has three variables named gravity, damping and repulsion. It contain one array named z containing 32 elements. This code has one Class named Particle. After that there is contructor with named particle. After that there are mutators.
float gravity = 0.3; // three variables
float damping = 0.99;
float repulsion = 0.3;
particle[] Z = new particle[32]; // one array named z containing 32 elements
void setup() {
smooth();
size(500, 500, P2D);//P2D is best used with pixel data for 2 dimensional objects
background(0);
//frameRate(60);
// initialize particles randomly
for (int i = 0; i < Z.length; i++) {
Z[i] = new particle(random(width), random(height), random(1) - 0.5, random(1) - 0.5, 16 + random(32));
}
}
void draw() {
float r;
// clear
background(0);
// display particles
for (int i = 0; i < Z.length; i++) {
for (int j = i + 1; j < Z.length; j++) {
Z[i].connect(Z[j]);
}
Z[i].display();
}
// update particle position
for (int i = 0; i < Z.length; i++) {
for (int j = 0; j < Z.length; j++) {
if (j != i)
Z[i].gravitate(Z[j]);
}
Z[i].update();
Z[i].dampen();
}
}
class particle {
float x;
float y;
float px;
float py;
float vx;
float vy;
float mass;
float forcex;
float forcey;
particle(float dx, float dy, float Vx, float Vy, float M) {//constructor
//ASSIGNING VALUES TO THE VARIABLES .. it is a contructor
x = dx; // eg x is mutator
y = dy;
px = dx;
py = dy;
vx = Vx;
vy = Vy;
mass = M;
forcex = 0;
forcey = 0;
}
// calculate the gravitational force contribution by particle Z
void gravitate(particle Z) {
float F, r2;
r2 = sq(x - Z.x) + sq(y - Z.y);
if (r2 != 0) {
F = mass*Z.mass*gravity - mass*Z.mass*repulsion*100/r2;
F /= r2 * sqrt(r2);
forcex += F * (Z.x - x);
forcey += F * (Z.y - y);
}
}
void update() {
// update particle speed
vx += forcex / mass;
vy += forcey / mass;
if (vx > 32) {
vx = 32;
}
else if (vx < -32) {
vx = -32;
}
if (vy > 32) {
vy = 32;
}
else if (vy < -32) {
vy = -32;
}
// update particle position
x += vx;
y += vy;
// check for boundaries
if (x < 0) {
x = 0;
vx = -vx;
} else if (x > width) {
x = width;
vx = - vx;
}
if (y < 0) {
y = 0;
vy = -vy;
} else if (y > height) {
y = height;
vy = -vy;
}
// reset forces
forcex = 0;
forcey = 0;
px = x;
py = y;
}
void dampen() {
vx *= damping;
vy *= damping;
}
void display() {
float trans = 16+144*atan(sqrt(sq(vx)+sq(vy)))/HALF_PI;
stroke(255, 255, 255, 92);
fill(125, 175, 255, trans);
ellipse(x, y, mass*1.5, mass*1.5);
fill(255, 255, 0);
noStroke();
ellipse(x, y, mass / 4, mass / 4);
}
void connect(particle Z) {
stroke(200, 200, 200, 64);
line(x, y, Z.x, Z.y);
}
}
Tutorial:
This code has three variables named gravity, damping and repulsion. It contain one array named z containing 32 elements. This code has one Class named Particle. After that there is contructor with named particle. After that there are mutators.
No comments:
Post a Comment