/// <summary> /// Creates an instance of WorldSimulation class. /// </summary> /// <param name="map">The map to use as physic base.</param> public WorldSimulation(Map.Map map) { CollisionSystem collisionSystem = new CollisionSystemSAP(); collisionSystem.UseTriangleMeshNormal = false; world = new World(collisionSystem); world.Gravity = new Jitter.LinearMath.JVector(0, 0, -10); Initialize(map); }
public bool CheckIfFilled(Map.Map map, int layer, ObstacleCollection obstacles) { Simplify(); var convexPolygons = BayazitDecomposer.ConvexPartition(this); var blockPointsDictionary = new Dictionary<Block, List<Vector2>>(); var blocks = GetAssociatedBlocks(convexPolygons, map, layer, blockPointsDictionary); var layerObstacles = CreateLayerObstacles(layer, obstacles); //ToDo: Don't call this method every time, the result does not change... return CheckLid(blocks, map, layer, layerObstacles, blockPointsDictionary); }
private static bool CheckLid(IEnumerable<Block> blocks, Map.Map map, int layer, Dictionary<int, List<IObstacle>> layerObstacles, Dictionary<Block, List<Vector2>> blockPointsDictionary) { var openBlocks = 0; foreach (var block in blocks) { if (block.Lid) continue; var blocksAbove = CheckBlocksAbove(block, map, layer, layerObstacles, blockPointsDictionary); if (!blocksAbove) openBlocks++; } return openBlocks == 0; }
public GTA2Game(string mapName, string styleName) { map = new Map.Map(mapName, styleName); map.CalcCoord(); _physics = new Physics(); _physics.Initialize(map); sprites = new Sprites(); GameObject.spriteAtlas = sprites; pedList = new List<Pedestrian>(); pedList.Add(new Pedestrian(new Vector3(65, 180, 5))); }
public GTA2Game(string mapName, string styleName) { map = new Map.Map(mapName, styleName); map.CalcCoord(); _physics = new WorldSimulation(map); sprites = new Sprites(); GameObject.spriteAtlas = sprites; pedList = new List<Pedestrian>(); Pedestrian ped = new Pedestrian(new Vector3(65, 178, 5)); pedList.Add(ped); _physics.AddPed(ped); debugDraw = false; }
public GTA2Game(string mapName, string styleName) { map = new Map.Map(mapName, styleName); map.CalcCoord(); _physics = new WorldSimulation(map); sprites = new Sprites(); GameObject.spriteAtlas = sprites; pedList = new List <Pedestrian>(); Pedestrian ped = new Pedestrian(new Vector3(65, 178, 5)); pedList.Add(ped); _physics.AddPed(ped); debugDraw = 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); }
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; }
public static IEnumerable<Block> GetAssociatedBlocks(IEnumerable<List<Vector2>> convexPolygons, Map.Map map, int layer) { var blockPointsDictionary = new Dictionary<Block, List<Vector2>>(); return GetAssociatedBlocks(convexPolygons, map, layer, blockPointsDictionary); }
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; }
public SlopeFigure(Map.Map map, int layer, LineNodeDictionary nodes) : base(map, layer, nodes) { }