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;