开发者

Separating two colliding circles

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开发者_JAVA技巧 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!


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")


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