Class TiledMapCollisions
A collision handler for a MonoGame.Extended tiled tile map. The idea behind this collision handler is that, on the map's tileset, each tile is assigned a certain rectangular area. That area is converted into a collision map that is dealt with in tile units, where each tile's covered area is 1x1 units big.
public class TiledMapCollisions
- Inheritance
-
TiledMapCollisions
- Inherited Members
Constructors
TiledMapCollisions(TiledMap, CollectCollisions)
Creates a new tiled map collision handler for the given map
public TiledMapCollisions(TiledMap map = null, TiledMapCollisions.CollectCollisions collisionFunction = null)
Parameters
mapTiledMapThe map
collisionFunctionTiledMapCollisions.CollectCollisionsThe function used to collect the collision info of a tile, or null to use DefaultCollectCollisions(List<RectangleF>, TileCollisionInfo)
Methods
DefaultCollectCollisions(List<RectangleF>, TileCollisionInfo)
The default implementation of TiledMapCollisions.CollectCollisions which is used by SetMap(TiledMap, CollectCollisions) if no custom collision collection function is passed
public static void DefaultCollectCollisions(List<RectangleF> collisions, TiledMapCollisions.TileCollisionInfo tile)
Parameters
collisionsList<RectangleF>The list of collisions to add to
tileTiledMapCollisions.TileCollisionInfoThe tile's collision information
GetCollidingAreas(RectangleF, Func<TileCollisionInfo, bool>)
Returns an enumerable of all of the Collisions of the colliding tiles in the given area. This method is a convenience method based on GetCollidingTiles(RectangleF, Func<TileCollisionInfo, bool>).
public IEnumerable<RectangleF> GetCollidingAreas(RectangleF area, Func<TiledMapCollisions.TileCollisionInfo, bool> included = null)
Parameters
areaRectangleFThe area to check for collisions in
includedFunc<TiledMapCollisions.TileCollisionInfo, bool>A function that determines if a certain info should be included or not
Returns
- IEnumerable<RectangleF>
An enumerable of collision rectangles for that area
GetCollidingTiles(RectangleF, Func<TileCollisionInfo, bool>)
Returns an enumerable of tile collision infos that intersect the given area. Optionally, a predicate can be supplied that narrows the search.
public IEnumerable<TiledMapCollisions.TileCollisionInfo> GetCollidingTiles(RectangleF area, Func<TiledMapCollisions.TileCollisionInfo, bool> included = null)
Parameters
areaRectangleFThe area to check for collisions in
includedFunc<TiledMapCollisions.TileCollisionInfo, bool>A function that determines if a certain info should be included or not
Returns
- IEnumerable<TiledMapCollisions.TileCollisionInfo>
An enumerable of collision infos for that area
GetPenetrations(Func<RectangleF>, Func<TileCollisionInfo, bool>, bool)
Returns an enumerable of normals and penetration amounts for each TiledMapCollisions.TileCollisionInfo that intersects with the given MonoGame.Extended.RectangleF area.
The normals and penetration amounts are based on Penetrate(RectangleF, RectangleF, out Vector2, out float).
Note that all x penetrations are returned before all y penetrations, which improves collision detection in sidescrolling games with gravity. Note that this behavior can be inverted using prioritizeX.
public IEnumerable<(Vector2, float)> GetPenetrations(Func<RectangleF> getArea, Func<TiledMapCollisions.TileCollisionInfo, bool> included = null, bool prioritizeX = true)
Parameters
getAreaFunc<RectangleF>The area to penetrate
includedFunc<TiledMapCollisions.TileCollisionInfo, bool>A function that determines if a certain info should be included or not
prioritizeXboolWhether all x penetrations should be prioritized (returned first). If this is false, all y penetrations are prioritized instead.
Returns
- IEnumerable<(Vector2, float)>
A set of normals and penetration amounts
IsColliding(RectangleF, Func<TileCollisionInfo, bool>)
Returns whether there are any colliding tiles in the given area. Optionally, a predicate can be supplied that narrows the search.
public bool IsColliding(RectangleF area, Func<TiledMapCollisions.TileCollisionInfo, bool> included = null)
Parameters
areaRectangleFThe area to check for collisions in
includedFunc<TiledMapCollisions.TileCollisionInfo, bool>A function that determines if a certain info should be included or not
Returns
- bool
True if there are any colliders in the area, false otherwise
SetMap(TiledMap, CollectCollisions)
Sets this collision handler's handled map
public void SetMap(TiledMap map, TiledMapCollisions.CollectCollisions collisionFunction = null)
Parameters
mapTiledMapThe map
collisionFunctionTiledMapCollisions.CollectCollisionsThe function used to collect the collision info of a tile, or null to use DefaultCollectCollisions(List<RectangleF>, TileCollisionInfo)
UpdateCollisionInfo(int, int, int)
Updates the collision info for the tile at the given position.
public void UpdateCollisionInfo(int layerIndex, int x, int y)