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 › [Newbie] rotating many images
Page Index Toggle Pages: 1
[Newbie] rotating many images (Read 947 times)
[Newbie] rotating many images
Aug 16th, 2005, 7:13pm
 
Hello,

I am new in the wonderful world of processing, and currently I am stuck on a problem that I cant seem to solve. Since I come from Actionscript programming, I have to understand the way processing displays graphical elements first, which I dont do at all at the moment Smiley.

I have a lot of bitmaps which follow the mouse (eek) - and I want them to rotate towards the mouse.
Since the images itself cant be rotated, I looked into the global rotate method, but obviously i do not understand how to make that work...display image and then rotate it is obviously not what I want...

please help a confused person Smiley


Code:

PImage b;
BitmapFollow b1;
BitmapFollow b2;
BitmapFollow b3;

void setup()
{
size (500,500);
framerate(40);
background(255);

b = loadImage("http://processing.org/discourse/public_html/YaBBImages/smiley.gif");

b1 = new BitmapFollow(12);
b2 = new BitmapFollow(4);
b3 = new BitmapFollow(43);
}

void draw()
{
background(255);
b1.draw();
b2.draw();
b3.draw();
}


class BitmapFollow
{

float xm;
float ym;
int fric;
float a;
BitmapFollow(int f)
{
fric = f;
}

void draw()
{
xm += (mouseX - xm)/fric;
ym += (mouseY - ym)/fric;

a = atan2(mouseY-height/2, mouseX-width/2);
//rotate(a); // doesnt work
image(b, xm, ym);
}
}

Re: [Newbie] rotating many images
Reply #1 - Aug 16th, 2005, 7:38pm
 

I'm not sure I follow you exactly, but...does this help any? It's not perfect, but it's a start. oh you need P3D in your size:

Code:

size(500, 500, P3D);


Code:


void draw()
 {
   pushMatrix();
   xm += (mouseX - xm)/fric;
   ym += (mouseY - ym)/fric;
   

   a = atan2(mouseY-height/2, mouseX-width/2);  
   translate(xm, ym);
   rotateZ(a);

   image(b, -b.width/2, -b.height/2);
   popMatrix();
 }

Re: [Newbie] rotating many images
Reply #2 - Aug 16th, 2005, 8:01pm
 

Code:


a = atan2(mouseY-ym, mouseX-xm);



You could also try this.
Re: [Newbie] rotating many images
Reply #3 - Aug 17th, 2005, 8:45am
 
Thanks Mark, it is working fine!


So to get an understanding whats going on, am I right assuming that
- pushMatrix() "saves" the global transformation Matrix,
then all the "local" transformations take place without affecting the saved matrix and
- popMatrix() restores the Matrix to the time of the last pushMatrix?

Theres is another thing that I am not understanding:
Why do you use rotateZ and not just simple rotate - it seems do have the same effect. I understand that rotating around the Z-axis has the effect of rotating through the center of the object - but is it necessary, and what are the benefits?

Code:

void draw()
{
xm += (mouseX - xm)/fric;
ym += (mouseY - ym)/fric;

a = atan2(mouseY-ym, mouseX-xm);

pushMatrix();
translate(xm, ym);
rotate(a);
image(b, -b.width/2, -b.height/2);
popMatrix();
}
Re: [Newbie] rotating many images
Reply #4 - Aug 17th, 2005, 10:31am
 

Your right about the push and pop. It behaves like a conventional stack.

The rotateZ is there to demonstrate accessibility in P3D or OPENGL mode, it could just as easily been rotate().

I've never really used rotate on its own and have never measured the computation costs associated with each, but the benefits of switching render mode to P3D or OPENGL are substantial improvements in performance.


Re: [Newbie] rotating many images
Reply #5 - Aug 17th, 2005, 11:03am
 
Thanks again for your explanations, they helped a lot.
Im running already in openGL mode, which is of course a sweet performance when you come from Flash-programming Smiley

Another question related to that: is there any possibility to let an openGL applet run in the browser? It doesnt seem to work for me...?
Re: [Newbie] rotating many images
Reply #6 - Aug 17th, 2005, 11:08am
 
This problem sounds familiar to me, although I've never tried it myself.

Do a search on the forum as I'm sure it's been mentioned elsewhere.

*** update ***

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1119054178;start=1#1

The suggestion here is that it may never work!

*** further update ***

A definite no on this one, I think.
Re: [Newbie] rotating many images
Reply #7 - Aug 17th, 2005, 3:37pm
 
re: opengl in a browser, it's the fourth item in this section: http://processing.org/faq/bugs.html#opengl

rotate() and rotateZ() are the same, it's just that it make sense to have rotate() and then rotateX() and rotateY() when running in 3D; nor does it make sense to use a function called rotateZ() if you're only doing things in 2D. so we just decided to have them both be the same.
Re: [Newbie] rotating many images
Reply #8 - Aug 17th, 2005, 7:42pm
 
for what it's worth, this is even noted in the developer documentation:
http://dev.processing.org/reference/core/javadoc/processing/core/PGraphics.html#rotate(float)

"Two dimensional rotation. Same as rotateZ (this is identical to a 3D rotation along the z-axis) but included for clarity -- it'd be weird for people drawing 2D graphics to be using rotateZ. And they might kick our a-- for the confusion."

(not that you should be using the developer docs, but that when looking for esoteric details like this, they're often covered there)
Page Index Toggle Pages: 1