我試圖按照本教程http://www.tonypa.pri.ee/tbw/tut05.html在基於圖塊的世界中實現實時碰撞。基於圖塊的2D碰撞檢測問題
我覺得感謝我的實體的中心座標這些屬性:
public float CenterX
{
get { return X + Width/2f; }
set { X = value - Width/2f; }
}
public float CenterY
{
get { return Y + Height/2f; }
set { Y = value - Height/2f; }
}
然後在我的更新方法,在播放器類,它被稱爲每一幀,我有這樣的代碼:
public override void Update()
{
base.Update();
int downY = (int)Math.Floor((CenterY + Height/2f - 1)/16f);
int upY = (int)Math.Floor((CenterY - Height/2f)/16f);
int leftX = (int)Math.Floor((CenterX + Speed * NextX - Width/2f)/16f);
int rightX = (int)Math.Floor((CenterX + Speed * NextX + Width/2f - 1)/16f);
bool upleft = Game.CurrentMap[leftX, upY] != 1;
bool downleft = Game.CurrentMap[leftX, downY] != 1;
bool upright = Game.CurrentMap[rightX, upY] != 1;
bool downright = Game.CurrentMap[rightX, downY] != 1;
if(NextX == 1)
{
if (upright && downright)
CenterX += Speed;
else
CenterX = (Game.GetCellX(CenterX) + 1)*16 - Width/2f;
}
}
downY,upY,leftX和rightX應該分別找到最低的Y位置,最高的Y位置,最左邊的X位置和最右邊的X位置。我想補充+速度* NextX因爲在本教程中了getMyCorners函數被調用,這些參數:
getMyCorners (ob.x+ob.speed*dirx, ob.y, ob);
的GetCellX和GetCellY方法:
public int GetCellX(float mX)
{
return (int)Math.Floor(mX/SGame.Camera.TileSize);
}
public int GetCellY(float mY)
{
return (int)Math.Floor(mY/SGame.Camera.TileSize);
}
的問題是球員「閃爍」,同時擊中一個牆壁,角落檢測甚至不能正常工作,因爲它可以重疊僅撞擊其中一個角落的牆壁。我不明白什麼是錯的。在教程中,ob.x和ob.y字段應該與我的CenterX和CenterY屬性相同,ob.width和ob.height應該與Width/2f和Height/2f相同。但它仍然不起作用。
感謝您的幫助。
如果你沒有得到堆棧溢出的迴應,你可能想嘗試http://gamedev.stackexchange.com/ - 這是一個遊戲開發者專用的網站。也就是說,我會花一些時間,看看你首先得到什麼樣的迴應。 :-) – 2011-02-10 22:18:13
你的實體的維度是什麼?只有在實體的尺寸小於或等於瓦片尺寸時,此方法纔有效。 – Weeble 2011-02-10 23:04:06