【发布时间】:2021-09-26 06:48:05
【问题描述】:
我正在创建一个程序,其中一个圆圈在一个更大的圆圈内部反弹。 我已经弄清楚了如何计算圆圈碰撞的时间和地点,但不知道如何计算应该应用的弹跳圆圈上的结果向量。任何帮助将不胜感激。
下面或web editor中的代码。
let pos, vel, acc;
function setup() {
createCanvas(400, 400);
pos = createVector(width / 2 + 20, height / 2 - 10); //inital position of ball
vel = createVector(0, 0.5); //inital velocity
acc = createVector(0.1, 0.1); //inital acc
}
function draw() {
background(220);
fill(255)
//vel.add(acc); //acceleration disabled due to testing
pos.add(vel); //adds velocity to the position
ellipse(pos.x, pos.y, 20); //draws the ball
noFill();
circle(width / 2, height / 2, 100); //draws the main ball (border)
if (dist(pos.x, pos.y, width / 2, height / 2) >= 50 - 10) { //if the ball hits the circle
theta = atan2(pos.y - height / 2, pos.x - width / 2);
pX = 50 * cos(theta) + width / 2;
pY = height / 2 + 50 * sin(theta);
POI = createVector(pX, pY); //point of intersection vector
fill(255, 0, 0);
circle(pX, pY, 2);
//a vector that when applied, *should* bounce the circle back
oppositeForceVector = p5.Vector.sub(POI, (width / 2, height / 2));
vel.add(oppositeForceVector);
vel.limit(1);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
【问题讨论】:
-
您可能会发现这很有帮助:openprocessing.org/sketch/1174676 它涵盖了稍微不同的场景,但概念是相同的。
标签: javascript collision p5.js