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 & HelpSound,  Music Libraries › Selecting seperate objects to play different sound
Page Index Toggle Pages: 1
Selecting seperate objects to play different sound (Read 566 times)
Selecting seperate objects to play different sound
Jan 15th, 2008, 2:11pm
 
Hey all, just a bit of help with a project i have. They are ten cylinders and i need each one to have a seperate sound. but i think the way it is laid out at the moment they are set as one since the colors are the same. So when the user clicks on the cylinder, the colour changes, but has its on specific colour to begin with. Thanks alot


float rot = 0.0;
int sideN = 100;

void setup()
{
 size(800, 400, P3D);
 noStroke();
}

void draw()
{


 background(0);
 lights();
 for (int i = 0; i < 10; i++) {
   float xpos = ((i*70)+100);
   cylinder(25,200,sideN, xpos, height/2, i);
 }

}




void cylinder(float w, float h, int sides, float xstart, float ystart, int idx)
{
 h = h+ystart;
 float angle;
 float[] x = new float[sides+1];
 float[] z = new float[sides+1];
 float maxx= 0;
 //get the x and z position on a circle for all the sides
 for(int i=0; i < x.length; i++){
   angle = TWO_PI / (sides) * i;
   x[i] = (sin(angle) * w)+xstart;
   maxx = max(x[i], maxx);
   z[i] = cos(angle) *w;
 }
 
 if ((mouseX > xstart-25) && (mouseX < maxx+25)) {
   fill(0,255,0);
   println(idx);
 }
 else {
   fill(255,255,0);  
 }
 //draw the top of the cylinder
 beginShape(TRIANGLE_FAN);

 vertex(xstart,   -h/2,    0);

 for(int i=0; i < x.length; i++){
   vertex(x[i], -h/2, z[i]);
 }

 endShape();

 //draw the center of the cylinder
 beginShape(QUAD_STRIP);

 for(int i=0; i < x.length; i++){
   vertex(x[i], -h/2, z[i]);
   vertex(x[i], h/2, z[i]);
 }

 endShape();

 //draw the bottom of the cylinder
 beginShape(TRIANGLE_FAN);

 vertex(xstart,   h/8,    0);

 for(int i=0; i < x.length; i++){
   vertex(x[i], h/2, z[i]);
 }

 endShape();
}

void keyPressed()
{
 if (keyCode == UP)
 {
   sideN++;
 }
 if (keyCode == DOWN)
 {
   sideN--;
 }
}

Re: Selecting seperate objects to play different s
Reply #1 - Jan 15th, 2008, 3:58pm
 
since your cylinders are not only geometrical shapes, but objects with specific attributes (sound, color, etc) a good idea would be to code it in an object-oriented way. you could do a class for your cylinders, something like :

Code:
class Cylinder {
float x, y; // position
color c; // color
Sample s; // sample

void display {
// display method
}

}


I've been doing this with rectangles, and it might interest you : see http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Exhibition;action=display;num=1192376397
Re: Selecting seperate objects to play different s
Reply #2 - Jan 15th, 2008, 3:58pm
 
you might want to use trace a ray from your camera/mouse pos to the world and find out which cylinder you have hit.
and play the associated sound and change the object color.

thats it
Re: Selecting seperate objects to play different s
Reply #3 - Jan 15th, 2008, 6:39pm
 
antiplastik wrote on Jan 15th, 2008, 3:58pm:
since your cylinders are not only geometrical shapes, but objects with specific attributes (sound, color, etc) a good idea would be to code it in an object-oriented way. you could do a class for your cylinders, something like :

Code:
class Cylinder {
float x, y; // position
color c; // color
Sample s; // sample

void display {
// display method
}

}


I've been doing this with rectangles, and it might interest you : see http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Exhibition;action=display;num=1192376397



thats great guys, thanks alot
Page Index Toggle Pages: 1