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 › interact recursive funct. with a not recursive ...
Page Index Toggle Pages: 1
interact recursive funct. with a not recursive ... (Read 554 times)
interact recursive funct. with a not recursive ...
Jun 13th, 2007, 2:20pm
 
hello everybody,

i have a question about pushMatrix and popMatrix. i have a recursive function and i want to react to some not recursive balls. but with pushMatrix i'm loosing the orientation and i don't know how to get the corresponding coordinates of the identify matrix?

can somebody help?
thanks.

// my programm
float a  = 0.0;
float inc = TWO_PI/30;
float dreher;

//fluegelkreise akrivieren
Fluegelkreis[] kreiseF = new Fluegelkreis[10];

int zufall =0;
int kugelZaehler=0;

void setup()
{
 size(400,400);
 smooth();
 frameRate(25);
//10 Fluegelkreise machen
 for (int i=0;i<10;i++)  
 {
   kreiseF[i] = new Fluegelkreis();
 }
}

void draw()
{
 background(254);
 // gleichmässige Bewegung
 dreher = sin(a)/10;
 a+= inc;

 // Kugelfigur zeichnen  
 initKugelfig(200);

// Fluegelkugeln produzieren?
 zufall = int (random(20));
 if (zufall==2) {
     kreiseF[kugelZaehler].aktiv = 1;
     kreiseF[kugelZaehler].y = height - int(random(150)) -40;
     if (kugelZaehler == 9)
     {
       kugelZaehler=0;
     } else
     {
     kugelZaehler+=1;
     }
 }

//Fluegelkugeln bewegen
for (int i=0;i<10;i++)
 {
   if (kreiseF[i].aktiv == 1)
   {
     kreiseF[i].walk();
     kreiseF[i].render();
   }
 }
}

class Fluegelkreis
{
 int y,x;
 int aktiv = 0;

 Fluegelkreis ()
 {
   y = 0;
   x = 0;
   aktiv = 0;
 }  
 void render()
 {
   fill(0,0,200,150);
   noStroke();
   ellipseMode(CENTER);
   ellipse(x,y,30,30);
   
   if (x>width)
   {
     aktiv =0;
     x=0;
   }
 }  
 void walk()
 {
   x+= 5;
 }
}

   
void initKugelfig (int x)
{
 pushMatrix();
 translate(x,height);
 drawKugelfig(0);  
 popMatrix();  
}

void drawKugelfig(int grad)
{
 pushMatrix();
 int kreisrad = 40-grad*4;
 ellipseMode(CENTER);
 noStroke();
 fill(250,0,0,180);  

 ellipse(0,0,kreisrad,kreisrad);

 rotate(dreher);
 translate(0,-kreisrad);
 if(grad<7){drawKugelfig(grad+1);}
 popMatrix();
}
Re: interact recursive funct. with a not recursive
Reply #1 - Jun 13th, 2007, 4:22pm
 
pushMatrix saves the current matrix to the stack and when you call popMatrix it just restores it back..
im not sure what do you want to do, would you explain ?
Re: interact recursive funct. with a not recursive
Reply #2 - Jun 13th, 2007, 4:45pm
 
for example:
i want that the red balls get green if they touch a blue ball. but i cannot compare their position because i don't have the position of the red balls corresponding to the starting matrix.
Re: interact recursive funct. with a not recursive
Reply #3 - Jun 13th, 2007, 6:17pm
 
this is one easy way to do that.. get matrix and take translation vector.. other way would be for you to use parent children relationship to do transformation yourself..
so far it prints the position.. you should create an array and save them for later use.. i think this is what u were looking for.



import processing.opengl.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;


// my programm
float a  = 0.0;
float inc = TWO_PI/30;
float dreher;



PGraphicsOpenGL pgl;
GL opengl;
GLU glu;


float[] m = new float[16];

//fluegelkreise akrivieren
Fluegelkreis[] kreiseF = new Fluegelkreis[10];

int zufall =0;
int kugelZaehler=0;

float posx =0;
float posy =0;

void setup()
{
 size(400,400, OPENGL);
 
 pgl = ((PGraphicsOpenGL)g);
 opengl = pgl.gl;
 glu = ((PGraphicsOpenGL)g).glu;


 smooth();
 frameRate(25);
//10 Fluegelkreise machen
 for (int i=0;i<10;i++)  
 {
   kreiseF[i] = new Fluegelkreis();
 }
}

void draw()
{
 background(254);
 // gleichmässige Bewegung
 dreher = sin(a)/10;
 a+= inc;

 // Kugelfigur zeichnen  
 initKugelfig(200);

// Fluegelkugeln produzieren?
 zufall = int (random(20));
 if (zufall==2) {
kreiseF[kugelZaehler].aktiv = 1;
kreiseF[kugelZaehler].y = height - int(random(150)) -40;
if (kugelZaehler == 9)
{
  kugelZaehler=0;
} else
{
kugelZaehler+=1;
}
 }

//Fluegelkugeln bewegen
for (int i=0;i<10;i++)
 {
   if (kreiseF[i].aktiv == 1)
   {
kreiseF[i].walk();
kreiseF[i].render();
   }
 }
}

class Fluegelkreis
{
 int y,x;
 int aktiv = 0;

 Fluegelkreis ()
 {
   y = 0;
   x = 0;
   aktiv = 0;
 }  
 void render()
 {
   fill(0,0,200,150);
   noStroke();
   ellipseMode(CENTER);
   ellipse(x,y,30,30);
   
   if (x>width)
   {
aktiv =0;
x=0;
   }
 }  
 void walk()
 {
   x+= 5;
 }
}

   
void initKugelfig (int x)
{
 pushMatrix();
 translate(x, height);
 posy = 0;  
 drawKugelfig(0);  
 popMatrix();  
}

void drawKugelfig(int grad)
{
 pushMatrix();
 int kreisrad = 40-grad*4;
 ellipseMode(CENTER);
 noStroke();
 fill(250,0,0,180);  

 ellipse(0,0,kreisrad,kreisrad);

 rotate(dreher);
 translate(0,-kreisrad);
 

 //-------------------------------
 //  Get the modelview matrix and extract the position vector
 //-------------------------------
 opengl = pgl.beginGL();
 // get the current modelview matrix
 opengl.glGetFloatv( GL.GL_MODELVIEW_MATRIX , m, 0 );  
 pgl.endGL();
 float x = m[12];
 float y = m[13];
 float z = m[14];
 println( "X="+x );
 println( "Y="+y );
 println( "Z="+z );
 

 if(grad<7){drawKugelfig(grad+1);}
 popMatrix();
}
Re: interact recursive funct. with a not recursive
Reply #4 - Jun 13th, 2007, 6:25pm
 
i wonder if screnX() and screenY() could do the trick for you?

http://processing.org/reference/screenX_.html
http://processing.org/reference/screenY_.html
Page Index Toggle Pages: 1