2012-04-15 4 views
1

こんにちは、私はXNAを初めて使用しており、マウスのクリックを使用して文字がある場所から別の場所に移動するゲームプロトタイプを開発しようとしています。XNA Vector2のパスが矩形の内側に含まれています

私は現在の位置を表す矩形を持っています。プレーヤーのマウス入力を使用して、ターゲット位置をVector2として取得します。ベクトル2の減算によって、ソースからターゲットへの方向ベクトルを抽出します。

//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; 
    } 

答えて

1
  1. は、あなたも、だけではない方向
  2. たぶん、あなたは何を¿
  3. 以上の解像度を必要とする初期postionが必要ですか?これは、それはあなたがしようとしているものに依存し...何かあなたのコード、多分innacurateが向上します
    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; 
    } 
    


次のPOS用CALCSは少し複雑です
  • 「偽」の評価を削除...

    を行うには、多分あなたはそのヨーヨーを実現する必要がありブレゼンハムのアルゴリズムhttp://en.wikipedia.org/wiki/Bresenham「s_line_algorithmに

    を面白い見つけることができます船やキャラクターなどがソース位置にある場合は、計算量を増やす必要があるかもしれません。

  • 関連する問題