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 › converting 2d random movement to 3d
Page Index Toggle Pages: 1
converting 2d random movement to 3d (Read 309 times)
converting 2d random movement to 3d
Jun 8th, 2009, 4:42am
 
Hello, i have this code that makes a curved random 2d movement  of objects  with sin and cos. How can i convert this 2d movement to 3d , having the same behaivor as 2d?

thanks

here is the code:


Code:

agente[] agentes = new agente[44]; ;

void setup(){
size(600,400);
frameRate(30);
for(int i=0;i<44;i++){
agentes[i] = new agente(random(133), random(133));
}
}


void draw(){
for(int i=0;i<44;i++){
agentes[i].mover();
agentes[i].dibujar();
}
}

class agente{
float xx;
float yy;
float dx;
float dy;
float direccion = random(TWO_PI);
float velocidad = 5;

agente(float x, float y ){
xx = x;
yy = y;
}

void mover(){
cambiaangulo(20);
dx = velocidad * cos(direccion);
dy = velocidad * sin(direccion );
xx = xx + dx;
yy = yy + dy;
}

void dibujar(){
pushMatrix();
translate(xx,yy);
rect(0,0,10,10);
popMatrix();
}

void cambiaangulo(float amplitud) {
float radianes = radians(amplitud);
direccion = direccion + random ( - radianes , radianes);
}
}

Re: converting 2d random movement to 3d
Reply #1 - Jun 8th, 2009, 5:00am
 
Just add appropriate z variables and render in 3D?

Code:
int numAgentes = 44;
agente[] agentes = new agente[numAgentes]; ;

void setup(){
size(600,400,P3D);
 frameRate(30);
 for(int i=0;i<numAgentes;i++){  
   agentes[i] = new agente(random(133), random(133), random(133));  
 }
}


void draw(){
 for(int i=0;i<numAgentes;i++){  
   agentes[i].mover();
   agentes[i].dibujar();  
 }
}

class agente{
 float xx;
 float yy;
 float zz;
 float dx;
 float dy;
 float dz;
 float direccion = random(TWO_PI);
 float velocidad = 5;
 
 agente(float x, float y, float z ){
   xx = x;
   yy = y;
   zz = z;
 }
 
 void mover(){
   cambiaangulo(20);
   dx =  velocidad * cos(direccion);
   dy = velocidad * sin(direccion );
   dz = velocidad * sin(direccion );
   xx += dx;
   yy += dy;
   zz += dz;  
 }
 
 void dibujar(){
   pushMatrix();
   translate(xx,yy,zz);
   rect(0,0,10,10);
   popMatrix();
 }
 
 void cambiaangulo(float amplitud) {
   float radianes = radians(amplitud);
   direccion = direccion + random ( - radianes , radianes);
 }
}


edit: Thought it was worth passing on some good practice: rather than using 'magic numbers' in your code - in this case the number of 'agentes' - set this value in a variable and reference this as and where necessary.  Now you only have to change the variable to change the number of agentes, rather than hunting down all the hard-coded numbers.

Finally, a neat short-cut:

xx = xx + dx;

can be written as:

xx += dx;
Page Index Toggle Pages: 1