Adding sound, does this look right?
in
Core Library Questions
•
1 year ago
Hi, I am adding sound to sections of my sketch from 0-500 and 500-1000 on the x axis and 0-250 and 250-500 on the y axis this is what i have so far but it isnt working, whats wrong with it, thanks
import ddf.minim.*;
}
Minim minim;
AudioPlayer tone1;
final float somedelay = 0.03;
Ball[] balls = {
new Ball(100, 400, 8),
new Ball(300, 400, 20)
};
final int smallBall = 0;
final int bigBall = 1;
PVector[] vels = {
new PVector(2.15, -1.35),
new PVector(-1.65, .42)
};
void setup() {
size(500, 1000);
smooth();
noStroke();
}
void draw() {
fill(0, 50);
rect(0, 0, width, height);
fill(204);
minim = new minim(this);
tone1 = minim.loadSample("Buzz.mp3",512);
if(smallBall.x > 0 && < 500){
if(smallBall.y > 0 && < 250){
tone1.play;
{
int i=smallBall;
balls[i].x += vels[i].x;
balls[i].y += vels[i].y;
}
balls[bigBall].applyMouse();
for (int i=0; i<2; i++) {
if (i==bigBall)
fill(255, 255, 0); // yellow
else
fill(175); // gray
ellipse(balls[i].x, balls[i].y, balls[i].r*2, balls[i].r*2);
checkBoundaryCollision(balls[i], vels[i]);
}
checkObjectCollision(balls, vels);
}
void checkObjectCollision(Ball[] b, PVector[] v) {
PVector bVect = new PVector();
bVect.x = b[1].x - b[0].x;
bVect.y = b[1].y - b[0].y;
float bVectMag = sqrt(bVect.x * bVect.x + bVect.y * bVect.y);
if (bVectMag < b[0].r + b[1].r) {
float theta = atan2(bVect.y, bVect.x);
float sine = sin(theta);
float cosine = cos(theta);
Ball[] bTemp = {
new Ball(), new Ball()
};
bTemp[1].x = cosine * bVect.x + sine * bVect.y;
bTemp[1].y = cosine * bVect.y - sine * bVect.x;
PVector[] vTemp = {
new PVector(), new PVector()
};
vTemp[0].x = cosine * v[0].x + sine * v[0].y;
vTemp[0].y = cosine * v[0].y - sine * v[0].x;
vTemp[1].x = cosine * v[1].x + sine * v[1].y;
vTemp[1].y = cosine * v[1].y - sine * v[1].x;
PVector[] vFinal = {
new PVector(), new PVector()
};
vFinal[0].x = ((b[0].m - b[1].m) * vTemp[0].x + 2 * b[1].m *
vTemp[1].x) / (b[0].m + b[1].m);
vFinal[0].y = vTemp[0].y;
vFinal[1].x = ((b[1].m - b[0].m) * vTemp[1].x + 2 * b[0].m *
vTemp[0].x) / (b[0].m + b[1].m);
vFinal[1].y = vTemp[1].y;
bTemp[0].x += vFinal[0].x;
bTemp[1].x += vFinal[1].x;
Ball[] bFinal = {
new Ball(), new Ball()
};
bFinal[0].x = cosine * bTemp[0].x - sine * bTemp[0].y;
bFinal[0].y = cosine * bTemp[0].y + sine * bTemp[0].x;
bFinal[1].x = cosine * bTemp[1].x - sine * bTemp[1].y;
bFinal[1].y = cosine * bTemp[1].y + sine * bTemp[1].x;
b[1].x = b[0].x + bFinal[1].x;
b[1].y = b[0].y + bFinal[1].y;
b[0].x = b[0].x + bFinal[0].x;
b[0].y = b[0].y + bFinal[0].y;
v[0].x = cosine * vFinal[0].x - sine * vFinal[0].y;
v[0].y = cosine * vFinal[0].y + sine * vFinal[0].x;
v[1].x = cosine * vFinal[1].x - sine * vFinal[1].y;
v[1].y = cosine * vFinal[1].y + sine * vFinal[1].x;
}
}
void checkBoundaryCollision(Ball ball, PVector vel) {
if (ball.x > width-ball.r) {
ball.x = width-ball.r;
vel.x *= -1;
}
else if (ball.x < ball.r) {
ball.x = ball.r;
vel.x *= -1;
}
else if (ball.y > height-ball.r) {
ball.y = height-ball.r;
vel.y *= -1;
}
else if (ball.y < ball.r) {
ball.y = ball.r;
vel.y *= -1;
}
}
class Ball {
float x, y, r, m;
Ball() {
}
Ball(float x, float y, float r) {
this.x = x;
this.y = y;
this.r = r;
m = r*.1;
}
//
void applyMouse() {
if (x<mouseX)
vels[bigBall].x += somedelay;
else
vels[bigBall].x -= somedelay;
if (y<mouseY)
vels[bigBall].y += somedelay;
else
vels[bigBall].y -= somedelay;
x += (mouseX - x) * somedelay;
y += (mouseY - y) * somedelay;
}
void stop()
{
tone1.close();
minim.stop();
super.stop();
}
1