gpt4 book ai didi

java - 二维球碰撞问题 : no conservation of energy

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:55:36 27 4
gpt4 key购买 nike

我正在尝试编写一个简单的物理模拟,其中具有不同半径和质量的球在完美弹性和无摩擦的环境中弹跳。我按照以下资源编写了自己的代码:http://www.vobarian.com/collisions/2dcollisions2.pdf我还测试了这里的代码:Ball to Ball Collision - Detection and Handling

问题已编辑

在 Rick Goldstein 和 Ralph 的帮助下,我的代码可以正常工作(有一个错字……)。非常感谢你的帮助。但是,我仍然对为什么其他算法对我不起作用感到困惑。球在正确的方向反弹,但系统的总能量永远不会守恒。速度越来越快,直到球开始在屏幕上的静止位置闪烁。我其实很想在我的程序中使用这段代码,因为它比我写的要简洁很多。

这是我编写的功能算法(尽管我确实从其他来源获取了第一部分)。它在 Bubble 类中:

public void resolveCollision(Bubble b)
{
// get the minimum translation distance
Vector2 delta = (position.subtract(b.position));
float d = delta.getMagnitude();
// minimum translation distance to push balls apart after intersecting
Vector2 mtd = delta.multiply(((getRadius() + b.getRadius())-d)/d);

// resolve intersection --
// inverse mass quantities
float im1 = 1 / getMass();
float im2 = 1 / b.getMass();

// push-pull them apart based off their mass
position = position.add(mtd.multiply(im1 / (im1 + im2)));
b.position = b.position.subtract(mtd.multiply(im2 / (im1 + im2)));

//get the unit normal and unit tanget vectors
Vector2 uN = b.position.subtract(this.position).normalize();
Vector2 uT = new Vector2(-uN.Y, uN.X);

//project ball 1 & 2 's velocities onto the collision axis
float v1n = uN.dot(this.velocity);
float v1t = uT.dot(this.velocity);
float v2n = uN.dot(b.velocity);
float v2t = uT.dot(b.velocity);

//calculate the post collision normal velocities (tangent velocities don't change)
float v1nPost = (v1n*(this.mass-b.mass) + 2*b.mass*v2n)/(this.mass+b.mass);
float v2nPost = (v2n*(b.mass-this.mass) + 2*this.mass*v1n)/(this.mass+b.mass);

//convert scalar velocities to vectors
Vector2 postV1N = uN.multiply(v1nPost);
Vector2 postV1T = uT.multiply(v1t);
Vector2 postV2N = uN.multiply(v2nPost);
Vector2 postV2T = uT.multiply(v2t);

//change the balls velocities
this.velocity = postV1N.add(postV1T);
b.velocity = postV2N.add(postV2T);
}

这是行不通的

public void resolveCollision(Bubble b)
{
// get the minimum translation distance
Vector2 delta = (position.subtract(b.position));
float d = delta.getMagnitude();
// minimum translation distance to push balls apart after intersecting
Vector2 mtd = delta.multiply(((getRadius() + b.getRadius())-d)/d);

// resolve intersection --
// inverse mass quantities
float im1 = 1 / getMass();
float im2 = 1 / b.getMass();

// push-pull them apart based off their mass
position = position.add(mtd.multiply(im1 / (im1 + im2)));
b.position = b.position.subtract(mtd.multiply(im2 / (im1 + im2)));

// impact speed
Vector2 v = (this.velocity.subtract(b.velocity));
float vn = v.dot(mtd.normalize());

// sphere intersecting but moving away from each other already
if (vn > 0.0f) return;

// collision impulse (1f is the coefficient of restitution)
float i = (-(1.0f + 1f) * vn) / (im1 + im2);
Vector2 impulse = mtd.multiply(i);

// change in momentum
this.velocity = this.velocity.add(impulse.multiply(im1));
b.velocity = b.velocity.subtract(impulse.multiply(im2));
}

如果你发现了什么,请告诉我。谢谢

最佳答案

在设置 v1nPost 的行中是否有错字?看起来分母应该是 this.mass + b.mass,而不是 this.mass * b.mass

此外,因为您正在计算 thisb 之间的碰撞,您是否正在检查以确保您不会也在 之间进行相同的碰撞bthis,从而将应用于碰撞中每个参与气泡的增量加倍?

关于java - 二维球碰撞问题 : no conservation of energy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5008252/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com