本文整理汇总了C#中Microsoft.Xna.Framework.Rectangle.GetIntersectionDepth方法的典型用法代码示例。如果您正苦于以下问题:C# Rectangle.GetIntersectionDepth方法的具体用法?C# Rectangle.GetIntersectionDepth怎么用?C# Rectangle.GetIntersectionDepth使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Xna.Framework.Rectangle
的用法示例。
在下文中一共展示了Rectangle.GetIntersectionDepth方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CheckCollision
public Vector2 CheckCollision(Vector2 position, int width, int height)
{
int minI = Math.Max(0, (int)Math.Floor((position.Y - (height / 2)) / 16));
int maxI = Math.Min(Map.Instance.CurrentPanel.Tiles.GetLength(0) - 1, (int)Math.Floor((position.Y + (height / 2)) / 16));
int minJ = Math.Max(0, (int)Math.Floor((position.X - (width / 2)) / 16));
int maxJ = Math.Min(Map.Instance.CurrentPanel.Tiles.GetLength(1) - 1, (int)Math.Floor((position.X + (width / 2)) / 16));
Rectangle bounds = new Rectangle((int)position.X - (width / 2), (int)position.Y - (height / 2), width, height);
for (int i = minI; i <= maxI; ++i)
{
for (int j = minJ; j <= maxJ; j++)
{
if (Map.Instance.CurrentPanel.Tiles[i, j] == 1)
{
Rectangle tileBounds = new Rectangle(j * 16, i * 16, 16, 16);
Vector2 depth = bounds.GetIntersectionDepth(tileBounds);
if (depth != Vector2.Zero)
{
float absDepthX = Math.Abs(depth.X);
float absDepthY = Math.Abs(depth.Y);
if (absDepthY < absDepthX)
{
bounds.Y += (int)depth.Y;
position.Y += depth.Y;
}
else
{
bounds.X += (int)depth.X;
position.X += depth.X;
}
}
}
}
}
return position;
}
示例2: ProcessLevelCollisions
private void ProcessLevelCollisions(Entity entity)
{
Entity level = _world.EntityManager.GetEntities(Aspect.All(typeof(Map)))[0];
if (level == null) return;
Map map = level.GetComponent<Map>();
CollisionBox box = entity.GetComponent<CollisionBox>();
Position position = entity.GetComponent<Position>();
Rectangle bounds = new Rectangle(
(int)position.X + box.OffsetX,
(int)position.Y + box.OffsetY,
box.Width,
box.Height
);
int leftTile = (int) Math.Floor((float) bounds.Left/TilesetConstants.TileWidth);
int rightTile = (int)Math.Ceiling(((float)bounds.Right / TilesetConstants.TileWidth)) - 1;
int topTile = (int) Math.Floor((float) bounds.Top/TilesetConstants.TileHeight);
int bottomTile = (int) Math.Ceiling(((float) bounds.Bottom/TilesetConstants.TileHeight)) - 1;
for (int y = topTile; y <= bottomTile; ++y)
{
for (int x = leftTile; x <= rightTile; ++x)
{
if (x >= map.Width || x < 0 ||
y >= map.Height || y < 0)
continue;
TileData tile = map.Tiles[x, y];
if (tile != null && tile.IsCollidable)
{
Rectangle tileBounds = new Rectangle(
x * TilesetConstants.TileWidth,
y * TilesetConstants.TileHeight,
TilesetConstants.TileWidth,
TilesetConstants.TileHeight
);
Vector2 depth = bounds.GetIntersectionDepth(tileBounds);
var type = bounds.GetCollisionType(tileBounds);
// Entity's bottom collided with top of tile.
if (type == CollisionType.Top)
{
position.Y += depth.Y;
position.IsOnGround = true;
break;
}
else if (type == CollisionType.Left)
{
}
else if (type == CollisionType.Right)
{
}
else if (type == CollisionType.Bottom)
{
}
}
}
}
}