1

是)我有的:

很多气泡。但为了更简单,假设我有两个。当它们相遇时,它们会发生碰撞并改变方向。

var xVelocityBubble1 = Math.random();
var yVelocityBubble1 = Math.random();

var xVelocityBubble2 = Math.random();
var yVelocityBubble2 = Math.random();

moveBubbles = function() {
xbubble1 += xVelocityBubble1;
ybubble1 += yVelocityBubble1;

xbubble2 -= xVelocityBubble2;
xbubble2 -= yVelocityBubble2;

if (Math.sqrt(Math.pow(xbubble1 - xbubble2, 2) + Math.pow(ybubble1 - ybubble2, 2)) < radius * 2) {
xVelocityBubble1 *= -1;
yVelocityBubble1 *= -1;
xVelocityBubble2 *= -1;
yVelocityBubble2 *= -1;
}
}

我想要的是:

我不想让圆圈简单地改变方向,因为那看起来很奇怪和无聊。所以我想计算圆相交的角度,然后我需要计算它们交换了多少动量以及它如何影响每个圆。

我的问题:

我真的不知道如何计算角度和动量!有什么提示吗?

4

2 回答 2

1

如果它们碰撞,要获得这两个气泡之间的角度,请执行以下操作:

获取其中一个气泡移动的方向向量

direction = {x: Math.abs(xVelocityBubble1), y: Math.abs(yVelocityBubble1)};

然后对该向量进行归一化(将它的 x 和 y 分量除以它的长度)

这样做之后,您将获得角度的余弦作为 x 分量,将正弦作为 y,只需在Math.acosor中使用它们中的任何一个Math.asin,您就会得到它们碰撞的角度。

于 2012-10-19T17:25:51.963 回答
0

这段代码显示了小行星的碰撞:

            for (var i = 0; i < asteroidsLength; i++) {
                var tmpAsteroid = asteroids[i];

                for (var j = i + 1; j < asteroidsLength; j++) {
                    var tmpAsteroidB = asteroids[j];

                    var dX = tmpAsteroidB.x - tmpAsteroid.x;
                    var dY = tmpAsteroidB.y - tmpAsteroid.y;
                    var distance = Math.sqrt((dX * dX) + (dY * dY));

                    if (distance < tmpAsteroid.radius + tmpAsteroidB.radius) {
                        var angle = Math.atan2(dY, dX);
                        var sine = Math.sin(angle);
                        var cosine = Math.cos(angle);

                        // Rotate asteroid position
                        var x = 0;
                        var y = 0;

                        // Rotate asteroidB position
                        var xB = dX * cosine + dY * sine;
                        var yB = dY * cosine - dX * sine;

                        // Rotate asteroid velocity

                        var vX = tmpAsteroid.vX * cosine + tmpAsteroid.vY * sine;
                        var vY = tmpAsteroid.vY * cosine - tmpAsteroid.vX * sine;

                        // Rotate asteroidB velocity
                        var vXb = tmpAsteroidB.vX * cosine + tmpAsteroidB.vY * sine;
                        var vYb = tmpAsteroidB.vY * cosine - tmpAsteroidB.vX * sine;

                        // Conserve momentum
                        var vTotal = vX - vXb;
                        vX = ((tmpAsteroid.mass - tmpAsteroidB.mass) * vX + 2 * tmpAsteroidB.mass * vXb) / (tmpAsteroid.mass + tmpAsteroidB.mass);
                        vXb = vTotal + vX;

                        // Move asteroids apart
                        xB = x + (tmpAsteroid.radius + tmpAsteroidB.radius);

                        // Rotate asteroid positions back
                        tmpAsteroid.x = tmpAsteroid.x + (x * cosine - y * sine);
                        tmpAsteroid.y = tmpAsteroid.y + (y * cosine + x * sine);

                        tmpAsteroidB.x = tmpAsteroid.x + (xB * cosine - yB * sine);
                        tmpAsteroidB.y = tmpAsteroid.y + (yB * cosine + xB * sine);

                        // Rotate asteroid velocities back
                        tmpAsteroid.vX = vX * cosine - vY * sine;
                        tmpAsteroid.vY = vY * cosine + vX * sine;

                        tmpAsteroidB.vX = vXb * cosine - vYb * sine;
                        tmpAsteroidB.vY = vYb * cosine + vXb * sine;
                    };

                };
于 2012-10-23T23:55:49.867 回答