/// <summary> /// Gets the 3D data from map centered in pos. /// </summary> /// <param name="pos">The center of square</param> /// <param name="size">The size of the square</param> /// <returns></returns> public Frame getPosition(Vector2 pos, uint size = 10) { Contract.Requires(map.ValidPosition(new Vector3(pos, 2))); //ToDo: work the case when some blocks don't exist because they are outside the map. In that case the frontier blocks should be repeated. List <VertexPositionNormalTexture> VertexPosList = new List <VertexPositionNormalTexture>(); List <int> IndexBufferList = new List <int>(); for (uint i = (uint)pos.X - size; i < pos.X + size; i++) { for (uint j = (uint)(pos.Y) - size; j < pos.Y + size; j++) { for (uint k = 0; k < map.Height; k++) { Block block = map.GetBlock(new Vector3(i, j, k)); int idx = 0; foreach (VertexPositionNormalTexture vx in block.Coors) { VertexPosList.Add(vx); idx++; } int c = VertexPosList.Count - idx; foreach (int ib in block.IndexBufferCollection) { IndexBufferList.Add(c + ib); } } } } List <VertexPositionNormalTexture> objectsVertexPosList = new List <VertexPositionNormalTexture>(); List <int> objectsIndexBufferList = new List <int>(); foreach (Pedestrian ped in pedList) { Frame md = ped.Draw(); int idx = 0; foreach (VertexPositionNormalTexture vx in md.ObjectVertexList) { objectsVertexPosList.Add(vx); idx++; } int c = objectsVertexPosList.Count - idx; foreach (int ib in md.ObjectIndexList) { objectsIndexBufferList.Add(c + ib); } } return(new Frame(VertexPosList, IndexBufferList, objectsVertexPosList, objectsIndexBufferList, pos)); }
/// <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); }
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 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> /// Gets the 3D data from map centered in pos. /// </summary> /// <param name="pos">The center of square</param> /// <param name="size">The size of the square</param> /// <returns></returns> public Frame getPosition(Vector2 pos, uint size = 10) { Contract.Requires(map.ValidPosition(new Vector3(pos, 2))); //ToDo: work the case when some blocks don't exist because they are outside the map. In that case the frontier blocks should be repeated. List <VertexPositionNormalTexture> VertexPosList = new List <VertexPositionNormalTexture>(); List <int> IndexBufferList = new List <int>(); for (uint i = (uint)pos.X - size; i < pos.X + size; i++) { for (uint j = (uint)(pos.Y) - size; j < pos.Y + size; j++) { for (uint k = 0; k < map.Height; k++) { Block block = map.GetBlock(new Vector3(i, j, k)); int idx = 0; foreach (VertexPositionNormalTexture vx in block.Coors) { VertexPosList.Add(vx); idx++; } int c = VertexPosList.Count - idx; foreach (int ib in block.IndexBufferCollection) { IndexBufferList.Add(c + ib); } } } } List <VertexPositionNormalTexture> objectsVertexPosList = new List <VertexPositionNormalTexture>(); List <int> objectsIndexBufferList = new List <int>(); foreach (Pedestrian ped in pedList) { Frame md = ped.Draw(); int idx = 0; foreach (VertexPositionNormalTexture vx in md.ObjectVertexList) { objectsVertexPosList.Add(vx); idx++; } int c = objectsVertexPosList.Count - idx; foreach (int ib in md.ObjectIndexList) { objectsIndexBufferList.Add(c + ib); } } Frame frame = new Frame(VertexPosList, IndexBufferList, objectsVertexPosList, objectsIndexBufferList, pos); if (debugDraw) { drawer = new DebugDrawer(); foreach (RigidBody body in _physics.RigidBodiesList) { body.DebugDraw(drawer); body.AllowDeactivation = false; body.EnableDebugDraw = true; } frame.TriangleDebug = drawer.DrawInfo.TriangleDebug; frame.LineDebug = drawer.DrawInfo.LineDebug; } return(frame); }