private static IEnumerable <Block> GetAssociatedBlocks(IEnumerable <List <Vector2> > convexPolygons, Map.Map map, int layer, IDictionary <Block, List <Vector2> > blockPointsDictionary) { var blocks = new List <Block>(); foreach (var convexPolygon in convexPolygons) { float minX; float maxX; float minY; float maxY; CalculateBounds(convexPolygon, out minX, out maxX, out minY, out maxY); maxX = (float)Math.Ceiling(maxX); maxY = (float)Math.Ceiling(maxY); var pointsCache = new Dictionary <Vector2, bool>(); for (var y = (int)minY; y < maxY; y++) { for (var x = (int)minX; x < maxX; x++) { var block = map.GetBlock(new Vector3(x, y, layer)); List <Vector2> blockPoints; if (!blockPointsDictionary.TryGetValue(block, out blockPoints)) { blockPoints = GetBlockPoints(block, layer); blockPointsDictionary.Add(block, blockPoints); } var addBlock = false; foreach (var blockPoint in blockPoints) { bool isOnPolygon; if (pointsCache.TryGetValue(blockPoint, out isOnPolygon)) { continue; } isOnPolygon = IsPointInPolygonOrEdge(convexPolygon, blockPoint); pointsCache.Add(blockPoint, isOnPolygon); if (!isOnPolygon) { continue; } addBlock = true; break; } if (addBlock && !blocks.Contains(block)) { blocks.Add(block); } } } } return(blocks); }
private IList <ILineObstacle> GetBlockObstacles(int z) { var obstacles = new List <ILineObstacle>(); for (var x = 0; x < _map.Width; x++) { for (var y = 0; y < _map.Length; y++) { _map.GetBlock(new Vector3(x, y, z)).GetCollision(obstacles, false); } } return(obstacles); }
private static bool CheckBlocksAbove(Block block, Map.Map map, int layer, Dictionary <int, List <IObstacle> > layerObstacles, Dictionary <Block, List <Vector2> > blockPointsDictionary) { List <Vector2> blockPoints; if (!blockPointsDictionary.TryGetValue(block, out blockPoints)) { blockPoints = GetBlockPoints(block, layer); blockPointsDictionary.Add(block, blockPoints); } for (var z = (int)block.Position.Z + 1; z < 8; z++) { if (map.GetBlock(new Vector3(block.Position.X, block.Position.Y, z)).Lid) { return(true); } var blockFilled = false; foreach (var layerObstacle in layerObstacles[z]) { var containAll = false; var polygonObstacle = layerObstacle as PolygonObstacle; if (polygonObstacle != null) { containAll = blockPoints.All(polygonObstacle.Contains); } var rectangleObstacle = layerObstacle as RectangleObstacle; if (rectangleObstacle != null) { containAll = blockPoints.All(rectangleObstacle.Contains); } if (!containAll) { continue; } //all points are within the polygon, block is ok blockFilled = true; break; } if (blockFilled) { return(true); } } return(false); }
/// <summary> /// Initialize the physic world with the geometric detail of map. /// </summary> /// <param name="map">The base to create the physic world.</param> private void Initialize(Map.Map map) { List <JVector> vertices = new List <JVector>(); List <TriangleVertexIndices> indices = new List <TriangleVertexIndices>(); for (uint i = 0; i < map.Width; i++) { for (uint j = 0; j < map.Length; j++) { for (uint k = 0; k < map.Height; k++) { int pos = 0; Block block = map.GetBlock(new Vector3(i, j, k)); block.CreateColisions(); foreach (JVector vertice in block.CollisionVertices) { vertices.Add(vertice); pos++; } int newPos = vertices.Count - pos; foreach (TriangleVertexIndices indice in block.CollisionTriangles) { TriangleVertexIndices t = new TriangleVertexIndices(indice.I0 + newPos, indice.I1 + newPos, indice.I2 + newPos); indices.Add(t); } } } } //ToDo: The vertices list has duplicated vertices. In the worst case each vertices has 4 different instantiation. //Probably some performance can be expected by remove the duplicated ones. //However it is also necessary to update the indies List with the correct positions. Octree tree = new Octree(vertices, indices); TriangleMeshShape shape = new TriangleMeshShape(tree); RigidBody body = new RigidBody(shape); body.IsStatic = true; world.AddBody(body); }