分离两个碰撞的圆

发布于 2024-12-02 10:17:40 字数 1756 浏览 2 评论 0原文

我试图将两个正在碰撞的圆分开。感谢其他人的帮助,我就在那里!

这是我的代码:

var totalRadius : Number = _circle1.radius + _circle2.radius;
var x : Number = _circle1.position.x - _circle2.position.x;
var y : Number = _circle1.position.y - _circle2.position.y;

var distanceSquared : Number = (x * x) + (y * y);

if (distanceSquared < totalRadius * totalRadius)
{   
    var distance : Number = Math.sqrt(distanceSquared);

    var separation : Number = totalRadius - distance;

    var unitVectorX : Number = (_circle1.position.x - _circle2.position.x) / distance;
    var unitVectorY : Number = (_circle1.position.y - _circle2.position.y) / distance;

    _circle1.position.x += unitVectorX * (separation / 2);
    _circle1.position.y += unitVectorY * (separation / 2);

    _circle2.position.x -= unitVectorX * (separation / 2);
    _circle2.position.y -= unitVectorY * (separation / 2);
}

如果圆具有相同的速度,效果很好。当它们具有不同的速度时就会出现问题,并且问题是因为我均匀地分割了分离(分离/ 2)我认为!

如果circle1 的速度为1,0 并且circle2 的速度为-1,0,则一切正常。两个圆圈相撞,停了下来。

如果圆 1 的速度为 2,0,圆 2 的速度为 -1,0,则圆逐渐向右移动。我想这就是发生的事情:

frame1:

  • circle1(99, 100)
  • circle2(101, 100)

frame2:

  • circle1(101, 100)
  • circle2(100, 100)
  • 检测到碰撞,校正位置分别为-0.5和+0.5。
  • 圆1 (100.5, 100)
  • 圆2 (100.5, 100)

帧3:

  • 圆1 (102.5, 100)
  • 圆2 (99.5, 100)
  • 检测到碰撞,校正位置分别为-1.5 和+1.5。
  • 圆1 (101, 100)
  • 圆2 (101, 100)

第4帧:

  • 圆1 (103, 100)
  • 圆2 (100, 100)
  • 检测到碰撞,校正位置分别为-1.5和+1.5。
  • 圆1 (101.5, 100)
  • 圆2 (101.5, 100)

如您所见,由于速度差异,两个圆都向右增加了+0.5。

最后,我的问题是:我怎样才能将它们的速度纳入方程中,这样它就不会影响它们的分离?

谢谢!

I'm trying to separate two circles that are colliding. Thanks to the help of others, I'm right there!

This is my code:

var totalRadius : Number = _circle1.radius + _circle2.radius;
var x : Number = _circle1.position.x - _circle2.position.x;
var y : Number = _circle1.position.y - _circle2.position.y;

var distanceSquared : Number = (x * x) + (y * y);

if (distanceSquared < totalRadius * totalRadius)
{   
    var distance : Number = Math.sqrt(distanceSquared);

    var separation : Number = totalRadius - distance;

    var unitVectorX : Number = (_circle1.position.x - _circle2.position.x) / distance;
    var unitVectorY : Number = (_circle1.position.y - _circle2.position.y) / distance;

    _circle1.position.x += unitVectorX * (separation / 2);
    _circle1.position.y += unitVectorY * (separation / 2);

    _circle2.position.x -= unitVectorX * (separation / 2);
    _circle2.position.y -= unitVectorY * (separation / 2);
}

It works great if the circles have the same velocity. The problem occurs when they have different velocities and the problem is because I'm splitting the separation evenly (separation / 2) I think!

Everything works perfectly if circle1 has a velocity of 1,0 and circle2 has a velocity of -1,0. The two circles hit each other and stop.

If circle1 has a velocity of 2,0 and circle2 has a velocity of -1,0, the circles gradually move to the right. I imagine this is what's happening:

frame1:

  • circle1 (99, 100)
  • circle2 (101, 100)

frame2:

  • circle1 (101, 100)
  • circle2 (100, 100)
  • collision detected, corrected position of -0.5 and +0.5 respectively.
  • circle1 (100.5, 100)
  • circle2 (100.5, 100)

frame3:

  • circle1 (102.5, 100)
  • circle2 (99.5, 100)
  • collision detected, corrected position of -1.5 and +1.5 respectively.
  • circle1 (101, 100)
  • circle2 (101, 100)

frame4:

  • circle1 (103, 100)
  • circle2 (100, 100)
  • collision detected, corrected position of -1.5 and +1.5 respectively.
  • circle1 (101.5, 100)
  • circle2 (101.5, 100)

As you can see, both circles are gaining +0.5 to the right because of the difference of velocity.

So finally, my question: How can I factor in their velocity into the equation so that it doesn't play a factor in their separation?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み零 2024-12-09 10:17:40

您需要计算撞击点,而不是仅仅(任意)将它们均等地向后移动。

快速搜索找到了这些链接:

  1. http://www.t3hprogrammer.com/research/circle-circle-collision-tutorial#TOC-Static-Circle-Circle-Collision-Dete(“动态圆-圆碰撞”部分”)

  2. http://nonlinear.openspark.com/tutorials/vectors/index .htm(“影响,而不是交集”部分)

You need to calculate the point of impact, rather than just (arbitrarily) moving them both backwards equally.

A quick search found these links:

  1. http://www.t3hprogrammer.com/research/circle-circle-collision-tutorial#TOC-Static-Circle-Circle-Collision-Dete (the section "Dynamic Circle-Circle Collision")

  2. http://nonlinear.openspark.com/tutorials/vectors/index.htm (section "Impact, not intersection")

素罗衫 2024-12-09 10:17:40

简而言之:你必须在那里获得 动量 ;)

正如我猜测的您希望质量相同,可以归结为“v1_before^2 + v2_before^2 = v1_after^2 + v2_after^2”。正如维基文章所建议的,我只是“切换”速度。

我不明白的是为什么你认为圆圈都会向右移动?这不应该是弹性碰撞吗?那么如果你希望它们具有相同的质量,它们应该朝不同的方向移动。

to make the answer short: you will have to get momentum in there ;)

As I guess you want to have the masses the same it comes down to "v1_before^2 + v2_before^2 = v1_after^2 + v2_after^2". As the wiki-article suggests I just "switch" the velocities.

What I do not understand is why you think the circles will both move to the right? Isn't this suppost to be a elastic collision? Then they should go in different directions if you want them to have same mass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文