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