Skip to content
Advertisement

incorrect bouncing behaviour in p5js play library

I’m using the play library: https://molleindustria.github.io/p5.play/

I’m doing a simple bouncing balls demo, with one large ball whose mass in play is given by $pi r^2$. The basic physics looks right to me. But after a few seconds, the large ball starts to jump large distances when struck by a small ball.

Here’s my complete code (EDIT, also pasted below): https://editor.p5js.org/jmmcd/sketches/BMPtPY098

It seems like a bug in the collision/bounce code. But my code is conceptually the same as https://molleindustria.github.io/p5.play/examples/index.html?fileName=collisions4.js, which doesn’t have this problem.

// Inspired by Drescher's discussion of the 
// arrow of time in "Good and Real"

// there is a bug I can't fix: soon after the animation starts,
// the large circle starts jumping as if being displaced or as 
// if a collision goes wrong

let circles;

function setup() {
  createCanvas(400, 400);
  r = 5; s = 1; // radius and speed of small circles
  R = 50; S = 2; // radius and speed of large circles
  circles = new Group;
  for (let i = 0; i < 1; i++) {
    // big circle(s)  
    circles.add(makeCircle(R, S)); 
  }
  for (let i = 0; i < 100; i++) {
    // lots of little circles  
    circles.add(makeCircle(r, s * random(0, 1)));  
  }
}

function makeCircle(r, s) {
  let col = [random(255), random(255), random(255)];
  c = createSprite(random(width), random(height), r, r);
  c.draw = function() { 
    fill(col); 
    ellipse(0, 0, r, r) 
  } 
  c.setCollider("circle");
  c.setSpeed(s, random(0, 360));
  c.mass = PI * r * r;
  c.scale = 1;
  return c;
}

function draw() {
  background(220);
  circles.bounce(circles);
  bounceGroupWalls(circles);
  for (let c of circles) {
    c.debug = mouseIsPressed;
  }
  drawSprites();
  status();
}

function status() {
  textSize(12);
  let s = 0;
  for (let c of circles) {
    s += c.getSpeed() * c.mass;
  }
  s = s.toFixed(0);
  text("Momentum " + s, 30, 30);
}

function bounceGroupWalls(g) {
  //all sprites in group g bounce at the screen edges
  for (let s of g) {
    if(s.position.x <= 0 || s.position.x >= width) {
      s.velocity.x *= -1;
    }
    if(s.position.y <= 0 || s.position.y >= height) {
      s.velocity.y *= -1;
    }    
  }
}

Advertisement

Answer

It seems there are some bugs in the p5.play physics, in particular tunneling. Some big improvements already implemented, and more are to come. See https://github.com/molleindustria/p5.play/issues/214.

Advertisement