Skip to content

Instantly share code, notes, and snippets.

@helloCaller
Created February 12, 2016 19:32
Show Gist options
  • Save helloCaller/5b566f9fa387bc8ab4a4 to your computer and use it in GitHub Desktop.
Save helloCaller/5b566f9fa387bc8ab4a4 to your computer and use it in GitHub Desktop.
Processing sketch with rotating image on key press
/*
I've been pursuing an interest in applying differnt forces with Processing, so here is a first stab at applying
velocity to an angle.
Hold down any key and watch as the image spins faster and faster, let the key go and watch the image wind down to a stop.
Created by Luke Garwood
with some adaptated code from The Nature of Code by Daniel Shiffman
http://natureofcode.com/book/chapter-3-oscillation/2
*/
//---Global variables
float angle = 0;//setting initial angle to 0
float velocity = 0;// set initial force of turn to 0
float acceleration = 0.001; //set initial acceleration to 0.001 for a gradual application
void setup() { //everything we need to setup once
size(640, 640); //set up the size of the project
}
void draw() {// draw is like our void Loop friend from arduino
background(50); // set a background with a grey scale value of 50
strokeWeight(5); // draw with a thickness of 5
rectMode(CENTER); // have the center point be in the middle of the line and not at it's origin so that we rotate around the middle
translate(width/2, height/2);//move point or origin from 0,0 to the middle of the project allowing for relative coordinates
rotate(angle); //rotate the image at a variable set of radians, to be defined further later
line(-60, 0, 60, 0);//draw a line based on our new point of origin
// line takes an x,y starting point and an x,y ending point as parameters
pushMatrix(); //push and pop matrix essentially are a way of setting coordinates for a time, and then allowing processing to go back to the old coordinates
translate(-60, 0); // change the origin again
rotate(PI); //rotate 90 degrees
line(0, 0, 0, -50); //draw a new line
strokeWeight(2); // change the thickness for the circle
if (velocity >= 0.25) { //condition for changing the colour of the circle
fill(300, 50, 200); //if velocity is greater than 0.25 change to pink
} else { //if velocity is anything else
fill(255); // circle is white
}
ellipse(0, -50, 20, 20);//draw ellipse at the end of the last line with a width and height of 20
popMatrix(); // revert back to the old coordinates
//---repeating everything we just did on the otherside
pushMatrix();
translate(60, 0);
rotate(PI);
strokeWeight(5);
line(0, 0, 0, 50);
strokeWeight(2);
if (velocity >= 0.25) {
fill(100, 50, 200);//turning blue for this circle
} else {
fill(255);
}
ellipse(0, 50, 20, 20);
popMatrix();
if (keyPressed == true) { //condition for while any key is pressed
velocity += acceleration; //increase velocity by the amount acceleration indicated at the top of the page
angle += velocity; // add that velocity incrementally to our angle so that the image spins faster and faster
println(velocity); //print to console for debugging
} else { // if a key isn't pressed
if (velocity > 0) { //check to see if velocity is greater than 0 and if it is
velocity -= (acceleration*1.5);// decrease velocity by acceleration multiplied by 1.5
angle += velocity; // have the angle and rotation act accordingly
}
}
}//---end of draw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment