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 › Problem w/ collisions within certain y coordinates
Page Index Toggle Pages: 1
Problem w/ collisions within certain y coordinates (Read 506 times)
Problem w/ collisions within certain y coordinates
Jan 6th, 2010, 3:44am
 
Hi all, having a problem with collision detection within certain areas of the y coordinates. Basically I want when the line hits the ellipse within the first 100 pixels on the y coordinates a sound is generated. The sounds i have generated are done using supercollider through osc.
Below is my code any help would be great.
Cheers

Code:
ArrayList balls;
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
int numBalls = 100;
int currentBall = 0;
int ballWidth = 25;
float linex = 0.0;
boolean ball = false;
boolean start = false;
PFont font;
int ballX = 0;
int ballY = 0;




void setup(){
size(500,550);
smooth();
oscP5 = new OscP5(this,12000);
myRemoteLocation = new NetAddress("127.0.0.1",57120);
balls = new ArrayList();
font = createFont("GillSans-48.vlw",42);
textFont(font);
}




void draw(){
background(0);
stroke(247,15,15,200);
strokeWeight(8);



if(start == true){
linex = linex + 2.0;
line(linex,0.0,linex,500);
if(linex>=500){
linex=0;
}
}



stroke(255);
strokeWeight(4);
line(0,100,500,100);
line(0,200,500,200);
line(0,300,500,300);
line(0,400,500,400);


fill(250,25,25);
noStroke();
rect(0,500,500,500);
if((keyPressed == true) && (key == 's')){
start = true;
}


fill(255);
text("Press 's' to start",10,545);



for(int i = balls.size()-1; i>=0;i--){
Ball ball = (Ball) balls.get(i);
ball.display();
}
}



void mousePressed(){
ball =true;
ballX = mouseX;
ballY = mouseY;
balls.add(new Ball(ballX, ballY,ballWidth));
}


BALL CLASS CODE:
Code:
 class Ball{
float x,y,w;


Ball(float tempX, float tempY, float tempW){
x = tempX;
y = tempY;
w = tempW;

}

void display(){
fill(255);
noStroke();

if(dist(x,0,linex,0)<=ballWidth/2)fill(255,0,0);
if(dist(x,0,linex,0)>=ballWidth/2)fill(255);
ellipse(x,y,w,w);
if(dist(x,0,linex,0)<=ballWidth/2){
OscMessage myMessage = new OscMessage("/synth");
oscP5.send(myMessage,myRemoteLocation);
}
}
}


SUPERCOLLIDER CODE:
Code:
(
SynthDef("synth",{ arg freq = 440, note = 50, amp = 0.3, dur = 1;
var sig;
sig = SinOsc.ar(freq, 0, amp)
* EnvGen.kr(Env.perc(0.01,dur),doneAction:2);
Out.ar(0, sig ! 2);
}).send(s);
)

(
var node;
o = OSCresponder(nil, '/synth', {arg time, responder, msg, addr;
Synth(\synth,
[\freq, 440, \dur, 1 ]);
}).add;
)

o = remove;
Re: Problem w/ collisions within certain y coordinates
Reply #1 - Jan 6th, 2010, 4:54am
 
not quite sure if i understand you correctly, but are you trying to trigger a sound only if the ball is within the y-coordinates 0 to 100? if yes, wouldnt the following adjustment of your if condition do the job?
Code:

if( (dist(x,0,linex,0)<=ballWidth/2) && (y<100) ){
    OscMessage myMessage = new OscMessage("/synth");
   oscP5.send(myMessage,myRemoteLocation);  
}


another thought, looking at your code, when linex intersects with a ball, (many) osc messages will be sent while this intersection is true. wouldn't it be more efficient to only send one osc message when the line touches the ball for the first time? if yes, maybe the following would do the job:

Code:

class Ball{
 float x,y,w;
 

 Ball(float tempX, float tempY, float tempW){
   x = tempX;
   y = tempY;
   w = tempW;

 }
 
 boolean doSend;

 // boolean doSend
 // add a boolean variable to handle the
 // intersection behaviour of a ball and linex.
 // this variable will be set to true while
 // no intersection is detected. as soon as
 // an intersection occurs, an osc message will be
 // sent and variable doSend will be set to false.
 // now, while the line intersects the ball, no other
 // osc message will be sent. after the line leaves
 // the ball, doSend will be set back to true so that
 // a new osc message can be triggered the next time
 // an intersection occurs.

 
 void display(){
   fill(255);
   noStroke();

   if( dist(x,0,linex,0)<=ballWidth/2) {
     fill(255,0,0);
   } else {
     fill(255);
   }

   ellipse(x,y,w,w);
   if( (dist(x,0,linex,0)<=ballWidth/2) && (y<100)){
     if(doSend) {
       OscMessage myMessage = new OscMessage("/synth");
       oscP5.send(myMessage,myRemoteLocation);
       doSend = false;
       println("sending a message.");
     }
   }
   else {
     doSend = true;
   }
 }
}
Re: Problem w/ collisions within certain y coordinates
Reply #2 - Jan 6th, 2010, 4:57am
 
Thanks so much for your help, this has helped me brilliantly.
Cheers again
Page Index Toggle Pages: 1