こんにちは、私は 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;
}