我一直在努力的弹性碰撞演示,似乎并不能得到完全正确。所述CIRCLE1对象似乎从CIRCLE2物体的一侧正确碰撞,但穿过CIRCLE2如果从相反侧碰撞。
这怎么能弹性碰撞进行修改,以更准确?
下面是与现场演示一个CodePen的链接。
main.ts
class DemoCanvas {
canvasWidth: number = 500;
canvasHeight: number = 500;
canvas: HTMLCanvasElement = document.createElement('canvas');
constructor() {
this.canvas.width = this.canvasWidth;
this.canvas.height = this.canvasHeight;
this.canvas.style.border = '1px solid black';
this.canvas.style.position = 'absolute';
this.canvas.style.left = '50%';
this.canvas.style.top = '50%';
this.canvas.style.transform = 'translate(-50%, -50%)';
document.body.appendChild(this.canvas);
}
clear() {
this.canvas.getContext('2d').clearRect(0, 0, this.canvas.width, this.canvas.height);
}
getContext(): CanvasRenderingContext2D {
return this.canvas.getContext('2d');
}
getWidth(): number {
return this.canvasWidth;
}
getHeight(): number {
return this.canvasHeight;
}
getTop(): number {
return this.canvas.getBoundingClientRect().top;
}
getRight(): number {
return this.canvas.getBoundingClientRect().right;
}
getBottom(): number {
return this.canvas.getBoundingClientRect().bottom;
}
getLeft(): number {
return this.canvas.getBoundingClientRect().left;
}
}
class Circle {
x: number;
y: number;
dx: number;
dy: number;
xVelocity: number;
yVelocity: number;
radius: number;
color: string;
canvas: DemoCanvas;
context: CanvasRenderingContext2D;
constructor(x: number, y: number, xVelocity: number, yVelocity: number, color: string, gameCanvas: DemoCanvas) {
this.radius = 20;
this.x = x;
this.y = y;
this.xVelocity = xVelocity;
this.yVelocity = yVelocity;
this.color = color;
this.canvas = gameCanvas;
this.context = this.canvas.getContext();
}
public draw(): void {
this.context.fillStyle = this.color;
this.context.beginPath();
this.context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
this.context.fill();
}
public move(): void {
this.x += this.xVelocity;
this.y += this.yVelocity;
}
checkWallCollision(gameCanvas: DemoCanvas): void {
let top = 0;
let right = 500;
let bottom = 500;
let left = 0;
if(this.y < top + this.radius) {
this.y = top + this.radius;
this.yVelocity *= -1;
}
if(this.x > right - this.radius) {
this.x = right - this.radius;
this.xVelocity *= -1;
}
if(this.y > bottom - this.radius) {
this.y = bottom - this.radius;
this.yVelocity *= -1;
}
if(this.x < left + this.radius) {
this.x = left + this.radius;
this.xVelocity *= -1;
}
}
}
let demoCanvas = new DemoCanvas();
let circle1: Circle = new Circle(250, 250, 5, 5, "#F77", demoCanvas);
let circle2: Circle = new Circle(250, 540, 5, 5, "#7FF", demoCanvas);
function detectCollisions():void {
if (circle1.x + circle1.radius + circle2.radius > circle2.x
&& circle1.x < circle2.x + circle1.radius + circle2.radius
&& circle1.y + circle1.radius + circle2.radius > circle2.y
&& circle1.y < circle2.y + circle1.radius + circle2.radius) {
if (distanceTo() < circle1.radius + circle2.radius) {
calculateNewVelocities();
}
}
}
function distanceTo():Number {
var distance = Math.sqrt(((circle1.x - circle2.x) * (circle1.x - circle2.x)) + ((circle1.y - circle2.y) * (circle1.y - circle2.y)));
if (distance < 0) {
distance = distance * -1;
}
return distance;
}
function calculateNewVelocities():void {
var mass1 = circle1.radius;
var mass2 = circle2.radius;
var velX1 = circle1.xVelocity;
var velX2 = circle2.xVelocity;
var velY1 = circle1.yVelocity;
var velY2 = circle2.yVelocity;
var newVelX1 = (velX1 * (mass1 - mass2) + (2 * mass2 * velX2)) / (mass1 + mass2);
var newVelY1 = (velY1 * (mass1 - mass2) + (2 * mass2 * velY2)) / (mass1 + mass2);
circle1.xVelocity = newVelX1;
circle1.yVelocity = newVelY1;
circle1.x = circle1.x + newVelX1;
circle1.y = circle1.y + newVelY1;
}
addEventListener('mousemove', function(e) {
let mouseX = e.clientX - demoCanvas.getLeft();
let mouseY = e.clientY - demoCanvas.getTop();
circle2.x = mouseX;
circle2.y = mouseY;
});
function loop() {
demoCanvas.clear();
circle1.draw();
circle2.draw();
circle1.move();
circle1.checkWallCollision(demoCanvas);
circle2.checkWallCollision(demoCanvas);
detectCollisions();
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
嗯,我不知道,你有你的公式来更新circle1
,当它与circle2
碰撞的速度,但the correct formula是取决于两个圆的相对位置的东西(的接触角为它们之间不只是距离,但好)。
如果我们假设circle2
将是固定的(或如此庞大,这circle1
不会影响其运动),然后在calculateNewVelocities()
代码应该是这个样子:
var xd = circle1.x - circle2.x; // x displacement between circles
var yd = circle1.y - circle2.y; // y displacement between circles
var dsq = xd*xd + yd*yd; // square of distance between circles
var DV = 2*(velX1*xd + velY1*yd)/dsq; // velocity change factor
// EDIT
// fix to prevent orbiting... if circle1 is already rebounding,
// leave it alone
if (DV > 0) {
DV=0;
}
// velocity change is in the opposite direction of
// the displacement between circles
var newVelX1 = velX1 - DV * xd;
var newVelY1 = velY1 - DV * yd;
这应该给你的东西隐约合理时,碰撞来自外部。如果您发现该圆重叠,这可能会导致circle1
轨道circle2
或一些疯狂的喜欢,所以你可能要调整,要杜绝此类事情的发生。
编辑:我认为“公转”的问题,我前面提到的是圆圈继续碰撞后重叠,引发多次冲突接二连三的结果。解决方法是,如果它已经从标题的circle1
中心客场不能修改circle2
的速度。这样,只有第一碰撞有任何影响和随后的检测到的碰撞被忽略,直到这样的时间circle1
朝向circle2
再次移动。
无论如何,这只是一个建议。希望你能找到你要找的内容。祝好运。