私はプラットフォーマーに取り組んでおり、32x32 のスプライトと 32x32 のタイルがあります。また、配列を使用してマップを生成するタイル エンジンも使用します。を使用しRectangleHelper.cs
て、タイルとプレーヤーとの衝突を修正しました。これまでのところ、タイルの上部とタイルの左側にも衝突する可能性があります。
画像 1 では、「On Top Of」衝突が問題なく機能することを示しています。エラーも何もありません。
写真 2 では、「Collision Left Of」を示していますが、これもうまく機能しています。
しかし、写真 3 では、キャラクターが浮いていることがわかります。これは、衝突長方形が何らかの形で数ピクセル伸びているためであり、それが問題の 1 つであり、答えが見つからないようです。
写真 4 では、引き伸ばされた衝突長方形の右側から衝突しようとしましたが、1 ~ 2 フレームほど壁に飛び込んでしまい、スクリーンショットを撮ることができませんでしたが、それも 1 つです。問題の。
これRectangleHelper.cs
は、タイル衝突エンジンに関する YouTube の「oyyou9」のチュートリアルからのものです。彼にとっては、すべてがうまく機能します。
また、ブロックの底にぶつかるとキャラクターが消えてしまいます。
RectangleHelper.cs
public static class RectangleHelper
{
public static bool TouchTopOf(this Rectangle r1, Rectangle r2)
{
return (r1.Bottom >= r2.Top - 1 &&
r1.Bottom <= r2.Top + (r2.Height / 2) &&
r1.Right >= r2.Left + (r2.Width / 5) &&
r1.Left <= r2.Right - (r2.Width / 5));
}
public static bool TouchBottomOf(this Rectangle r1, Rectangle r2)
{
return (r1.Top <= r2.Bottom + (r2.Height / 5) &&
r1.Top >= r2.Bottom - 1 &&
r1.Right >= r2.Left + (r2.Width / 5) &&
r1.Left <= r2.Right - (r2.Width / 5));
}
public static bool TouchLeftOf(this Rectangle r1, Rectangle r2)
{
return (r1.Right <= r2.Right &&
r1.Right >= r2.Left - 5&&
r1.Top <= r2.Bottom - (r2.Width / 4) &&
r1.Bottom >= r2.Top + (r2.Width / 4));
}
public static bool TouchRightOf(this Rectangle r1, Rectangle r2)
{
return (r1.Left >= r2.Left &&
r1.Left <= r2.Right &&
r1.Top <= r2.Bottom - (r2.Width / 4) &&
r1.Bottom >= r2.Top + (r2.Width / 4));
}
}
ここでわかるように、たくさんのランダムな値があり、ビデオでは、彼はそれらが何に適しているかを実際には語っていませんでした.
そして、私のプレーヤークラスの衝突メソッド:
public void Collision(Rectangle newRectangle, int xOffset, int yOffset)
{
if (rectangle.TouchTopOf(newRectangle))
{
rectangle.Y = newRectangle.Y - rectangle.Height;
velocity.Y = 0f;
hasJumped = false;
}
if (rectangle.TouchLeftOf(newRectangle))
{
position.X = newRectangle.X - rectangle.Width * 2;
}
if (rectangle.TouchRightOf(newRectangle))
{
position.X = newRectangle.X + newRectangle.Width +1;
}
if (rectangle.TouchBottomOf(newRectangle))
{
velocity.Y = 1f;
}
}
を使用しRectangleHelper.cs
て衝突を修正します。