3-D Anaglyph Sketch for the kids.. Enjoy......
in
Share your Work
•
2 years ago
Any help for making this more robust and dynamic is appreciated. Enjoy:
// Josh Shanholtz's 3D Bounce 2/16/2011 written in processing 1.2.1
// http://www.processing.org/
// Click mouse to add spheres and wear 3D anaglyph glasses for full effect.....
import processing.opengl.*;
float l;
int m;
int MAX = 1000;
float spring = 0.03;
float gravity = 0.001;
float friction = -0.09;
float q = 1;
boolean u = true;
Ball[] bBalls = new Ball[MAX];
int mouseclick;
float x;
float fc;
int r;
void setup()
{
size(1024,730,OPENGL);
background(30);
mouseclick = 0;
r = int(random(-9,9));
for (int i = 0; i < MAX; i++) // iterating 1000 times
{
m = i;
bBalls[i] = new Ball(random(height), random(width), random(26,61), i, bBalls);
bBalls[i].x = random(0,1024);
bBalls[i].y = random(0,730);
bBalls[i].x2 = random(0,1024);
bBalls[i].y2 = random(0,730);
bBalls[i].vx = random(-3,3);
bBalls[i].vy = random(-3,3);
bBalls[i].vz = random(-3,3);
bBalls[i].vx2 = random(-3,3);
bBalls[i].vy2 = random(-3,3);
bBalls[i].diameter = random(26,52);
l = bBalls[i].diameter;
bBalls[i].dz = random(-1.1,-0.3);
bBalls[i].f = bBalls[i].diameter;
bBalls[i].c = color(0,255,255);
bBalls[i].c2 = color(255,0,0);
bBalls[i].fil = color(random(70),random(70),random(100));
bBalls[i].fil2 = color(red(bBalls[i].fil),0,blue(bBalls[i].fil),80);
bBalls[i].a = random(-1,1); //rotation
bBalls[i].az = random(-1,1); //rotation
bBalls[i].sd = int(random(4,9));
bBalls[i].n = i;
bBalls[i].linx = bBalls[i].x2;
bBalls[i].liny = bBalls[i].y2;
bBalls[i].linc = color(255,0,0);
bBalls[i].linc2 = color(0,255,255);
bBalls[i].linz = i;
bBalls[i].lincurve = random(-200,2000);
bBalls[i].lincurve2 = random(-100,1000);
bBalls[i].zbx = random(0,1024); // x pos. of depth bouncing ellipses
bBalls[i].zby = random(0,730); // y pos. of depth bounceing ellipses
bBalls[i].zbz = bBalls[i].zbx; // initial offset of color stroke
bBalls[i].zbvx = random(.5,2);
bBalls[i].zbvy = random(-6,6);
bBalls[i].zbvz = 0;
bBalls[i].zbd = random(20,40);
bBalls[i].zbdz = -1;
bBalls[i].zcr = color(255,0,0,i*10);
bBalls[i].zcb = color(0,255,255,i*10);
bBalls[i].x3 = bBalls[i].x;
bBalls[i].y3 = bBalls[i].y;
bBalls[m].x4 = bBalls[i].x;
bBalls[m].y4 = bBalls[i].y;
bBalls[i].rr = random(-5,5);
}
}
void draw()
{
background(0);
float cameraY = height/2.0;
float fov = 700/float(width) * PI/2;
float cameraZ = cameraY / tan(fov / 2.0);
float aspect = float(width)/float(height);
perspective(fov, aspect, cameraZ/10.0, cameraZ*10.0);
for (int i = 0; i < mouseclick; i++)
{
bBalls[i].collide();
bBalls[i]. move();
bBalls[i].display();
}
}
class Ball
{
float rr;
float x3; // curves assosiation with bBalls[i].x/y suff
float y3; //
float x4; //
float y4; // " "
color zcr;
color zcb;
float zbx;
float zby ;
float zbz ;
float zbvx ;
float zbvy ;
float zbvz ;
float zbd ;
float zbdz ;
color linc;
color linc2;
float x2;
float y2;
float vx2;
float vy2;
float linx;
float liny;
float linz;
float lincurve;
float lincurve2;
float f;
int id;
int n;
float diameter;
float a;
int sd;
float az;
float x;
float y;
float z;
float vx; // left/right velocity
float vy; // up/down velocity
float vz; // depth velocity
float d; // ellipse initial diameter
float dz; //ellipse diameter based off z-depth direction
float fc;
color c = color(255,0,0); //red ellipse
color c2 = color(0,255,255); //cyan ellipse
color fil;
color fil2;
Ball[] others;
Ball(float xin,float yin,float din,int idin, Ball[] oin){
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}
void collide()
{
for (int i = id + 1; i < mouseclick; i++) {
float dx = others[i].x - x;
float dy = others[i].y - y;
float a = others[i].az;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter+ diameter;
if (distance < minDist) {
float angle = atan2(dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others[i].x) * spring;
float ay = (targetY - others[i].y) * spring;
vx -= ax;
vy -= ay;
a = -az;
others[i].vx += ax;
others[i].vy += ay;
}
}
}
void move(){
a = a + az;
diameter = diameter + dz;
x += vx;
y += vy;
f = diameter *4.3;
linx=linx+vx;
liny=liny+vy;
zbx = zbx + vx; //These "z" variables refer to the ellipses their red/blue rings
zby = zby + vy;
//zbz = zbz ;
zbd = zbd + dz;
if (x + diameter > width) {
x = width - diameter;
vx = -vx;
vx *= friction;
}
else if (x - diameter < 0) {
x = diameter;
vx = -vx;
vx *= friction;
}
if (y + diameter > height) {
y = height - diameter;
vy = -vy;
vx *= friction;
}
else if (y - diameter < 0) {
y = diameter;
vy = -vy;
vx *= friction;
}
if (diameter > 61) {
dz = -dz;
//f = -f;
}
if (diameter < 20) {
dz = -dz;
//f = -f;
}
}
void display()
{
ellipseMode(CENTER_RADIUS);
strokeWeight(2);
stroke(c,f*.65);
noFill();
sphereDetail(4);
pushMatrix();
translate(x,y,diameter);
rotateX(a/13);
rotateY(az/13);
rotateZ(a/13);
sphere(diameter);
popMatrix();
stroke(c2,f*.65); // ...(,f*.8) = fade in 3d "z" world
pushMatrix();
translate(x+(f*.02)*diameter/15,y,diameter);
rotateX(a/13);
rotateY(az/13);
rotateZ(a/13);
sphere(diameter); // Originally (diameter*1.3)
popMatrix();
}
}
void mousePressed(){
mouseclick = mouseclick+3;
println("user has clicked mouse " + mouseclick + " Times");
}
// Josh Shanholtz's 3D Bounce 2/16/2011 written in processing 1.2.1
// http://www.processing.org/
// Click mouse to add spheres and wear 3D anaglyph glasses for full effect.....
import processing.opengl.*;
float l;
int m;
int MAX = 1000;
float spring = 0.03;
float gravity = 0.001;
float friction = -0.09;
float q = 1;
boolean u = true;
Ball[] bBalls = new Ball[MAX];
int mouseclick;
float x;
float fc;
int r;
void setup()
{
size(1024,730,OPENGL);
background(30);
mouseclick = 0;
r = int(random(-9,9));
for (int i = 0; i < MAX; i++) // iterating 1000 times
{
m = i;
bBalls[i] = new Ball(random(height), random(width), random(26,61), i, bBalls);
bBalls[i].x = random(0,1024);
bBalls[i].y = random(0,730);
bBalls[i].x2 = random(0,1024);
bBalls[i].y2 = random(0,730);
bBalls[i].vx = random(-3,3);
bBalls[i].vy = random(-3,3);
bBalls[i].vz = random(-3,3);
bBalls[i].vx2 = random(-3,3);
bBalls[i].vy2 = random(-3,3);
bBalls[i].diameter = random(26,52);
l = bBalls[i].diameter;
bBalls[i].dz = random(-1.1,-0.3);
bBalls[i].f = bBalls[i].diameter;
bBalls[i].c = color(0,255,255);
bBalls[i].c2 = color(255,0,0);
bBalls[i].fil = color(random(70),random(70),random(100));
bBalls[i].fil2 = color(red(bBalls[i].fil),0,blue(bBalls[i].fil),80);
bBalls[i].a = random(-1,1); //rotation
bBalls[i].az = random(-1,1); //rotation
bBalls[i].sd = int(random(4,9));
bBalls[i].n = i;
bBalls[i].linx = bBalls[i].x2;
bBalls[i].liny = bBalls[i].y2;
bBalls[i].linc = color(255,0,0);
bBalls[i].linc2 = color(0,255,255);
bBalls[i].linz = i;
bBalls[i].lincurve = random(-200,2000);
bBalls[i].lincurve2 = random(-100,1000);
bBalls[i].zbx = random(0,1024); // x pos. of depth bouncing ellipses
bBalls[i].zby = random(0,730); // y pos. of depth bounceing ellipses
bBalls[i].zbz = bBalls[i].zbx; // initial offset of color stroke
bBalls[i].zbvx = random(.5,2);
bBalls[i].zbvy = random(-6,6);
bBalls[i].zbvz = 0;
bBalls[i].zbd = random(20,40);
bBalls[i].zbdz = -1;
bBalls[i].zcr = color(255,0,0,i*10);
bBalls[i].zcb = color(0,255,255,i*10);
bBalls[i].x3 = bBalls[i].x;
bBalls[i].y3 = bBalls[i].y;
bBalls[m].x4 = bBalls[i].x;
bBalls[m].y4 = bBalls[i].y;
bBalls[i].rr = random(-5,5);
}
}
void draw()
{
background(0);
float cameraY = height/2.0;
float fov = 700/float(width) * PI/2;
float cameraZ = cameraY / tan(fov / 2.0);
float aspect = float(width)/float(height);
perspective(fov, aspect, cameraZ/10.0, cameraZ*10.0);
for (int i = 0; i < mouseclick; i++)
{
bBalls[i].collide();
bBalls[i]. move();
bBalls[i].display();
}
}
class Ball
{
float rr;
float x3; // curves assosiation with bBalls[i].x/y suff
float y3; //
float x4; //
float y4; // " "
color zcr;
color zcb;
float zbx;
float zby ;
float zbz ;
float zbvx ;
float zbvy ;
float zbvz ;
float zbd ;
float zbdz ;
color linc;
color linc2;
float x2;
float y2;
float vx2;
float vy2;
float linx;
float liny;
float linz;
float lincurve;
float lincurve2;
float f;
int id;
int n;
float diameter;
float a;
int sd;
float az;
float x;
float y;
float z;
float vx; // left/right velocity
float vy; // up/down velocity
float vz; // depth velocity
float d; // ellipse initial diameter
float dz; //ellipse diameter based off z-depth direction
float fc;
color c = color(255,0,0); //red ellipse
color c2 = color(0,255,255); //cyan ellipse
color fil;
color fil2;
Ball[] others;
Ball(float xin,float yin,float din,int idin, Ball[] oin){
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}
void collide()
{
for (int i = id + 1; i < mouseclick; i++) {
float dx = others[i].x - x;
float dy = others[i].y - y;
float a = others[i].az;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter+ diameter;
if (distance < minDist) {
float angle = atan2(dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others[i].x) * spring;
float ay = (targetY - others[i].y) * spring;
vx -= ax;
vy -= ay;
a = -az;
others[i].vx += ax;
others[i].vy += ay;
}
}
}
void move(){
a = a + az;
diameter = diameter + dz;
x += vx;
y += vy;
f = diameter *4.3;
linx=linx+vx;
liny=liny+vy;
zbx = zbx + vx; //These "z" variables refer to the ellipses their red/blue rings
zby = zby + vy;
//zbz = zbz ;
zbd = zbd + dz;
if (x + diameter > width) {
x = width - diameter;
vx = -vx;
vx *= friction;
}
else if (x - diameter < 0) {
x = diameter;
vx = -vx;
vx *= friction;
}
if (y + diameter > height) {
y = height - diameter;
vy = -vy;
vx *= friction;
}
else if (y - diameter < 0) {
y = diameter;
vy = -vy;
vx *= friction;
}
if (diameter > 61) {
dz = -dz;
//f = -f;
}
if (diameter < 20) {
dz = -dz;
//f = -f;
}
}
void display()
{
ellipseMode(CENTER_RADIUS);
strokeWeight(2);
stroke(c,f*.65);
noFill();
sphereDetail(4);
pushMatrix();
translate(x,y,diameter);
rotateX(a/13);
rotateY(az/13);
rotateZ(a/13);
sphere(diameter);
popMatrix();
stroke(c2,f*.65); // ...(,f*.8) = fade in 3d "z" world
pushMatrix();
translate(x+(f*.02)*diameter/15,y,diameter);
rotateX(a/13);
rotateY(az/13);
rotateZ(a/13);
sphere(diameter); // Originally (diameter*1.3)
popMatrix();
}
}
void mousePressed(){
mouseclick = mouseclick+3;
println("user has clicked mouse " + mouseclick + " Times");
}