当玩家超过 1 个方块时碰撞抽搐
Collision twitching when player is on more than 1 block
我正在用 Monogame 编码的基于 2D 图块的游戏中编写碰撞代码。我 运行 遇到一个问题,如果我的玩家同时站在 2 个方块上,它会开始抽搐,因为碰撞解决是 运行ning 2 次,我希望它只 运行 一次。我该怎么做?这是我的代码:
public void HandleCollisions()
{
Rectangle plyRect = ply.GetBounds(); //get player rectangle
Vector2 currentPos = new Vector2(plyRect.X, plyRect.Y); //get player current position
Vector2 xy = new Vector2( (float)Math.Floor((ply.pos.X + ply.tex.Width) / world.tileSize),
(float)Math.Floor((ply.pos.Y + ply.tex.Height) / world.tileSize)); //get tiles position based on player position
for (int x = (int)xy.X - 4; x <= (int)xy.X + 4; x++) //run through tiles near the player
{
for (int y = (int)xy.Y - 4; y <= (int)xy.Y + 4; y++)
{
if (x >= 0 && y >= 0 && x < world.GetWorldSize().X && y < world.GetWorldSize().Y) //check if tiles are within map
{
if (world.tiles[x, y] != null)
{
if (world.tiles[x, y].collision == Tile.Collision.SOLID) //check if tile is solid
{
Rectangle tileRect = world.tiles[x, y].GetRect(); //get the tiles rectangle
if (plyRect.Intersects(tileRect)) //check if intersecting
{
Vector2 depth = RectangleExtension.GetIntersectionDepth(plyRect, tileRect); //get intersecting depth
if (depth != Vector2.Zero)
{
float absDepthX = Math.Abs(depth.X);
float absDepthY = Math.Abs(depth.Y);
if (absDepthY < absDepthX)
{
currentPos = new Vector2(currentPos.X, currentPos.Y + depth.Y); //resolve Y collision first
}
else
{
currentPos = new Vector2(currentPos.X + depth.X, currentPos.Y); //then resolve X collision
}
}
}
}
}
}
}
}
ply.pos = currentPos; //set player position after the checking is done
}
一旦检测到碰撞,使用 break 跳出 for 循环;声明
我正在用 Monogame 编码的基于 2D 图块的游戏中编写碰撞代码。我 运行 遇到一个问题,如果我的玩家同时站在 2 个方块上,它会开始抽搐,因为碰撞解决是 运行ning 2 次,我希望它只 运行 一次。我该怎么做?这是我的代码:
public void HandleCollisions()
{
Rectangle plyRect = ply.GetBounds(); //get player rectangle
Vector2 currentPos = new Vector2(plyRect.X, plyRect.Y); //get player current position
Vector2 xy = new Vector2( (float)Math.Floor((ply.pos.X + ply.tex.Width) / world.tileSize),
(float)Math.Floor((ply.pos.Y + ply.tex.Height) / world.tileSize)); //get tiles position based on player position
for (int x = (int)xy.X - 4; x <= (int)xy.X + 4; x++) //run through tiles near the player
{
for (int y = (int)xy.Y - 4; y <= (int)xy.Y + 4; y++)
{
if (x >= 0 && y >= 0 && x < world.GetWorldSize().X && y < world.GetWorldSize().Y) //check if tiles are within map
{
if (world.tiles[x, y] != null)
{
if (world.tiles[x, y].collision == Tile.Collision.SOLID) //check if tile is solid
{
Rectangle tileRect = world.tiles[x, y].GetRect(); //get the tiles rectangle
if (plyRect.Intersects(tileRect)) //check if intersecting
{
Vector2 depth = RectangleExtension.GetIntersectionDepth(plyRect, tileRect); //get intersecting depth
if (depth != Vector2.Zero)
{
float absDepthX = Math.Abs(depth.X);
float absDepthY = Math.Abs(depth.Y);
if (absDepthY < absDepthX)
{
currentPos = new Vector2(currentPos.X, currentPos.Y + depth.Y); //resolve Y collision first
}
else
{
currentPos = new Vector2(currentPos.X + depth.X, currentPos.Y); //then resolve X collision
}
}
}
}
}
}
}
}
ply.pos = currentPos; //set player position after the checking is done
}
一旦检测到碰撞,使用 break 跳出 for 循环;声明