Added updated examples from branch

This commit is contained in:
codeanticode
2012-05-09 23:58:30 +00:00
parent 9f847df04c
commit a586b8f0ae
109 changed files with 19979 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
/**
* Simple 3D Bird
* by Ira Greenberg.
*
* Using a box and 2 rects to simulate a flying bird.
* Trig functions handle the flapping and sinuous movement.
*/
float ang = 0, ang2 = 0, ang3 = 0, ang4 = 0;
float px = 0, py = 0, pz = 0;
float flapSpeed = 0.2;
void setup(){
size(displayWidth, displayHeight, P3D);
noStroke();
}
void draw(){
background(0);
lights();
// Flight
px = sin(radians(ang3)) * 170;
py = cos(radians(ang3)) * 300;
pz = sin(radians(ang4)) * 500;
translate(width/2 + px, height/2 + py, -700+pz);
rotateX(sin(radians(ang2)) * 120);
rotateY(sin(radians(ang2)) * 50);
rotateZ(sin(radians(ang2)) * 65);
// Body
fill(153);
box(20, 100, 20);
// Left wing
fill(204);
pushMatrix();
rotateY(sin(radians(ang)) * -20);
rect(-75, -50, 75, 100);
popMatrix();
// Right wing
pushMatrix();
rotateY(sin(radians(ang)) * 20);
rect(0, -50, 75, 100);
popMatrix();
// Wing flap
ang += flapSpeed;
if (ang > 3) {
flapSpeed *= -1;
}
if (ang < -3) {
flapSpeed *= -1;
}
// Increment angles
ang2 += 0.01;
ang3 += 2.0;
ang4 += 0.75;
}

View File

@@ -0,0 +1,99 @@
class Bird {
// Properties
float offsetX, offsetY, offsetZ;
float w, h;
int bodyFill;
int wingFill;
float ang = 0, ang2 = 0, ang3 = 0, ang4 = 0;
float radiusX = 120, radiusY = 200, radiusZ = 700;
float rotX = 15, rotY = 10, rotZ = 5;
float flapSpeed = 0.4;
float rotSpeed = 0.1;
// Constructors
Bird(){
this(0, 0, 0, 60, 80);
}
Bird(float offsetX, float offsetY, float offsetZ,
float w, float h){
this.offsetX = offsetX;
this.offsetY = offsetY;
this.offsetZ = offsetZ;
this.h = h;
this.w = w;
bodyFill = color(153);
wingFill = color(204);
}
void setFlight(float radiusX, float radiusY, float radiusZ,
float rotX, float rotY, float rotZ){
this.radiusX = radiusX;
this.radiusY = radiusY;
this.radiusZ = radiusZ;
this.rotX = rotX;
this.rotY = rotY;
this.rotZ = rotZ;
}
void setWingSpeed(float flapSpeed){
this.flapSpeed = flapSpeed;
}
void setRotSpeed(float rotSpeed){
this.rotSpeed = rotSpeed;
}
void fly() {
pushMatrix();
float px, py, pz;
// Flight
px = sin(radians(ang3)) * radiusX;
py = cos(radians(ang3)) * radiusY;
pz = sin(radians(ang4)) * radiusZ;
translate(width/2 + offsetX + px, height/2 + offsetY+py, -700 + offsetZ+pz);
rotateX(sin(radians(ang2)) * rotX);
rotateY(sin(radians(ang2)) * rotY);
rotateZ(sin(radians(ang2)) * rotZ);
// Body
fill(bodyFill);
box(w/5, h, w/5);
// Left wing
fill(wingFill);
pushMatrix();
rotateY(sin(radians(ang)) * 20);
rect(0, -h/2, w, h);
popMatrix();
// Right wing
pushMatrix();
rotateY(sin(radians(ang)) * -20);
rect(-w, -h/2, w, h);
popMatrix();
// Wing flap
ang += flapSpeed;
if (ang > 3) {
flapSpeed*=-1;
}
if (ang < -3) {
flapSpeed*=-1;
}
// Ang's run trig functions
ang2 += rotSpeed;
ang3 += 1.25;
ang4 += 0.55;
popMatrix();
}
}

View File

