xxxxxxxxxx
87
// calculate to how many digits?
let digits = 9;
// increase this to improve accuracy
let timeSteps = 6;
// rules (follow these or risk crashing your browser)
// do not put the timeSteps very high, i would keep it under 10
// dont put the digits extremely high, like seriously
// oh and the accuracy becomes useless past 9 digits
// time steps dont need to be put really high
// timestep of 6 does at least 9
let blockImg;
let block1;
let block2;
let clack;
let count = 0;
let countDiv;
let countDif;
timeSteps = timeSteps ** (digits - 1);
function preload() {
blockImg = loadImage('block.png');
clack = loadSound('clack.wav');
}
function setup() {
createCanvas(windowWidth, 200);
block1 = new Block(10, 20, 1, 0, 0);
const m2 = pow(100, digits - 1);
block2 = new Block(50, 100, m2, -1 / timeSteps, 20);
createDiv('number of collisions:');
countDiv = createDiv(count);
countDiv.style('font-size', '50pt');
createDiv('accuracy:');
countDif = createDiv(0);
countDif.style('font-size', '50pt');
}
function draw() {
let difPI = int(PI * pow(10, digits - 1));
background(200);
let clackSound = false;
for (let i = 0; i < timeSteps; i++) {
if (block1.collide(block2)) {
const v1 = block1.bounce(block2);
const v2 = block2.bounce(block1);
block1.v = v1;
block2.v = v2;
clackSound = true;
count++;
}
if (block1.hitWall()) {
block1.reverse();
clackSound = true;
count++;
}
block1.update();
block2.update();
}
if (clackSound) {
clack.play();
}
block1.show();
block2.show();
countDiv.html(nf(count, digits));
countDif.html(nf(difPI - count, digits));
}