# Monthly Archives: July 2015

## Anna-mation

For this assignment I drew on elements from the coding we did in class and code I found online to create my animation.  It went through multiple iterations and tweaks before reaching this stage, and I feel like the process of experimenting helped my overall comfort level with these skills.

I incorporated an animated background superimposed with an ellipse that follows the mouse with the slow in/slow out and the secondary action of variations in size as it moves.  In addition, the ellipses in the background overlap and continually “grow” as the program runs, which adds to the sense of dimension and dynamics. Hopefully.

Jul27HW

## Bounce

Here is my code:

float xPos;
float yPos;
float diam = 50;
float xSpeed;
float ySpeed;
float xPos2 = 350;
float yPos2 = 25;
float xSpeed2 = 3;
float ySpeed2 = 4;

color ballColor1=(#CC99FF);
color ballColor2 =(#FFE2E2);

void setup() {
size(400, 300);
noStroke();
background(#99CCFF);

xPos = random(0, width);
yPos = random(0, height);

xSpeed = random(-5.0, 5.0);
ySpeed = random(-5.0, 5.0);

}

void draw() {
background(#99CCFF);

fill(ballColor1);
ellipse(xPos, yPos, 50, 50);
fill(ballColor2);
ellipse(xPos2, yPos2, diam, diam);

xPos = xPos+xSpeed;
yPos = yPos+ySpeed;

xPos2 = xPos2 + xSpeed2;
yPos2 = yPos2 + ySpeed2;

if (xPos > width || xPos < 0) {
xSpeed = xSpeed*-1;
}
if (yPos > height || yPos < 0) {
ySpeed = ySpeed*-1;
}
if (xPos2 > width-diam/2 || xPos2 < diam/2) {
xSpeed2 = -xSpeed2;

}
if (yPos2 > height-diam/2 || yPos2 < diam/2) {
ySpeed2 = -ySpeed2;
}

}

Here is my movie: bounce

## working with classes

I tried to do 3 simple balls affected by gravity…and then through the internet I inevitably got introduced to using classes. I like them because they seem to organize and simplify things (especially repeated actions and objects), but it was harder for me to understand because its not as linear as what we did on thursday (instead of making objects and applying actions, you sort of make your actions and apply them to objects).

It was confusing, but I found a really nice youtube demo that walked through it and I left a ton of little notes in my code.

Class_July28

Thanks!

*could not figure out how to randomize xPos and yPos, I replaced all the x/y with xPos/yPos and inserted our code from Thursday under void move but it made the ball frantically shake.  Also, I am curious about where in the class I would insert a keypress to add some interaction. Thanks!

code:

## Bouncing Balls from Echo

Hi all,

One Bouncing Ball: I started with one bouncing ball. I want the ball to drop from the top of the canvas, and gradually stop bouncing. When pressing mouse, the ball will start bouncing again, and the ball’s diameter, color and speed will change.

BoucingBall1_Echo(1080)

float xPos = 250;
float yPos = 0;
float diam = 50;
float ySpeed = 2;

color ballColor=color(random(255),random(ySpeed),random(255));

void setup() {
size(500,400);
noStroke();
smooth();
}

void draw() {
background(0);
ellipse(xPos, yPos, diam, diam);
fill(ballColor);

ySpeed = ySpeed + 0.75;
yPos = yPos + ySpeed;
if (yPos > height – diam) {
yPos = height – diam;
ySpeed = ySpeed * -0.9;
}
if (yPos <= 0) {
ySpeed = -ySpeed;
}
}
void mousePressed() {
ySpeed = random(2, 10);
yPos=0;
xPos=250;
diam=random(30,70);
ballColor=color(random(ySpeed),random(255),random(255));
}

Two Bouncing Balls: Then I tried to code two bouncing balls, the code is based on the previous one and the code from last class. Basically, when the two bouncing balls meet, the background will flash red. Also, when pressing mouse, two balls’ diameters and colors will change.

TwoBoucingBalls_Echo(720)

float xPos = 250;
float yPos = 0;
float diam = 50;
float ySpeed = 2;
float xPos2 = 350;
float yPos2 = 25;
float xSpeed2 = 3;
float ySpeed2 = 4;

color ballColor1=color(random(255),random(ySpeed),random(255));
color ballColor2=color(random(ySpeed),random(255),random(255));

void setup() {
size(500,400);
noStroke();
smooth();
}

void draw() {
background(0);
fill(ballColor1);
ellipse(xPos, yPos, diam, diam);
fill(ballColor2);
ellipse(xPos2, yPos2, diam, diam);

ySpeed = ySpeed + 0.75;
yPos = yPos + ySpeed;

xPos2 = xPos2 + xSpeed2;
yPos2 = yPos2 + ySpeed2;

if (yPos > height – diam) {
yPos = height – diam;
ySpeed = ySpeed * -0.9;
}
if (yPos <= 0) {
ySpeed = -ySpeed;
}

if (xPos2 > width-diam/2 || xPos2 < diam/2) {
xSpeed2 = -xSpeed2;

}
if (yPos2 > height-diam/2 || yPos2 < diam/2) {
ySpeed2 = -ySpeed2;
}

if(dist(xPos, yPos, xPos2, yPos2)<=diam){
ySpeed=ySpeed-2*ySpeed;
xSpeed2=xSpeed2-2*xSpeed2;
ySpeed2=ySpeed2-2*ySpeed2;
}

if(dist(xPos, yPos, xPos2, yPos2)<=diam){
background(#F70505);
}
}

void mousePressed(){
ySpeed = random(2, 10);
xPos=250;
yPos=0;
xSpeed2=2;
ySpeed2=4;
xPos2=350;
yPos2=25;
diam=random(30,50);
ballColor1=color(random(255),random(ySpeed),random(255));
ballColor2=color(random(ySpeed),random(255),random(255));
}

Two Crazy Bouncing Balls: When I coded the two bouncing balls, I accidentally messed up the code a little bit. I got a crazy version of bouncing balls. When two balls hit each time, one of the balls will speed up and finally goes crazy. Here are my crazy balls:

TwoBouncingBall2Crazy_Echo

float xPos = 250;
float yPos = 0;
float diam = 50;
float ySpeed = 2;
float xPos2 = 350;
float yPos2 = 25;
float xSpeed2 = 3;
float ySpeed2 = 4;

color ballColor1=color(random(255),random(ySpeed),random(255));
color ballColor2=color(random(ySpeed),random(255),random(255));

void setup() {
size(500,400);
noStroke();
smooth();
}

void draw() {
background(0);
fill(ballColor1);
ellipse(xPos, yPos, diam, diam);
fill(ballColor2);
ellipse(xPos2, yPos2, diam, diam);

ySpeed = ySpeed + 0.75;
yPos = yPos + ySpeed;

xPos2 = xPos2 + xSpeed2;
yPos2 = yPos2 + ySpeed2;

if (yPos > height – diam) {
yPos = height – diam;
ySpeed = ySpeed * -0.9;
}
if (yPos <= 0) {
ySpeed = -ySpeed;
}

if (xPos2 > width-diam/2 || xPos2 < diam/2) {
xSpeed2 = -xSpeed2;

}
if (yPos2 > height-diam/2 || yPos2 < diam/2) {
ySpeed2 = -ySpeed2;
}

if(dist(xPos, yPos, xPos2, yPos2)<=diam){
ySpeed=ySpeed-2*ySpeed;
xSpeed2=xSpeed2-2*xSpeed2;
ySpeed2=ySpeed2-2*ySpeed; // I accidentally missed the “2,” That’s why it goes crazy!!!
}

if(dist(xPos, yPos, xPos2, yPos2)<=diam){
background(#F70505);
}
}

void mousePressed(){
ySpeed = random(2, 10);
xPos=250;
yPos=0;
xSpeed2=2;
ySpeed2=4;
xPos2=350;
yPos2=25;
diam=random(30,50);
ballColor1=color(random(255),random(ySpeed),random(255));
ballColor2=color(random(ySpeed),random(255),random(255));
}

## Awesome code & Sam’s trial & error(s)

Hi everyone!

I found some awesome demos while trying to figure out what I could do for my assignment. I miserably tried to manipulate code for hours…thinking I could simply look at a flocking code and other notes and synthesize a predator from which the boids would flee from. But, a challenge it was and a challenge it has remained. View a screen capture of everything here.

Here are some awesome demos:

Particles with Attraction and Repulsion Forces

Flocking with predators

I managed to integrate my ball into the flocking code found on processing (see below), but I couldn’t figure out how to code such that the flock would flee from the ball as well. So, I just pretended that this was a game and the user’s purpose was to avoid the flock with the mouse.

Ideally, one would create another function that tells the flock to avoid the x and y position of the ball (plus its diameter I assume) at any times.

Furthermore, I changed the class’s original multi-ball animation script by adding some color coding (see below). I just wrote another function and incorporated it.

Hey Y’all,

Here is my ball bouncing off the wall(s)! There are 2 Examples of the Code Working. One is as a Normal Basketball the next is as a Rainbow Ball.

Please Feel Free to ask any questions. Thanks! (By the Way the Video will be Choppy, the code doesn’t run choppy though).

final float g= 9.807;
final float time_to_run = 10000;//Changes How Long the Program Runs(Keep Value >10000)
float iniX;
float iniY;
float xPos;
float yPos;
float xDir = 1;
float yDir = 1;
int curMinHeight = 0;
float time;
float time_of_run;
float speedconstraint;
int time_check = 0;
boolean lets_go = false;
float yUpdate;

void setup(){
size(800,600,P3D);//Includes 3D for Sphere
background(#FFFFFF);
xPos= random(10,width/2);//Picks a Random Point to Start Bouncing Ball
iniX = xPos;//Saves the Initial points
yPos=random(200,height/2);
iniY = yPos;
time = millis();//Keeping Track of Time
}
void draw(){
println(time_to_run-time);//CountDown Timer
drawBall();//Draw Ball
updateBall();//Update Ball Position
yUpdate = gravityBall(yPos,yDir);//Changes Speed of Ball
yDir = yUpdate;
checkBounds();//Sees where Ball is
stopball();//Stops Ball if Time comes
// if((mouseX >= xPos+100 || mouseX <= xPos-100) && (mouseY >= yPos+100 || mouseY <= yPos-100)){
// println(“got it”);
}
void drawBall(){
background(#000000);
//fill(color(random(0,255),random(0,255),random(0,255)));//Rainbowsnake
smooth();
lights();//For 3D
translate(xPos,yPos,0);// Pust Ball in Correct Position
sphere(50);
//ellipse(xPos,yPos,50,50);
//line(xPos,yPos,50,50);
}
void updateBall(){
xPos = xPos+xDir;
yPos = yPos+yDir;
}
void checkBounds(){
if(xPos> width ){
xDir = xDir*-1;// Change Direction of Ball
}
if (xPos<0){
xDir = xDir*-1;//Change Direction of Sign
}
if (yPos> height){
yDir = yDir*-1; //change Direction of Sign
}
if (yPos<curMinHeight){//Lowers the Ceiling so Ball Drops Better
yDir = yDir*-1; // change direction of sign
}
}
float gravityBall( float y, float yd){
float temp;
speedconstraint = ((100000-time)/100000);// Arbitrary Rate for Ball to Slow Down
if(lets_go){//
temp = (.1* sqrt(2*g*(millis()-time))) * speedconstraint;//Basically Uses the Distance Traveled Since Hitting the Bottom to Adjust Speed
}
else
temp = (.1* sqrt(2*g*(millis()-time))) * speedconstraint*((time/time_to_run)+1);//Same as Above but Increases Speed as the Ceiling Gets Lower
if(yd<0) temp = temp*-1;//Makes sure is not NAN
return temp;
}
void stopball(){
curMinHeight = round((time/time_to_run)*height);//Current Ceiling Height
if (curMinHeight>=height/10) lets_go=true;//Tells the Ball to Speed Up
if(curMinHeight>=height){//Stops the Ball
yPos= height;
yDir = 0;
xDir = 0;
//Tells How Far the Ball has Traveled
println(“Starting Position:” , iniX , iniY);
println(“Ending Position:” , xPos, yPos);
println(“X Distance Traveled”, abs(iniX-xPos));
}
}

## Code for Image Manipulation

PImage face;
float redValavg;
float greenValavg;
float blueValavg;
color buf;
int randp1;
color hold;
boolean same_face = true;
int pos_lr = 0;
int pos_ud = 0;
int dimension;
int pix_mix=2;
void setup() {
size(640, 426);
}
void draw() {
image(face, pos_lr, pos_ud);
}

void keyPressed(){
if( key == ‘v’){// Exchanges Pixels Randomly
dimension = face.width * face.height;
face= sameface(face);
for (int i = 0; i < dimension; i += pix_mix) {
randp1=round(random(0,dimension-1));
hold = face.pixels[i];
face.pixels[i] = face.pixels[randp1];
face.pixels[randp1]=hold;
}
face.updatePixels();
}
if( key == ‘t’) same_face= !same_face; //Resets to FirstState
face= sameface(face);
dimension = face.width * face.height;
for (int i = 0; i < dimension; i += pix_mix) {
face.pixels[i] = color(0, 0, 0);
redValavg = random(0,255);
greenValavg= random(0,255);
blueValavg=random(0,255);
buf= color(redValavg,greenValavg,blueValavg);
if(i%2==0)face.pixels[i]=buf;
}
face.updatePixels();

}
if( key ==’c’){// Darkens
face= sameface(face);
dimension = face.width * face.height;
for (int i = 0; i < dimension; i += pix_mix) {
face.pixels[i] = color(0, 0, 0);
}
face.updatePixels();

}
if( key ==’x’){// Brightens
face= sameface(face);
dimension = face.width * face.height;
for (int i = 0; i < dimension; i += pix_mix) {
face.pixels[i] = color(255, 255, 255);
}
face.updatePixels();

}
//Moving the Image
if( keyCode == UP){
background(#FFFFFF);
pos_ud+=50;
}
if( keyCode == DOWN){
background(#FFFFFF);
pos_ud-=50;
}
if( keyCode == RIGHT){
background(#FFFFFF);
pos_lr+=50;
}
if( keyCode == LEFT){
background(#FFFFFF);
pos_lr-=50;
}
//pix_mix
if(key == ‘a’){
pix_mix=2*pix_mix;
println(“pixel num:” + pix_mix);
}
if(key == ‘s’){
if(pix_mix>1)pix_mix=pix_mix/2;
println(“pixel num:” + pix_mix);
}
if( key ==’m’) setup();

}

PImage sameface(PImage option){
if(same_face) return option;
}

## The problem with the patent system

Hi Folks,

Just saw this on Gizmodo and thought I’d share since it was a topic of discussion last week.

http://gizmodo.com/sorry-you-cant-have-any-more-sex-toys-because-of-this-p-1719816692

## Random…

Sorry to be a stickler here but…If you publish a book of random numbers, I am not sure you can say they are still random.

## Selfie Portrait

First off: I know that given the hyper-realism of this picture you may be inclined to think I cheated and simply uploaded a photograph. I did not. I chose to take a geometric approach and fashion my likeness out of shapes, lines, and points.  On the plus side this gave me a lot of much-needed practice coding and arranging shapes in Processing, but on the down side I found that my inexperience meant that the final product was a little clumsy and lacking detail.  Overall though, I found this assignment to be pretty therapeutic and rewarding. I also have a new activity for long plane rides.