2

私は過去数日間、ベクトルについて調べたり、検索したりしてきましたが、まだ数学について理解することができません..

私は 2 つの AABBを持っています。衝突時に、メソッドが Vector を返し、それを位置 Vector に追加して、オブジェクトを境界に戻すことができます。

これが私の現在のコードです:

(位置ベクトルは AABB の中心です)

public Vector2f collide(Sprite other) {
    if(!collideBoolean(other)) return null; //no collision

    float xAxis = Math.abs(this.position.x - other.getX()); //distance between centers on x axis
    float yAxis = Math.abs(this.position.y - other.getY()); //distance between centers on y axis

    //these combined values show the minimum distance apart the objects need to be to not collide
    int cw = this.getHalfWidth() + other.getHalfWidth(); //combined width
    int ch = this.getHalfHeight() + other.getHalfHeight(); //combined height

    float ox = Math.abs(xAxis - cw); //overlap on x
    float oy = Math.abs(yAxis - ch); //overlap on y

    //direction
    Vector2f dir = new Vector2f(this.position); 
    dir.sub(other.getPosition()); //subtract other.position from this.position
    dir.normalise();

    return new Vector2f(dir.x * ox, dir.y * oy);
}

(自明ですが、ここに collideBoolean(Sprite other) のコードもあります)

public boolean collideBoolean(Sprite other) {
    //code using halfwidths and centers
    if(Math.abs(this.position.x - other.getX()) > (this.getHalfWidth() + other.getHalfWidth())) return false;
    if(Math.abs(this.position.y - other.getY()) > (this.getHalfHeight() + other.getHalfHeight())) return false;

    return true;
}

私の現在のコードは多かれ少なかれ機能します..しかし、「その他」と衝突している(この)オブジェクトは、「その他」の最も近いコーナーに向かって押し出されます。

私は本当に近いと思います。確かに、別の目には目がくらむほど明白なものになるでしょうが、私はそれを完全に理解することはできません. どんな助けでも大歓迎です!

ありがとう、

編集:

これをcollide(Sprite other)メソッドの最後に追加すると機能しますが、私が望むほどきれいではありません。また、「他の」ボディに 1 つの軸だけに沿って移動する場合は正常に機能しますが、ボディを斜めに押し込むと、シェイプの内側に入り、ランダムに投げ出されます。

これはおそらく、ステップごとに移動するピクセルが多すぎるためです。ただし、衝突をスイープ テストする必要があります。

(このコードは、投影ベクトルを調べて、どのコンポーネントが大きいかを確認します。次に、0 が最大のコンポーネントになります。これは、最短パスに沿って形状からのみ投影していることを意味します)

    ....
    //direction
    ....

    Vector2f projection = new Vector2f(dir.x * (ox+1), dir.y * (oy+1));
    if(Math.abs(projection.x) > Math.abs(projection.y)) projection.x = 0;
    else if(Math.abs(projection.y) > Math.abs(projection.x)) projection.y = 0;

    return projection;
}

編集 2

Ishtar の回答の実装により、状況は良好に見えました。しかし、小さなオブジェクトを幅の広いオブジェクトと衝突させた場合、中心付近の衝突は正確に修正されますが、コーナーに近づくと形状に沈み込みます。

このような:

         _
 _______l_l_________
|                   |
|______OK___________|


 _--________________
| --                |
|_____SINKS IN______|

編集 3

現在の衝突コード:

public class Collision {

/** fix collision based on mass */
public static void collide(Sprite s1, Sprite s2) {
    float xAxis = Math.abs(s1.getX() - s2.getX()); //distance between centers
    float yAxis = Math.abs(s1.getY() - s2.getY()); //distance between centers

    int cw = s1.getHalfWidth() + s2.getHalfWidth(); //combined width
    int ch = s1.getHalfHeight() + s2.getHalfHeight(); //combined height

    //early exit
    if(xAxis > cw) return;
    if(yAxis > ch) return;

    float ox = Math.abs(xAxis - cw); //overlap on x
    float oy = Math.abs(yAxis - ch); //overlap on y

    if(s1.getMass() <= s2.getMass())
        fixCollision(s1, s2, ox+1, oy+1);    //the +1's make you get out of the shape instead of 
    else //if(s1.getMass() > s2.getMass())    //correcting you onto the edge where you'll be in constant collision
        fixCollision(s2, s1, ox+1, oy+1);
}

/**
 * Fixes the collision
 * @param s1 : this gets pushed out (should be lower mass)
 * @param s2 : this stays where it is
 * @param ox : the overlap along the x axis
 * @param oy : the overlap along the y axis
 */
private static void fixCollision(Sprite s1, Sprite s2, float ox, float oy) {
    //direction
    Vector2f dir = new Vector2f(s1.getPosition()); 
    dir.sub(s2.getPosition());
    dir.normalise();

    Vector2f projection = new Vector2f(dir.x * (ox), dir.y * (oy));
    if(Math.abs(projection.x) > Math.abs(projection.y)) projection.x = 0;
    else if(Math.abs(projection.y) > Math.abs(projection.x)) projection.y = 0;

    if(ox > oy) s1.getPosition().add( new Vector2f(0, dir.y * oy) ); //overlap is bigger on x so project on y
    else if(ox < oy) s1.getPosition().add( new Vector2f(dir.x * ox, 0)); //overlap is bigger on x so project on x
    else s1.getPosition().add( new Vector2f(dir.x * ox, dir.y * oy)); //corner to corner
}
4

2 に答える 2

1

「スライドの問題」を解決するには、次のようにします。(ただし、C#XNAコード)

Vector2 direction = (this.Center - other.Center);
direction.Normalize();

direction.X = (int)Math.Round(direction.X);
direction.Y = (int)Math.Round(direction.Y);

まず、終了する方向の単位ベクトルを取得します。しかし、それをそのまま使用すると、角を曲がったところで信頼性が低下します。丸めにより、最強の値が1になり、他の値は0になります。

丸めが必要な理由の説明:

たとえば、Y軸上の2つのオブジェクトの中心が等しく、左に出ているとします。方向の単位ベクトルは(-1、0)であり、オーバーラップを乗算するための信頼できる数値を提供します。しかし、図形の隅に近づくにつれて、数値はその非整数にさらに変わります(-0.88、 * )。乗算するとピクセルが失われ、図形に沈みます。

それ以来の説明を期待しますが、これらのことはあまり得意ではありません。

ちなみに、私はOPです^ _^..みんなの助けに感謝します

于 2012-02-07T16:39:45.787 に答える
1
float ox = Math.abs(xAxis - cw); //overlap on x
float oy = Math.abs(yAxis - ch); //overlap on y

//direction
Vector2f dir = new Vector2f(this.position); 
dir.sub(other.getPosition()); //subtract other.position from this.position
dir.normalise();

return new Vector2f(dir.x * ox, dir.y * oy);

返された平行移動ベクトルは、ox (x の重なり) と oy (y の重なり) の両方をゼロにします。しかし、それはあなたが必要とするものではありません。ox が 0 の場合、x 方向にオーバーラップがないため、まったくオーバーラップしません。oy が 0 の場合、同上。したがって、oxまたはoyのみをゼロにするベクトルを見つける必要があります。

if (ox > oy )
  return new Vector3f(0,dir.y*oy);//top-bottom collision
else if (ox < oy )
  return new Vector3f(dir.x*ox,0);//left-right collision
else //if(ox == oy)
  return new Vector3f(dir.x*ox,dir.y*oy); //corner-corner collision, 
                                          //unlikely with float's.
于 2011-12-15T10:30:55.657 に答える