p5js 播放库中的错误弹跳行为
incorrect bouncing behaviour in p5js play library
我正在使用 play
库:https://molleindustria.github.io/p5.play/
我正在做一个简单的弹跳球演示,其中一个大球在 play
中的质量由 $\pi r^2$ 给出。基础物理学对我来说很合适。但是几秒钟后,大球在被小球击中时开始跳得很远。
这是我的完整代码(编辑,也粘贴在下面):https://editor.p5js.org/jmmcd/sketches/BMPtPY098
这似乎是 collision/bounce 代码中的错误。但是我的代码在概念上与https://molleindustria.github.io/p5.play/examples/index.html?fileName=collisions4.js相同,没有这个问题。
// 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;
}
}
}
p5.play 物理学中似乎存在一些错误,尤其是隧道。已经实施了一些重大改进,并且还会有更多改进。参见 https://github.com/molleindustria/p5.play/issues/214。
我正在使用 play
库:https://molleindustria.github.io/p5.play/
我正在做一个简单的弹跳球演示,其中一个大球在 play
中的质量由 $\pi r^2$ 给出。基础物理学对我来说很合适。但是几秒钟后,大球在被小球击中时开始跳得很远。
这是我的完整代码(编辑,也粘贴在下面):https://editor.p5js.org/jmmcd/sketches/BMPtPY098
这似乎是 collision/bounce 代码中的错误。但是我的代码在概念上与https://molleindustria.github.io/p5.play/examples/index.html?fileName=collisions4.js相同,没有这个问题。
// 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;
}
}
}
p5.play 物理学中似乎存在一些错误,尤其是隧道。已经实施了一些重大改进,并且还会有更多改进。参见 https://github.com/molleindustria/p5.play/issues/214。