We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Rotate and image PGraphics
Page Index Toggle Pages: 1
Rotate and image PGraphics (Read 853 times)
Rotate and image PGraphics
Sep 22nd, 2008, 5:50pm
 
Working with particle systems, when I create and rotate a couple of PGrahics and I try to image() them on the screen, they draw with no rotation!
Here's some code:

rotated_flare_images[i] = papplet.createGraphics(pg.width, pg.height, PConstants.P3D);






rotated_flare_images[i].beginDraw();



rotated_flare_images[i].copy(pg, 0, 0, pg.width, pg.height, 0, 0, pg.width, pg.height);



float th = (float)i*dtheta;



rotated_flare_images[i].rotateZ(th);



rotated_flare_images[i].translate(0, -i*2); // DEBUG



rotated_flare_images[i].endDraw();

...

papplet.image(rotated_flare_images[i], 20 + i*rotated_flare_images[i].width, papplet.height - 80);

Where's the mistake?
Thanks!
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;
 }
}
Re: Rotate and image PGraphics
Reply #2 - Sep 22nd, 2008, 9:16pm
 
Actually the problem that I noted above only happens when changing code in an example.
Page Index Toggle Pages: 1