@@ -0,0 +1,53 @@
/**
* Crazy Flocking 3D Birds
* by Ira Greenberg.
*
* Simulates a flock of birds using a Bird class and nested
* pushMatrix() / popMatrix() functions.
* Trigonometry functions handle the flapping and sinuous movement.
*/
// Flock array
int birdCount = 200;
Bird[]birds = new Bird[birdCount];
float[]x = new float[birdCount];
float[]y = new float[birdCount];
float[]z = new float[birdCount];
float[]rx = new float[birdCount];
float[]ry = new float[birdCount];
float[]rz = new float[birdCount];
float[]spd = new float[birdCount];
float[]rot = new float[birdCount];
void setup() {
size(displayWidth, displayHeight, P3D);
noStroke();
// Initialize arrays with random values
for (int i = 0; i < birdCount; i++){
birds[i] = new Bird(random(-300, 300), random(-300, 300),
random(-500, -2500), random(5, 30), random(5, 30));
x[i] = random(20, 340);
y[i] = random(30, 350);
z[i] = random(1000, 4800);
rx[i] = random(-160, 160);
ry[i] = random(-55, 55);
rz[i] = random(-20, 20);
spd[i] = random(.1, 3.75);
rot[i] = random(.025, .15);
}
}
void draw() {
background(0);
lights();
for (int i = 0; i < birdCount; i++){
birds[i].setFlight(x[i], y[i], z[i], rx[i], ry[i], rz[i]);
birds[i].setWingSpeed(spd[i]);
birds[i].setRotSpeed(rot[i]);
birds[i].fly();
}
}

View File

@@ -0,0 +1,72 @@
// Custom Cube Class
class Cube{
PVector[] vertices = new PVector[24];
float w, h, d;
// Default constructor
Cube(){ }
// Constructor 2
Cube(float w, float h, float d) {
this.w = w;
this.h = h;
this.d = d;
// cube composed of 6 quads
//front
vertices[0] = new PVector(-w/2,-h/2,d/2);
vertices[1] = new PVector(w/2,-h/2,d/2);
vertices[2] = new PVector(w/2,h/2,d/2);
vertices[3] = new PVector(-w/2,h/2,d/2);
//left
vertices[4] = new PVector(-w/2,-h/2,d/2);
vertices[5] = new PVector(-w/2,-h/2,-d/2);
vertices[6] = new PVector(-w/2,h/2,-d/2);
vertices[7] = new PVector(-w/2,h/2,d/2);
//right
vertices[8] = new PVector(w/2,-h/2,d/2);
vertices[9] = new PVector(w/2,-h/2,-d/2);
vertices[10] = new PVector(w/2,h/2,-d/2);
vertices[11] = new PVector(w/2,h/2,d/2);
//back
vertices[12] = new PVector(-w/2,-h/2,-d/2);
vertices[13] = new PVector(w/2,-h/2,-d/2);
vertices[14] = new PVector(w/2,h/2,-d/2);
vertices[15] = new PVector(-w/2,h/2,-d/2);
//top
vertices[16] = new PVector(-w/2,-h/2,d/2);
vertices[17] = new PVector(-w/2,-h/2,-d/2);
vertices[18] = new PVector(w/2,-h/2,-d/2);
vertices[19] = new PVector(w/2,-h/2,d/2);
//bottom
vertices[20] = new PVector(-w/2,h/2,d/2);
vertices[21] = new PVector(-w/2,h/2,-d/2);
vertices[22] = new PVector(w/2,h/2,-d/2);
vertices[23] = new PVector(w/2,h/2,d/2);
}
void create(){
// Draw cube
for (int i=0; i<6; i++){
beginShape(QUADS);
for (int j=0; j<4; j++){
vertex(vertices[j+4*i].x, vertices[j+4*i].y, vertices[j+4*i].z);
}
endShape();
}
}
void create(color[]quadBG){
// Draw cube
for (int i=0; i<6; i++){
fill(quadBG[i]);
beginShape(QUADS);
for (int j=0; j<4; j++){
vertex(vertices[j+4*i].x, vertices[j+4*i].y, vertices[j+4*i].z);
}
endShape();
}
}
}

