Jelly Ball
in
Share your Work
•
3 years ago
I found this code on the old forums and some people could not find it so I am posting it here.
Richard Wong of rdwong.net is the programmer
Richard Wong of rdwong.net is the programmer
- /* //
Jelly Ball - 2010
------------------------------------------------
CODED FROM SCRATCH BY Richard Wong, Sydney AUS
If you are going to use this code for something,
please let me know as I would love to see it.
http://www.rdwong.net
*/ //
float x, y, z, r, radius, angle, a, b;
float xRadius, yRadius, zRadius, xCenter, yCenter, zCenter;
float xLast, yLast, zLast;
float res, force;
boolean showVectors;
float jelliness = 0.3;
void setup() {
size(800, 800, P3D);
background(0);
frameRate(60);
xCenter = width/2;
yCenter = height/2;
zCenter = 0;
radius = yRadius = xRadius = zRadius = width/6;
r = 0;
res = PI/32;
showVectors = false;
}
void draw() {
background(0);
directionalLight(200, 200, 200, -1, 0, -1);
lightSpecular(255, 100, 20);
emissive(0, 64, 128);
shininess(12);
cameraStuff();
fill(255, 0, 0, 126);
noStroke();
r += jelliness;
if (r > TWO_PI*2) {
r -= TWO_PI*2;
}
for (float i = 0; i <= PI; i += res) {
beginShape();
xRadius = radius - force + force*cos(i + r);
yRadius = radius - force + force*sin(i - r);
zRadius = radius - force + force*cos(i - r);
for (float j = 0; j <= PI; j += res) {
x = xRadius * sin(i) * cos(j) + xCenter;
y = yRadius * sin(i) * sin(j) + yCenter;
z = zRadius * cos(i) + zCenter;
if (showVectors) {
pushStyle();
stroke(255, 126);
point(x, y, z);
popStyle();
} else {
vertex(x, y, z);
}
}
for (float j = 0; j <= PI; j += res) {
x = xRadius * sin(i - res) * cos(PI - j) + xCenter;
y = yRadius * sin(i - res) * sin(PI - j) + yCenter;
z = zRadius * cos(i - res) + zCenter;
if (showVectors) {
pushStyle();
stroke(255, 126);
point(x, y, z);
popStyle();
} else {
vertex(x, y, z);
}
}
endShape();
}
for (float i = -PI; i <= 0; i += res) {
xRadius = radius - force + force*cos(r - i);
yRadius = radius - force + force*sin(i + r);
zRadius = radius - force + force*cos(r + i);
beginShape();
for (float j = 0; j <= PI; j += res) {
x = xRadius * sin(i) * cos(j) + xCenter;
y = yRadius * sin(i) * sin(j) + yCenter;
z = zRadius * cos(i) + zCenter;
if (showVectors) {
pushStyle();
stroke(255, 126);
point(x, y, z);
popStyle();
} else {
vertex(x, y, z);
}
}
for (float j = 0; j <= PI; j += res) {
x = xRadius * sin(i - res) * cos(PI - j) + xCenter;
y = yRadius * sin(i - res) * sin(PI - j) + yCenter;
z = zRadius * cos(i - res) + zCenter;
if (showVectors) {
pushStyle();
stroke(255, 126);
point(x, y, z);
popStyle();
} else {
vertex(x, y, z);
}
}
endShape();
}
force *= 0.98;
}
void mouseMoved() {
force += dist(mouseX, mouseY, pmouseX, pmouseY)/(width/12);
}
float xrotary = 0;
float yrotary = 0;
float zrotary = 0;
boolean smoothing = true;
void cameraStuff() {
xrotary += float(mouseX - width/2)/(width/2) * 0.03;
yrotary += float(mouseY - width/2)/(width/2) * 0.03;
zrotary += float(mouseX - mouseY)/(width/2) * 0.03;
translate(xCenter, yCenter, 0);
rotateX(xrotary);
rotateY(yrotary);
rotateZ(zrotary);
translate(-xCenter, -yCenter, 0);
}
void keyPressed() {
if (keyCode == UP){
xrotary += PI/16;
} else if (keyCode == DOWN) {
xrotary -= PI/16;
}
if (keyCode == RIGHT) {
yrotary += PI/16;
} else if (keyCode == LEFT) {
yrotary -= PI/16;
}
if (key == ENTER) {
if (smoothing) {
smoothing = false;
noSmooth();
} else {
smoothing = true;
smooth();
}
}
}
void mousePressed() {
showVectors = (showVectors == true) ? false : true;
}