1

私は物理シミュレーションを持っており、領域制約を設定して、その領域内の物体がその領域から出ないようにすることができます。ただし、原子が領域制約の「壁」の 1 つを通過すると、物理シミュレーションが爆発します。なぜこれを行うのですか?更新方法:

if (!atom.IsStatic)
{
    Vector2 force = Vector2.Zero;
    bool x = false, y = false;
    if (atom.Position.X - atom.Radius < _min.X)
    {
        force = new Vector2(-(_min.X - atom.Position.X), 0);
        if (atom.Velocity.X < 0)
            x = true;
    }
    if (atom.Position.X + atom.Radius > _max.X)
    {
        force = new Vector2(atom.Position.X - _max.X, 0);
        if (atom.Velocity.X > 0)
            x = true;
    }
    if (atom.Position.Y - atom.Radius < _min.Y)
    {
        force = new Vector2(0, -(_min.Y - atom.Position.Y));
        if (atom.Velocity.Y < 0)
            y = true;
    }
    if (atom.Position.Y + atom.Radius > _max.Y)
    {
        force = new Vector2(0, atom.Position.Y - _max.Y);
        if (atom.Velocity.Y > 0)
            y = true;
    }
    atom.ReverseVelocityDirection(x, y);
    if (!atom.IsStatic)
    {
        atom.Position += force;
    }
}
4

3 に答える 3

1

あなたはすでに問題を解決しているようですが、「力」が間違っているように思われます。反対側にある場合でも、原子を境界から遠ざけます。アトムが _max.X を通過したとします。

if (atom.Position.X + atom.Radius > _max.X)
    {
        force = new Vector2(atom.Position.X - _max.X, 0);
        ...
    }

「力」は +x 方向になり、壁からの原子の距離は反復ごとに 2 倍になります。ブーム!

于 2009-10-29T17:14:40.910 に答える
0

約 30 分間無意識にハッキングした後、単純に位置補正を適用しないことを考えました。それはそれを魅力のように修正しました。興味のある方のために、更新されたコードは次のとおりです。

if (!atom.IsStatic)
{
    if (atom.Position.X - atom.Radius < _min.X && atom.Velocity.X < 0)
    {
        atom.ReverseVelocityDirection(true, false);
    }
    if (atom.Position.X + atom.Radius > _max.X && atom.Velocity.X > 0)
    {
        atom.ReverseVelocityDirection(true, false);
    }
    if (atom.Position.Y - atom.Radius < _min.Y && atom.Velocity.Y < 0)
    {
        atom.ReverseVelocityDirection(false, true);
    }
    if (atom.Position.Y + atom.Radius > _max.Y && atom.Velocity.Y > 0)
    {
        atom.ReverseVelocityDirection(false, true);
    }
}
于 2009-10-23T10:59:09.513 に答える