Skip to content

Instantly share code, notes, and snippets.

@relrod
Created January 20, 2010 21:54
Show Gist options
  • Save relrod/282317 to your computer and use it in GitHub Desktop.
Save relrod/282317 to your computer and use it in GitHub Desktop.
//Squares v0.4 by deathclaw 09
import processing.opengl.*;
PFont font;
// magics hack
// Each letter must be an element of the array.
// A new square will be created for each letter.
String[] magics = {"M","A","G","I","C","S"};
int numSquares = magics.length;
Square[] s=new Square[numSquares];
void setup()
{
font = loadFont("BitstreamVeraSans-Bold-48.vlw");
textFont(font);
background(0);
size(600,400);
smooth();
colorMode(HSB,360,100,100,255);
xVel=random(-5,5);
for(int i=0;i<numSquares;i++)
{
s[i]=new Square(width/2,height/2,20);
s[i].movement(random(-3,3),random(-10),0,0.3);
}
frameRate(50);
}
float xVel;
float mx=0;
float angle=0;
float angle2=0;
float x=0;
float y=0;
float k=0;
float sine;
float purple;
float sat;
float sineC=0;
float randomizer=0;
float v=0;
float colors=0;
int chooser=0;
float xOff=0;
float yOff=0;
void draw()
{
background(255,0,100,0);
rectMode(CENTER);
int rows=12;
int cols=12;
k+=TWO_PI/360;
if(k>TWO_PI){k=0;}
sine=sin(k);
sineC=map(sine,-1,1,0,360);
for(int i=0;i<numSquares;i++)
{
if(mousePressed==true)
{mousePressed();}
colors=270;
s[i].colors(0,3,colors,100,90,230);
s[i].regenerate(width/2,height/2,0);
s[i].spin(0,0,s[i].xVel*s[i].yVel/6);
s[i].update();
s[i].s=50;
s[i].num=i;
if(i<numSquares-1)
{
stroke(0);
noFill();
bezier(s[i].x,s[i].y,s[i].x+s[i].xVel*5,s[i].y+s[i].yVel*5,s[i+1].x+s[i+1].xVel*5,s[i+1].y+s[i+1].yVel*5,s[i+1].x,s[i+1].y);
}
if(i==numSquares-1)
{
stroke(0);
noFill();
bezier(s[i].x,s[i].y,s[i].x+s[i].xVel*5,s[i].y+s[i].yVel*5,s[0].x+s[0].xVel*5,s[0].y+s[0].yVel*5,s[0].x,s[0].y);
}
s[i].display(magics[i]);
}
for(int f=int(0);f<height;f++) //reflective floor
{
stroke(255,f*1.5+60);
line(0,(s[0].yFloor+f),width,int(s[0].yFloor+f));
}
/*Rotating Grid settings
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
s[i*j].colors(0,3,100,100,100,250);
s[i*j].spin(i*30+20,j*30+20,0);
s[i*j].update(0,0,20);
s[i*j].display();
}
}
*/
if(frameCount%2==0 && frameCount<=180)
{
// saveFrame("x-####.gif");
}
println(frameRate);
}
void mousePressed()
{
for(int k=0;k<numSquares;k++)
{
xOff=mouseX-s[k].x;
yOff=mouseY-s[k].y;
if((xOff<=(s[k].hs))&&(xOff>=-(s[k].hs))&&(yOff<=(s[k].hs))&&(yOff>=-(s[k].hs)))
{chooser=k;break;}
else{chooser=numSquares;}
}
if(chooser!=numSquares)
{
s[chooser].x=mouseX;
s[chooser].y=mouseY;
s[chooser].xVel=mouseX-pmouseX;
s[chooser].yVel=mouseY-pmouseY;
s[chooser].age=0;
}
}
void mouseReleased()
{
chooser=numSquares;
}
class Environment
{
float yFloor;
}
class Square
{
float x,y;
float nx,ny;
float xo,yo;
float s;
float hs;
float age;
float c;
float transx,transy;
float angle;
float sStroke;
float sWeight;
float sHue,sat,bri,sAlpha;
float xVel,yVel,xAcc,yAcc,xFric,yFric,xVelo;
float yFloor=height*3/4;
int num;
Square(float xpos,float ypos,float sSize)
{
x=xpos;
y=ypos;
s=sSize;
}
void updatePos(float nx,float ny)
{
x=nx;
y=ny;
}
void updateSize(float ns)
{
s=ns;
}
void update()
{
xVel+=xAcc;
yVel+=yAcc;
x+=xVel;
y+=yVel;
age+=1;
}
void spin(float trax,float tray,float ang)
{
// transx=trax;
// transy=tray;
transx=x;
transy=y;
angle=ang;
}
void movement(float nxVel,float nyVel,float nxAcc,float nyAcc)
{
xVel=nxVel;
yVel=nyVel;
xAcc=nxAcc;
yAcc=nyAcc;
// xFric=nxFric;
// yFric=nyFric;
}
void regenerate(float nxo,float nyo,float nxVel)
{
xo=nxo;
yo=nyo;
xVelo=nxVel;
if((y>=yFloor-hs)&&(yVel>0))
{
y=yFloor-hs;
yVel=-yVel/1.01;
}
if((y<=hs)&&(yVel<0))
{
y=hs;
yVel=-yVel/30;
}
if((x>=width-hs)&&(xVel>0))
{
x=width-hs;
xVel=-xVel/1.04;
}
if((x<=hs)&&(xVel<0))
{
x=hs;
xVel=-xVel/1.04;
}
}
void colors(float nsStroke,float nsWeight,float nsHue,float nsat,float nbri,float nsAlpha)
{
sStroke=nsStroke;
sWeight=nsWeight;
sHue=nsHue;
sat=nsat;
bri=nbri;
sAlpha=nsAlpha;
}
void letter(String letter) {
text(letter, -8,8);
textSize(28);
fill(#FFFFFF);
}
void display(String letter)
{
colorMode(HSB,360,100,100);
stroke(0,0,sStroke);
strokeWeight(sWeight);
if(sWeight==0){noStroke();}
colorMode(HSB,360,100,100,255);
fill(sHue,sat,bri,sAlpha);
rectMode(CENTER);
pushMatrix();
translate(x,y);
rotate(angle);
rect(0,0,s,s);
fill(#FFFFFF);
letter(letter);
// reset the fill after we hack in a letter.
fill(sHue,sat,bri,sAlpha);
hs=s/2;
popMatrix();
stroke(0);
strokeWeight(3);
line(0,yFloor,width,yFloor); //floor line
pushMatrix();
translate(x,-y+2*yFloor);
rotate(angle);
rect(0,0,s,s);
hs=s/2;
popMatrix();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment