衝突検出を変更して正しく機能させる方法を見つけようとして立ち往生しています。すべての壁オブジェクトをリスト内に積み重ねてから、プレーヤーが移動すると、各壁オブジェクトをループして DetectCollision メソッドを呼び出すと、true または false が返されます。オブジェクトが壁の内側にあるかどうかによって異なります。
壁は衝突を検出します (X および Y 座標は壁の位置です)
public bool DetectCollision(float x, float y)
{
if ((x >= this.XCoordinate && x <= (this.XCoordinate + this.BlockWidth)) && (y >= this.YCoordinate && y <= (this.YCoordinate + this.BlockHeight)))
return true;
else
return false;
}
したがって、プレーヤーが移動しようとすると、プレーヤー関数で一時的な X、Y 座標に動きを追加し、それらが壁に衝突するかどうかを確認し、何も起こらない場合はプレーヤーを移動します。
しかし、ゲームフィールドの内側に壁を追加すると、衝突検出のために右下隅のみがチェックされるので、正常に機能しないことに気付きました。
プレイヤーの移動方法:
float x, y;
if (direction == Direction.E)
{
x = LiveObjects.player.XCoordinate - MovementSpeed;
y = LiveObjects.player.YCoordinate;
}
else if (direction == Direction.W)
{
x = LiveObjects.player.XCoordinate + MovementSpeed;
y = LiveObjects.player.YCoordinate;
}
else if (direction == Direction.N)
{
x = LiveObjects.player.XCoordinate;
y = LiveObjects.player.YCoordinate - MovementSpeed;
}
else
{
x = LiveObjects.player.XCoordinate;
y = LiveObjects.player.YCoordinate + MovementSpeed;
}
if (GameMechanics.DetectWallCollision(x, y) || GameMechanics.DetectWallCollision((x + LiveObjects.player.BlockWidth), (y + LiveObjects.player.BlockHeight))
{
OnPlayerInvalidMove(null, new PlayerEventArgs());
return;
}
DetectWallCollision のループは次のとおりです。
foreach (Wall wall in LiveObjects.walls)
{
if (wall.DetectCollision(x, y))
return true;
}
return false;
何か案は?