View File

@@ -0,0 +1,118 @@
/**
* Cubes Contained Within a Cube
* by Ira Greenberg.
*
* Collision detection against all
* outer cube's surfaces.
* Uses the Point3D and Cube classes.
*/
Cube stage; // external large cube
int cubies = 20;
Cube[]c = new Cube[cubies]; // internal little cubes
color[][]quadBG = new color[cubies][6];
// Controls cubie's movement
float[]x = new float[cubies];
float[]y = new float[cubies];
float[]z = new float[cubies];
float[]xSpeed = new float[cubies];
float[]ySpeed = new float[cubies];
float[]zSpeed = new float[cubies];
// Controls cubie's rotation
float[]xRot = new float[cubies];
float[]yRot = new float[cubies];
float[]zRot = new float[cubies];
// Size of external cube
float bounds = 300;
void setup() {
size(640, 360, P3D);
orientation(LANDSCAPE);
for (int i = 0; i < cubies; i++){
// Each cube face has a random color component
float colorShift = random(-75, 75);
quadBG[i][0] = color(0);
quadBG[i][1] = color(51);
quadBG[i][2] = color(102);
quadBG[i][3] = color(153);
quadBG[i][4] = color(204);
quadBG[i][5] = color(255);
// Cubies are randomly sized
float cubieSize = random(5, 15);
c[i] = new Cube(cubieSize, cubieSize, cubieSize);
// Initialize cubie's position, speed and rotation
x[i] = 0;
y[i] = 0;
z[i] = 0;
xSpeed[i] = random(-1, 1);
ySpeed[i] = random(-1, 1);
zSpeed[i] = random(-1, 1);
xRot[i] = random(40, 100);
yRot[i] = random(40, 100);
zRot[i] = random(40, 100);
}
// Instantiate external large cube
stage = new Cube(bounds, bounds, bounds);
}
void draw(){
background(50);
lights();
// Center in display window
translate(width/2, height/2, -130);
// Outer transparent cube
noFill();
// Rotate everything, including external large cube
rotateX(frameCount * 0.001);
rotateY(frameCount * 0.002);
rotateZ(frameCount * 0.001);
stroke(255);
// Draw external large cube
stage.create();
// Move and rotate cubies
for (int i = 0; i < cubies; i++){
pushMatrix();
translate(x[i], y[i], z[i]);
rotateX(frameCount*PI/xRot[i]);
rotateY(frameCount*PI/yRot[i]);
rotateX(frameCount*PI/zRot[i]);
noStroke();
c[i].create(quadBG[i]);
x[i] += xSpeed[i];
y[i] += ySpeed[i];
z[i] += zSpeed[i];
popMatrix();
// Draw lines connecting cubbies
stroke(0);
if (i < cubies-1){
line(x[i], y[i], z[i], x[i+1], y[i+1], z[i+1]);
}
// Check wall collisions
if (x[i] > bounds/2 || x[i] < -bounds/2){
xSpeed[i]*=-1;
}
if (y[i] > bounds/2 || y[i] < -bounds/2){
ySpeed[i]*=-1;
}
if (z[i] > bounds/2 || z[i] < -bounds/2){
zSpeed[i]*=-1;
}
}
}

View File

