1

こんにちは、私は XNA を初めて使用し、マウス クリックを使用してキャラクターがある場所から別の場所に移動するゲーム プロトタイプを開発しようとしています。

現在の位置を表す Rectangle があります。プレイヤーのマウス入力を使用して、ターゲットの位置を Vector2 として取得します。Vector2 減算により、ソースからターゲットへの方向ベクトルを抽出します。

//the cursor's coordinates should be the center of the target position
float x = mouseState.X - this.position.Width / 2;
float y = mouseState.Y - this.position.Height / 2;
Vector2 targetVector = new Vector2(x, y);
Vector2 dir = (targetVector - this.Center); //vector from source center to target
                                            //center

タイル マップを使用して世界を表現します。すべてのセルは 32x32 ピクセルです。

int tileMap[,];

私がやりたいことは、上記の方向ベクトルがマップ上の青いタイルを通過するかどうかを確認することです。青いタイルはマップ上で 1 です。

これを行う方法がわかりません。線形方程式と三角関数の公式を使用することを考えましたが、実装するのが難しいと感じています。ベクトルを正規化し、32 を掛けてベクトルのパスに沿って 32 ピクセルの長さの間隔を取得しようとしましたが、うまくいかないようです。何か問題があるかどうか、またはこの問題を解決する別の方法があるかどうか、誰か教えてもらえますか? ありがとう

//collision with blue wall. Returns point of impact
    private bool CheckCollisionWithBlue(Vector2 dir)
    {
        int num = Worldmap.size; //32
        int i = 0;
        int intervals = (int)(dir.Length() / num + 1);  //the number of 32-pixel length 
                                                        //inervals on the vector, with an edge
        Vector2 unit = Vector2.Normalize(dir) * num;    //a vector of length 32 in the same 
                                                        //direction as dir.
        Vector2 v = unit;
        while (i <= intervals & false)
        {
            int x = (int)(v.X / num);
            int y = (int)(v.Y / num);
            int type = Worldmap.getType(y, x);
            if (type == 1) //blue tile
            {
                return true;
            }
            else
            {
                i++;
                v = unit * i;
            }
        }
        return false;
    }
4

1 に答える 1

1
  1. 方向だけでなく、最初の位置も必要です
  2. 多分あなたはより多くの解像度が必要です
  3. 何?「偽の」評価を削除する
  4. 次の位置の計算は少し複雑です

 private bool CheckCollisionWithBlue(Vector2 source, Vector2 dir)
 {
    int num = 8; // pixel blocks of 8
    int i = 0;
    int intervals = (int)(dir.Length() / num);   

    Vector2 step = Vector2.Normalize(dir)*num;

    while (i <= intervals)
    {
        int x = (int)(source.X);
        int y = (int)(source.Y);
        int type = Worldmap.getType(y, x);
        if (type == 1) //blue tile
        {
            return true;
        }
        else
        {
            i++;
            source+=step;
        }
    }
    return false;
}

これはあなたのコードを改善するでしょう、しかし多分不正確です...それはあなたが何をしようとしているのかに依存します...

ブレゼンハムのラインアルゴリズムhttp://en.wikipedia.org/wiki/Bresenham's_line_algorithmがおもしろいと思うかもしれません。

船やキャラクター、またはソース位置にあるものが何かを追加する必要がある場合は、ボリュームの衝突ではなくラインの衝突を行っていることを理解する必要があります

于 2012-04-16T08:30:15.663 に答える