我在学校项目中做的圆圈/球碰撞模拟中遇到了这个奇怪的错误。我还可以补充一点,我有点菜鸟,我已经编程了大约一年。
在我用很多圆圈填充画布之前,碰撞工作正常。我真的不明白为什么。25 圈工作正常,但当我达到 50 圈时,圈子开始出现故障。
正如您在下面的链接中看到的那样,在运行了一段时间并疯狂地旋转之后,圆圈开始相互连接,这不是我喜欢发生的事情。
50 个圈子的示例: https ://dl.dropboxusercontent.com/u/9069602/circles/vers1/collisions.html
我的猜测是,随着圆圈之间的空间减小,圆圈相互连接的风险会更大,但在开始模拟时它们似乎有足够的空间。我使用 nextY/nextX 位置变量来计算实际影响发生前一帧的影响。这也可能是错误的来源。我无法真正指出要开始调试。
这是 50 圈案例的代码: https ://dl.dropboxusercontent.com/u/9069602/circles/vers1/circle.js
// Returns true if two circles are overlapping
function overlapDetection( circle1, circle2 ) {
var returnValue = false;
var dx = circle1.nextX - circle2.nextX;
var dy = circle1.nextY - circle2.nextY;
var distance = ( dx * dx + dy * dy );
if ( distance <= ( circle1.radius + circle2.radius ) * ( circle1.radius + circle2.radius ) ) {
returnValue = true;
}
return returnValue;
}
function collide() {
var circle;
var testCircle;
var returnValue = false;
for ( var i = 0; i < circles.length; i += 1 ) {
circle = circles[i];
for ( var j = i + 1; j < circles.length; j += 1 ) {
testCircle = circles[j];
if ( overlapDetection( circle, testCircle ) ) {
collideCircles( circle, testCircle );
collideCircle1 = circle.id;
collideCircle2 = testCircle.id;
returnValue = true;
}
}
}
return returnValue;
}
function collideCircles( circle1, circle2 ) {
var dx = circle1.nextX - circle2.nextX;
var dy = circle1.nextY - circle2.nextY;
var collisionAngle = Math.atan2( dy, dx );
var speed1 = Math.sqrt( circle1.velocityX * circle1.velocityX + circle1.velocityY * circle1.velocityY );
var speed2 = Math.sqrt( circle2.velocityX * circle2.velocityX + circle2.velocityY * circle2.velocityY );
var direction1 = Math.atan2( circle1.velocityY, circle1.velocityX );
var direction2 = Math.atan2( circle2.velocityY, circle2.velocityX );
var rotatedVelocityX1 = speed1 * Math.cos( direction1 - collisionAngle );
var rotatedVelocityY1 = speed1 * Math.sin( direction1 - collisionAngle );
var rotatedVelocityX2 = speed2 * Math.cos( direction2 - collisionAngle );
var rotatedVelocityY2 = speed2 * Math.sin( direction2 - collisionAngle );
var finalVelocityX1 = ( ( circle1.mass - circle2.mass ) * rotatedVelocityX1 + ( circle2.mass + circle2.mass ) * rotatedVelocityX2 ) / ( circle1.mass + circle2.mass );
var finalVelocityX2 = ( (circle1.mass + circle1.mass ) * rotatedVelocityX1 + ( circle2.mass - circle1.mass ) * rotatedVelocityX2 ) / ( circle1.mass + circle2.mass );
var finalVelocityY1 = rotatedVelocityY1;
var finalVelocityY2 = rotatedVelocityY2;
circle1.velocityX = Math.cos( collisionAngle ) * finalVelocityX1 + Math.cos( collisionAngle + Math.PI / 2 ) * finalVelocityY1;
circle1.velocityY = Math.sin( collisionAngle ) * finalVelocityX1 + Math.sin( collisionAngle + Math.PI / 2 ) * finalVelocityY1;
circle2.velocityX = Math.cos( collisionAngle ) * finalVelocityX2 + Math.cos( collisionAngle + Math.PI / 2 ) * finalVelocityY2;
circle2.velocityY = Math.sin( collisionAngle ) * finalVelocityX2 + Math.sin( collisionAngle + Math.PI / 2 ) * finalVelocityY2;
circle1.nextX += circle1.velocityX;
circle1.nextY += circle1.velocityY;
circle2.nextX += circle2.velocityX;
circle2.nextY += circle2.velocityY;
}
我希望我清楚这个问题。
提前致谢!