volts
YaBB Newbies
Offline
Posts: 27
Canada
Re: Rotate and image PGraphics
Reply #1 - Sep 22nd , 2008, 9:03pm
Hi Davide, I recently a rotating 2d sprite class. Select File|Examples|Basics|Sprite and replace the example code with that below to see it work. There seems to be a minor problem in the 0148 ode. You need to save before running or you'll get a message saying "The constructor Sprite(PImage) is undefined". I'll report this if its not already in bugzilla. -- Tom /** * Rotating Sprite * by Tom Blackwell. * Based on sprite example * by James Patterson. * * Demonstrates loading and displaying a transparent GIF image. */ PImage teddy; Sprite sprite; float xpos; float ypos; float rotation = 0; float drag = 30.0; void setup() { size(200,200, P3D); teddy = loadImage("teddy.gif"); sprite = new Sprite(teddy); xpos = width/2; ypos = height/2; frameRate(60); } void draw() { background(102); float difx = mouseX - xpos-teddy.width/2; if(abs(difx) > 1.0) { xpos = xpos + difx/drag; xpos = constrain(xpos, 0, width-teddy.width); } float dify = mouseY - ypos-teddy.height/2; if(abs(dify) > 1.0) { ypos = ypos + dify/drag; ypos = constrain(ypos, 0, height-teddy.height); } sprite.draw(xpos, ypos, 0, rotation); rotation += PI/32; } class Sprite { PImage img; Float size = 1.0; Float half_w, half_h; Sprite(PImage im) { img = im; half_w = float(im.width/2); half_h = float(im.height/2); } void draw(float x, float y, float z, float angle) { Boolean stroke_state = g.stroke; noStroke(); pushMatrix(); //translate(x+half_w, y+half_h, z); translate(x, y, z); rotate(angle); beginShape(); textureMode(NORMALIZED); texture(img); vertex(-half_w, -half_h, 0, 0); vertex(half_w, -half_h, 1, 0); vertex(half_w, half_h, 1, 1); vertex(-half_w, half_h, 0, 1); endShape(); popMatrix(); g.stroke = stroke_state; } }