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 › mouse position in an array
Page Index Toggle Pages: 1
mouse position in an array (Read 575 times)
mouse position in an array
Jun 21st, 2007, 8:53am
 
Sorry for the lame subject, i'm trying to rotate vertex rectangles (in an array)that are located under the mouse cursor.

Quote:
import processing.opengl.*;

fan[] fsurface;

void setup()
{
 size(1024,768, OPENGL);
 frameRate(60);

 fsurface=new fan[100];

 for(int x=0;x<10;x++)
 {
   for(int y=0;y<10;y++)
   {
     fsurface[x+y*10]=new fan((x)*101,(y)*75,101,75);
   }
 }
}

int lc=-120;

void draw()
{
 background(0,0,0);

if(random(100)>80)
 {
   int num= (mouseY*mouseX)%100;
   if(fsurface[num].f==0)
   {
     fsurface[num].flip();
   }
 }
 for(int i=0;i<fsurface.length;i++)
 {
   if(fsurface[i].x+fsurface[i].y==lc)
   {
     fsurface[i].flip();
   }
 }
 lc+=2;
 if(lc>120)
 {
   lc=-120;
 }
 
 translate(0,0,-100);
 for(int i=0;i<fsurface.length;i++)
 {
   fsurface[i].Draw();
 }
}

class fan
{
 int x,y,z,lx,ly;
 int sgx,slx,sgy,sly;
 int angle;
 int f;
 color c;  
 fan(int _x, int _y, int _lx,int _ly)
 {
   x=_x;
   y=_y;
   lx=_lx;
   ly=_ly;
   z=0;
   angle=0;
   c=color(0,120+y,120-y);
 }
 
 void flip()
 {
   f=1;
 }
 
 void Draw()
 {
   pushMatrix();
   stroke(0);
   translate(x+60,y+47,120);
  if((angle!=0 && angle !=180) || f==1)
   {

    angle = mouseY-(mouseY-mouseX)%10;
     rotateX(radians(angle));
     
   }
   beginShape(POLYGON);
   {
     vertex(-lx/2,-ly/2,0);
     vertex(lx/2,-ly/2,0);
     vertex(lx/2,ly/2,0);
     vertex(-lx/2,ly/2,0);
   }
   endShape();
   popMatrix();
 }  
}


Thank you for any suggestion.
Re: mouse position in an array
Reply #1 - Jun 21st, 2007, 8:32pm
 
Is it something like that you would like to have ?
Quote:




Fan[] fans;

void setup()
{
 size(800,600, P3D);

 fans=new Fan[100];

 for(int x=0;x<10;x++)
 {
   for(int y=0;y<10;y++)
   {
     fans[x+y*10]=new Fan(x,y,width/10,height/10);
   }
 }
}

void draw(){
 background(0);
 for(int i=0;i<fans.length;i++){
   fans[i].draw();
 }
}

class Fan{

 int x,y,lx,ly;
 boolean rotate;
 float alpha;
 color c;

 Fan(int x, int y, int lx, int ly){
   this.x=x*lx+lx/2;
   this.y=y*ly+ly/2;
   this.lx=lx;
   this.ly=ly;    
   rotate=false;
   c=color(random(255),random(255),random(255));
 }

 void draw(){
   if( mouseX>=x-lx/2 && mouseX<x+lx/2 && mouseY>=y-ly/2 && mouseY<y+ly/2 ){
     rotate=true;      
   }

   if( rotate ){
     alpha+=5;
   }

   if( alpha==180 ){
     alpha=0;
     rotate=false;
   }

   pushMatrix();
   translate(x,y);
   rotateX(radians(alpha));
   fill(c);
   rect(-lx/2,-ly/2,lx,ly);
   popMatrix();
 }

   
}


Re: mouse position in an array
Reply #2 - Jun 21st, 2007, 8:39pm
 
Wow That's exactly what i had in mind !

I'll have to just make it turn once.

Thank you very much ! :)

EDIT  > done
Quote:
void draw(){
   if( mouseX>=x-lx/2 && mouseX<x+lx/2 && mouseY>=y-ly/2 && mouseY<y+ly/2 && mouseX != pmouseX){
rotate=true;
}
Page Index Toggle Pages: 1