I'm trying to use minim to create a sounds when the balls collide, but i'm a bit lost. Thank you!

edited September 2017 in Library Questions
    import ddf.minim.*;

    import ddf.minim.analysis.*;

    import ddf.minim.effects.*;

    import ddf.minim.signals.*;

    import ddf.minim.spi.*;

    import ddf.minim.ugens.*;


    Ball[] balls =  { 
      new Ball(100, 400, 20), 
      new Ball(700, 400, 80)
    };


      Minim minim;

      Sampler b;

      AudioOutput out;

      Sampler d;

    void setup() {
      size(640, 360);
      minim = new Minim(this);
      b = new Sampler("Bell.mp3", 100, minim);
      out = minim.getLineOut();
      d = new Sampler("Door.mp3", 3, minim);

    }

    void draw() {
    background(51);
    for (Ball b : balls) {
    b.update();
    b.display();

       b.checkBoundaryCollision();
      }

  balls[0].checkCollision(balls[1]);
}

void mouseMoved () {

  b.trigger();

  d.trigger();
}

class Ball {

  PVector position;

  PVector velocity;

  float radius, m;

  Sampler sampler;


  Ball(float x, float y, float r_, Sampler a) {

    Sampler = a;

    position = new PVector(x, y);

    velocity = PVector.random2D();

    velocity.mult(3);

    radius = r_;

    m = radius*.1;
  }

  void update() {

    position.add(velocity);
  }

  void checkBoundaryCollision() {

    if (position.x > width-radius) {

      position.x = width-radius;

      velocity.x *= -1;

      triggerSample();

    } else if (position.x < radius) {

      position.x = radius;

      velocity.x *= -1;

    } else if (position.y > height-radius) {

      position.y = height-radius;

      velocity.y *= -1;

    } else if (position.y < radius) {

      position.y = radius;

      velocity.y *= -1;
    }
  }

  void checkCollision(Ball other) {


    PVector distanceVect = PVector.sub(other.position, position);

    float distanceVectMag = distanceVect.mag();


    float minDistance = radius + other.radius;

    if (distanceVectMag < minDistance) {
      float distanceCorrection = (minDistance-distanceVectMag)/2.0;
      PVector d = distanceVect.copy();
      PVector correctionVector = d.normalize().mult(distanceCorrection);
      other.position.add(correctionVector);
      position.sub(correctionVector);

      float theta  = distanceVect.heading();

      float sine = sin(theta);
      float cosine = cos(theta);


      PVector[] bTemp = {
        new PVector(), new PVector()
      };


      bTemp[1].x  = cosine * distanceVect.x + sine * distanceVect.y;
      bTemp[1].y  = cosine * distanceVect.y - sine * distanceVect.x;


      PVector[] vTemp = {
        new PVector(), new PVector()
      };

      vTemp[0].x  = cosine * velocity.x + sine * velocity.y;
      vTemp[0].y  = cosine * velocity.y - sine * velocity.x;
      vTemp[1].x  = cosine * other.velocity.x + sine * other.velocity.y;
      vTemp[1].y  = cosine * other.velocity.y - sine * other.velocity.x;


      PVector[] vFinal = {  
        new PVector(), new PVector()
      };

      vFinal[0].x = ((m - other.m) * vTemp[0].x + 2 * other.m * vTemp[1].x) / (m + other.m);
      vFinal[0].y = vTemp[0].y;

      vFinal[1].x = ((other.m - m) * vTemp[1].x + 2 * m * vTemp[0].x) / (m + other.m);
      vFinal[1].y = vTemp[1].y;


      bTemp[0].x += vFinal[0].x;
      bTemp[1].x += vFinal[1].x;


      PVector[] bFinal = { 
        new PVector(), new PVector()
      };

      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;

      other.position.x = position.x + bFinal[1].x;
      other.position.y = position.y + bFinal[1].y;

      position.add(bFinal[0]);


      velocity.x = cosine * vFinal[0].x - sine * vFinal[0].y;
      velocity.y = cosine * vFinal[0].y + sine * vFinal[0].x;
      other.velocity.x = cosine * vFinal[1].x - sine * vFinal[1].y;
      other.velocity.y = cosine * vFinal[1].y + sine * vFinal[1].x;
    }
  }

  void display() {

    noStroke();

    fill(204);

    ellipse(position.x, position.y, radius*2, radius*2);

  }
  void triggerSample(){

    sampler.rate.setLastValue(10 / radius);

    sampler.trigger();
  }
}

}

Answers

  • Question =?

    Relevant line numbers?

  • edited March 2017

    @framos --

    Here is a simple walkthrough of how to combine collision detection with sound playing:

    1. Make a new sketch
    2. Add the bouncing bubbles example code: https://processing.org/examples/bouncybubbles.html
    3. Save an audio file in your sketch data folder, e.g. http://sounds.stoutman.com/sounds/boing.wav
    4. Add an output and a sample to your sketch header:

      import ddf.minim.*;
      import ddf.minim.ugens.*;
      Minim minim;
      AudioOutput out;
      Sampler sample;
      
    5. Load the sample and patch it to your output in setup():

      minim = new Minim(this);
      out   = minim.getLineOut();
      sample  = new Sampler( "boing.wav", 12, minim );
      sample.patch(out);
      
    6. Trigger the sample in the collision() method, inside the if statement that fires when a collision happens:

      sample.trigger();
      
    7. That's it! Now you have 12 voices worth of "boing" sounds when balls collide. For more, read the minim documentation on Sampler: http://code.compartmental.net/minim/sampler_class_sampler.html

Sign In or Register to comment.