@@ -0,0 +1,149 @@
/**
* PushPop Cubes
* by Ira Greenberg.
*
* Array of rotating cubes creates
* dynamic field patterns. Color
* controlled by light sources. Example
* of pushMatrix() and popMatrix().
*/
// Cube class required
float ang;
int rows = 21;
int cols = 21;
int cubeCount = rows*cols;
int colSpan, rowSpan;
float rotspd = 2.0;
Cube[] cubes = new Cube[cubeCount];
float[] angs = new float[cubeCount];
float[] rotvals = new float[cubeCount];
void setup(){
size(displayWidth, displayHeight, P3D);
colSpan = width/(cols-1);
rowSpan = height/(rows-1);
noStroke();
// instantiate cubes
for (int i = 0; i < cubeCount; i++){
cubes[i] = new Cube(12, 12, 6, 0, 0, 0);
/* 3 different rotation options
- 1st option: cubes each rotate uniformly
- 2nd option: cubes each rotate randomly
- 3rd option: cube columns rotate as waves
To try the different rotations, leave one
of the rotVals[i] lines uncommented below
and the other 2 commented out. */
//rotvals[i] = rotspd;
//rotvals[i] = random(-rotspd * 2, rotspd * 2);
rotvals[i] = rotspd += .01;
}
}
void draw(){
int cubeCounter = 0;
background(0);
fill(200);
// Set up some different colored lights
pointLight(51, 102, 255, width/3, height/2, 100);
pointLight(200, 40, 60, width/1.5, height/2, -150);
// Raise overall light in scene
ambientLight(170, 170, 100);
// Translate, rotate and draw cubes
for (int i = 0; i < cols; i++){
for (int j = 0; j < rows; j++){
pushMatrix();
/* Translate each block.
pushmatix and popmatrix add each cube
translation to matrix, but restore
original, so each cube rotates around its
owns center */
translate(i * colSpan, j * rowSpan, -20);
//rotate each cube around y and x axes
rotateY(radians(angs[cubeCounter]));
rotateX(radians(angs[cubeCounter]));
cubes[cubeCounter].drawCube();
popMatrix();
cubeCounter++;
}
}
// Angs used in rotate function calls above
for (int i = 0; i < cubeCount; i++){
angs[i] += rotvals[i];
}
}
// Simple Cube class, based on Quads
class Cube {
// Properties
int w, h, d;
int shiftX, shiftY, shiftZ;
// Constructor
Cube(int w, int h, int d, int shiftX, int shiftY, int shiftZ){
this.w = w;
this.h = h;
this.d = d;
this.shiftX = shiftX;
this.shiftY = shiftY;
this.shiftZ = shiftZ;
}
/* Main cube drawing method, which looks
more confusing than it really is. It's
just a bunch of rectangles drawn for
each cube face */
void drawCube(){
// Front face
beginShape(QUADS);
normal(1, 0, 0);
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
// Back face
normal(-1, 0, 0);
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(w + shiftX, h + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
// Left face
normal(0, -1, 0);
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
// Right face
normal(0, 1, 0);
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(w + shiftX, h + shiftY, d + shiftZ);
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
// Top face
normal(0, 0, 1);
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
// Bottom face
normal(0, 0, -1);
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, h + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
endShape();
}
}

View File

@@ -0,0 +1,39 @@
/**
* Rotate 1.
*
* Rotating simultaneously in the X and Y axis.
* Transformation functions such as rotate() are additive.
* Successively calling rotate(1.0) and rotate(2.0)
* is equivalent to calling rotate(3.0).
*/
float a = 0.0;
float rSize; // rectangle size
void setup() {
size(640, 360, P3D);
orientation(LANDSCAPE);
rSize = width / 6;
noStroke();
fill(204, 204);
}
void draw() {
background(0);
a += 0.005;
if(a > TWO_PI) {
a = 0.0;
}
translate(width/2, height/2);
rotateX(a);
rotateY(a * 2.0);
rect(-rSize, -rSize, rSize*2, rSize*2);
rotateX(a * 1.001);
rotateY(a * 2.002);
rect(-rSize, -rSize, rSize*2, rSize*2);
}

View File

@@ -0,0 +1,43 @@
/**
* Rotate 2.
*
* The push() and pop() functions allow for more control over transformations.
* The push function saves the current coordinate system to the stack
* and pop() restores the prior coordinate system.
*/
float a; // Angle of rotation
float offset = PI/24.0; // Angle offset between boxes
int num = 12; // Number of boxes
color[] colors = new color[num]; // Colors of each box
color safecolor;
boolean pink = true;
void setup()
{
size(640, 360, P3D);
orientation(LANDSCAPE);
noStroke();
for(int i=0; i<num; i++) {
colors[i] = color(255 * (i+1)/num);
}
lights();
}
void draw()
{
background(0, 0, 26);
translate(width/2, height/2);
a += 0.01;
for(int i = 0; i < num; i++) {
pushMatrix();
fill(colors[i]);
rotateY(a + offset*i);
rotateX(a/2 + offset*i);
box(200);
popMatrix();
}
}