Skip to content

Instantly share code, notes, and snippets.

@timpulver
Created October 17, 2012 14:46
Show Gist options
  • Save timpulver/3905916 to your computer and use it in GitHub Desktop.
Save timpulver/3905916 to your computer and use it in GitHub Desktop.
[Processing] Rotate Image using a direction vector
/**
* Rotates an image around an angle
* For this the direction vector dir will be used.
* E.g. if you want to code something like Asteroids:
* A rocket, which is always pointing towards the direction it is flying to...
* For details about atan2 see here: http://www.processing.org/reference/atan2_.html
*
* keywords: atan, atan2, sin, cos, tan, vector, vektor, math, mathe, winkel,
* angle, rotation, processing.org, 360, TWO_PI, origin, ursprung, image, bild
*/
PImage img;
PVector pos = new PVector(30, 20);
PVector dir = new PVector(0.2, 0.1);
void setup(){
size(400, 400);
imageMode(CENTER);
img = loadImage("anImage.jpg");
}
void draw(){
pushMatrix();
translate(pos.x, pos.y);
float angle = atan2(dir[i].y, dir[i].x) + PI/2;
rotate(angle)
image(img, 0 , 0);
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment