Thursday, January 21, 2010

bouncing cubes

import processing.opengl.*;

Cube[] cubes = new Cube[1024];

int cubeMaxWidth;

void setup()
{
    size(800, 700, P3D); 
//    size(1280, 700, OPENGL); 
    lights();
    noStroke();
    frameRate(20);

    cubeMaxWidth = width / 60;    
    for(int i = 0; i < cubes.length; i++)
    {
        cubes[i] = new Cube(random(width), random(height), random(400));
    }
}

void draw()
{
    background(0xff,0xff,0xff);
    for(int i = 0; i < cubes.length; i++)
    {
        cubes[i].update();
    }
}

/**
 * Class to represent one cube
 */
class Cube
{
    float x, y, z;
    float size;
    float yv; // xv, zv
    float yr; // xr, zr
    
    Cube(float x, float y, float z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
        this.size = random(cubeMaxWidth);
        this.yv = random(12) - 6;
        this.yr = random(3.141 * 2);
    }
    
    int rgb(int r, int g, int b, int a)
    {
      r &= 0xff;
      b &= 0xff;
      g &= 0xff;
      a &= 0xff;
      return (a << 24) + (r << 16) + (g << 8) + b;
    }
    
    void update()
    {
        y += yv;
        yr += 0.1;
        if (y > height)
        {
            yv = -yv;
            yr = - yr;
        }
        else if (y < 0)
        {
            yv = -yv;
            yr = - yr;
        }
        fill(y, 0x255 - y, 0);
        pushMatrix();
        translate(x, y, z);
        rotateY(yv);
        rotateX(yr);
        box(size);
        popMatrix();
    }
        
}

infoinfo

submitted by: stu_axon
views: 

Trying out 3D processing.org. Written at flash Brighton, after seeing Seb Lee-Delisle working with cubes.

Tags: 3d, cubes, bounce

commentscomment

loading loading...

 

Add a comment: