Friday, 10 October 2014
rotating code
I have experimented with rotating the shape by rotating the camera, but quickly realized that what the code generated comes out 2d, not 3d, so rotating doesn't really create any cool effect. It just looks like a board being flipped around, I guess. Rendering 3d is too much for my computer to handle - it's already struggling with the last sketch - so I'd settle for this. But anyway... here is the code. Pressing the arrow keys changes the angle of the camera, thus rotating the 'object'
import ddf.minim.analysis.*;
import ddf.minim.*;
float eyeY, eyeX, eyeZ;
float angVertical = 0;
int d = 400;
Minim minim;
AudioPlayer [] players = new AudioPlayer[2];
AudioMetaData meta;
BeatDetect beat;
int
samples = 1024;
float
angleStep = TWO_PI/samples,
maxRadius = 300.,
scaleF = 125.,
radiusStep;
PVector
shapeCenter,
currentVertex;
PVector[]
lookupTable;
float phasorInc = samples / 53030.0;
float phasorInc2 = samples / 90000.0;
int nReflections = 10;
int nPointsPerFrame = 3600;
int nAngles = 6;
int r = 200;
float rad = 70;
Walker w;
Phasor p;
Phasor p2;
float[] angles;
Palette palette;
PVector getVCoordinates(PVector v, float d, float a) {
return new PVector(v.x + d * cos(a), v.y + d * sin(a));
}
float getAngleFromCenter(PVector v) {
return atan2(v.y - height / 2, v.x - width / 2);
}
class Phasor {
float inc;
float phase;
Phasor(float inc) {
this.inc = inc;
}
void update() {
phase += inc;
while (phase >= 1.0) {
phase -= 1.0;
}
while (phase < 0) {
phase += 1.0;
}
}
}
class Walker {
PVector v;
Walker(float x, float y) {
v = new PVector(x, y);
}
void update() {
float gauss = p2.phase * random(2);
v = getVCoordinates(v, gauss, angles[int(random(angles.length))]);
float offset = width / 4;
if (v.x >= width + offset) {
v.x -= width + offset;
}
if (v.x < -offset) {
v.x += width + offset;
}
if (v.y >= height + offset) {
v.y -= height + offset;
}
if (v.y < 0) {
v.y += height + offset;
}
float a = getAngleFromCenter(v);
float d = dist(v.x, v.y, width / 2, height / 2);
PVector center = new PVector(width / 2, height / 2);
float rAngle = PI / (float) nReflections;
noStroke();
fill(palette.getNorm(p.phase), map(d, 0, width, 64, 12));
gauss = max(0.5, gauss / 2);
for (int i = 0; i < nReflections; i++) {
float thisAngle = a + (TWO_PI / (float) nReflections) * i;
PVector thisV = getVCoordinates(center, d, thisAngle);
ellipse(thisV.x, thisV.y, gauss, gauss);
thisV = getVCoordinates(center, d, PI - thisAngle);
ellipse(thisV.x, thisV.y, gauss, gauss);
}
}
}
class Palette {
ArrayList<Integer> colors;
Palette() {
colors = new ArrayList<Integer>();
}
void add(color c) {
colors.add(c);
}
color getNorm(float p) {
int index = (int) (p * colors.size());
color c1 = colors.get(index);
color c2 = colors.get((index + 1) % colors.size());
return lerpColor(c1, c2, p * colors.size() - index);
}
}
void setup()
{
size(1000, 600, P3D);
eyeX = 0;
eyeY = height/2;
eyeZ = 0;
eyeX = d*(sin(radians(angVertical)));
eyeZ = d*cos(radians(angVertical));
angles = new float[nAngles];
for (int i = 0; i < angles.length; i++) {
angles[i] = ((float) i / (float) nAngles) * TWO_PI;
}
w = new Walker(width / 2, height / 2);
p = new Phasor(phasorInc);
p2 = new Phasor(phasorInc2);
palette = new Palette();
palette.add(color(11, 17, 13));
palette.add(color(44, 77, 86));
palette.add(color(195, 170, 114));
palette.add(color(220, 118, 18));
palette.add(color(189, 50, 0));
color c = palette.getNorm(0.0);
for (int i = 0; i < height; i++) {
stroke(lerpColor(color(32), color(0), (float) i / (float) height));
line(0, i, width, i);
}
smooth();
noStroke();
frameRate(60);
background(-1);
noCursor();
shapeCenter = new PVector(width/2, height/2);
lookupTable= new PVector[samples];
for (int i = 0; i < samples; i++)
{
float angle= i*angleStep + HALF_PI;
lookupTable[i] = new PVector(cos(angle), sin(angle));
}
minim = new Minim(this);
players[0]= minim.loadFile("Warriors.mp3");
players[1]= minim.loadFile("itstime.mp3");
radiusStep = maxRadius/(players[0].length()*200/players[1].length());
meta = players[0].getMetaData();
beat = new BeatDetect();
players[1].loop();
}
void draw()
{
background(0);
float t = map(mouseX, 0, width, 0, 1);
beat.detect(players[1].mix);
rect(0, 0, width, height);
camera(eyeX, eyeY, eyeZ,
0, height/2+10, 0,
0, 1, 0);
if (players[1].isPlaying())
{
visualizeWaves(players[1].right.toArray(), 0x50ff0000, maxRadius, scaleF);
visualizeWaves(players[1].left.toArray(), 0x50000000, maxRadius, scaleF);
visualizeWaves(players[1].mix.toArray(), 0x77777777, maxRadius -= radiusStep, scaleF);
translate(width/2, height/2);
if (beat.isOnset()) rad = rad*0.9;
else rad = 70;
ellipse(0, 0, 2*rad, 2*rad);
stroke(-1, 50);
int bsize = players[1].bufferSize();
for (int i = 0; i < bsize - 1; i+=5)
{
float x = (r)*cos(i*2*PI/bsize);
float y = (r)*sin(i*2*PI/bsize);
float x2 = (r + players[1].left.get(i)*100)*cos(i*3*PI/bsize);
float y2 = (r + players[1].left.get(i)*100)*sin(i*5*PI/bsize);
line(x, y, x2, y2);
}
}
}
void visualizeWaves(float [] wavesToVisualize, color fillColor, float radius, float scale_factor)
{
fill(fillColor);
stroke(0x10ffffff);
for (int i = 0; i < wavesToVisualize.length; i++)
{
float r = radius + wavesToVisualize[i]*scale_factor;
p.update();
p2.update();
w.update();
}
}
void keyPressed() {
switch(key) {
// Move camera
case CODED:
if (keyCode == UP) {
angVertical += 10;
}
if (keyCode == DOWN) {
angVertical -= 10;
}
break;
default:
break;
}
if (angVertical>=360)
angVertical=0;
eyeX = d*(sin(radians(angVertical)));
eyeZ = d*cos(radians(angVertical));
println("With angVertical: "+angVertical+
" the cam pos is : "+
eyeX+" / "+eyeY+" / "+eyeZ);
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment