public void setLine(Improbable.Vector3f startPoint, Improbable.Vector3f endPoint) { // setting the size of each step as we are walking along the line; this should be 1 meter Improbable.Vector3f incrementationVector = endPoint - startPoint; incrementationVector = incrementationVector.Normalized() * SIZE_OF_A_STEP; Improbable.Vector3f prevPoint = startPoint; int[] prevCoord = findAndSetPointInGrid(startPoint); while (!nextPointIsEndpoint(prevPoint, endPoint)) { Improbable.Vector3f currentPoint = prevPoint + incrementationVector; int[] currCoord = findAndSetPointInGrid(currentPoint); if (currCoord[0] != prevCoord[0] && currCoord[1] != prevCoord[1]) { // if diagonal, set the box lower to the diagonalization if (gridPointsAreDiagonal(prevCoord, currCoord)) { int[] higherCoordinate = findHigherGridCoordinate(prevCoord, currCoord); setGridCell(higherCoordinate[0], higherCoordinate[1] + 1, GridType.NEAR); } } prevPoint = currentPoint; prevCoord = currCoord; } findAndSetPointInGrid(endPoint); }
private bool nextPointIsEndpoint(Improbable.Vector3f currentPoint, Improbable.Vector3f endPoint) { double distanceToEndPoint = Math.Sqrt(Math.Pow(endPoint.x - currentPoint.x, 2) + Math.Pow(endPoint.z - currentPoint.z, 2)); return(distanceToEndPoint < SIZE_OF_A_STEP); }
public static EntityTemplate CreatePlayerEntityTemplate(string workerId, Improbable.Vector3f position) { var clientAttribute = $"workerId:{workerId}"; var template = new EntityTemplate(); template.AddComponent(new Position.Snapshot(), clientAttribute); template.AddComponent(new Metadata.Snapshot { EntityType = "User" }, UnityGameLogicConnector.WorkerType); template.AddComponent(new Player.PlayerInput.Snapshot(), clientAttribute); /* * template.AddComponent(new Launcher.Snapshot { EnergyLeft = 100, RechargeTimeLeft = 0 }, * UnityGameLogicConnector.WorkerType); * template.AddComponent(new Score.Snapshot(), WorkerUtils.UnityGameLogic); * template.AddComponent(new CubeSpawner.Snapshot { SpawnedCubes = new List<EntityId>() }, * UnityGameLogicConnector.WorkerType); */ template.AddComponent(new Player.PlayerAuth.Snapshot { IsAuthed = false, PlayerName = "unauthorized player" }, "GRPCManager"); TransformSynchronizationHelper.AddTransformSynchronizationComponents(template, clientAttribute); PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerId, clientAttribute, UnityGameLogicConnector.WorkerType); template.SetReadAccess(UnityClientConnector.WorkerType, UnityGameLogicConnector.WorkerType, "GRPCManager"); template.SetComponentWriteAccess(EntityAcl.ComponentId, UnityGameLogicConnector.WorkerType); return(template); }
public static EntityTemplate CreatePlayerEntityTemplate(string workerId, Improbable.Vector3f position) { var clientAttribute = $"workerId:{workerId}"; var template = new EntityTemplate(); template.AddComponent(new Position.Snapshot(), clientAttribute); template.AddComponent(new Metadata.Snapshot { EntityType = "Character" }, WorkerUtils.UnityGameLogic); template.AddComponent(new PlayerInput.Snapshot(), clientAttribute); template.AddComponent(new Launcher.Snapshot { EnergyLeft = 100, RechargeTimeLeft = 0 }, WorkerUtils.UnityGameLogic); template.AddComponent(new Score.Snapshot(), WorkerUtils.UnityGameLogic); template.AddComponent(new CubeSpawner.Snapshot { SpawnedCubes = new List <EntityId>() }, WorkerUtils.UnityGameLogic); TransformSynchronizationHelper.AddTransformSynchronizationComponents(template, clientAttribute); PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerId, clientAttribute, WorkerUtils.UnityGameLogic); template.SetReadAccess(WorkerUtils.UnityClient, WorkerUtils.UnityGameLogic); template.SetComponentWriteAccess(EntityAcl.ComponentId, WorkerUtils.UnityGameLogic); return(template); }
public void InitGlobalLayer(Improbable.Vector3f topLeft, Improbable.Vector3f bottomRight) { //Debug.LogWarning("call init bitmap " + bitmap != null); bitmap.InitialiseBitmap(topLeft, bottomRight); //Debug.LogWarning("Bitmap Ready"); bitmap.updateWithNoFlyZones(GlobalLayerWriter.Data.zones); //Debug.LogWarning("Bitmap w/NFZs Ready"); }
// Returns positive infinity if no point is found within a certain amount of layers. public float distanceToNoFlyZone(Improbable.Vector3f point) { Improbable.Vector3f p = nearestNoFlyZonePoint(point); if (p.y >= 0) { float w = p.x - point.x; float h = p.z - point.z; return(Mathf.Sqrt(Mathf.Pow(w, 2) + Mathf.Pow(h, 2))); } return(float.PositiveInfinity); }
public bool isPointInNoFlyZone(Improbable.Vector3f point) { foreach (Improbable.Controller.NoFlyZone zone in zones) { if (NoFlyZone.hasCollidedWith(zone, point)) { return(true); } } return(false); }
public Improbable.Collections.List <Improbable.Vector3f> generatePointToPointPlan(Improbable.Vector3f p1, Improbable.Vector3f p2) { if (isPointInNoFlyZone(p2)) { Debug.LogError("next target is in a NFZ"); return(null); // A plan can not be found } int[] coord1 = bitmap.findGridCoordinatesOfPoint(p1); if (coord1 == null) { Debug.LogError("coord1 fail"); return(null); } int[] coord2 = bitmap.findGridCoordinatesOfPoint(p2); if (coord1 == null) { Debug.LogError("coord2 fail"); return(null); } GridLocation l1 = new GridLocation(coord1[0], coord1[1]); GridLocation l2 = new GridLocation(coord2[0], coord2[1]); ASearch = new ThetaStarSearch(true); // Use AStarSearch or ThetaStarSearch here. float droneHeight = UnityEngine.Random.Range(SimulationSettings.MinimumDroneHeight, SimulationSettings.MaximumDroneHeight); List <GridLocation> locs = ASearch.run(bitmap, l1, l2); if (locs == null) { // The case that a path could not be found. Debug.LogError("search fail"); return(null); // Just return empty list. } //Debug.LogWarning("NUM VERTICES IN PATH: " + locs.Count); Improbable.Collections.List <Improbable.Vector3f> result = new Improbable.Collections.List <Improbable.Vector3f>(); result.Add(p1); foreach (GridLocation l in locs) { Improbable.Vector3f convertedLocation = convertLocation(l); Improbable.Vector3f location = new Improbable.Vector3f(convertedLocation.x, droneHeight, convertedLocation.z); result.Add(location); } result.Add(p2); return(result); }
public static bool isPointInTheBoundingBox(Improbable.Controller.NoFlyZone nfz, Improbable.Vector3f point) { bool res = false; if (point.x >= nfz.boundingBoxBottomLeft.x & point.x <= nfz.boundingBoxTopRight.x) { if (point.z >= nfz.boundingBoxBottomLeft.z & point.z <= nfz.boundingBoxTopRight.z) { res = true; } } return(res); }
// Returns an int[] of the form {x, y}. public int[] findGridCoordinatesOfPoint(Improbable.Vector3f point) { int x = (int)Math.Floor((point.x - TopLeft.x) / BIT_SIZE); int z = (int)Math.Floor((TopLeft.z - point.z) / BIT_SIZE); if (x < 0 || x >= GridWidth || z < 0 || z >= GridHeight) { Debug.LogError("Invalid bitmap index: x= " + x + ", z= " + z); Debug.LogError("G->P || point: " + point + " ,topLeft: " + TopLeft); return(null); } int[] result = { x, z }; return(result); }
/* * Sets the corresponding bit in the grid and returns the coordinates */ public int[] findAndSetPointInGrid(Improbable.Vector3f point) { int[] coord = findGridCoordinatesOfPoint(point); GridLocation loc = new GridLocation(coord[0], coord[1]); foreach (var n in GeneralNeighbours(loc, SimulationSettings.NFZ_PADDING)) { if (getGridCell(n.x, n.z) != GridType.IN) { setGridCell(n.x, n.z, GridType.NEAR); } } setGridCell(coord[0], coord[1], GridType.IN); return(coord); }
public static EntityTemplate CreatePlayerEntityTemplate(string workerId, Improbable.Vector3f position) { var clientAttribute = $"workerId:{workerId}"; var serverAttribute = WorkerType; var entityBuilder = EntityBuilder.Begin() .AddPosition(UnityEngine.Random.Range(-8, 8), 0, 0, serverAttribute) .AddMetadata("Player", serverAttribute) .SetPersistence(false) .SetReadAcl(AllWorkerAttributes) .SetEntityAclComponentWriteAccess(serverAttribute) .AddComponent(Player.PlayerEntity.Component.CreateSchemaComponentData(true), serverAttribute) .AddPlayerLifecycleComponents(workerId, clientAttribute, serverAttribute) .AddTransformSynchronizationComponents(clientAttribute); return(entityBuilder.Build()); }
private static bool isInPolygon(Improbable.Controller.NoFlyZone nfz, Improbable.Vector3f point) { bool isInside = false; for (int i = 0, j = nfz.vertices.Count - 1; i < nfz.vertices.Count; j = i++) { bool isInZRange = ((nfz.vertices[i].z > point.z) != (nfz.vertices[j].z > point.z)); if (isInZRange && (point.x < (nfz.vertices[j].x - nfz.vertices[i].x) * (point.z - nfz.vertices[i].z) / (nfz.vertices[j].z - nfz.vertices[i].z) + nfz.vertices[i].x)) { isInside = !isInside; } } return(isInside); }
public void addNoFlyZone(Improbable.Controller.NoFlyZone noFlyZone, bool sendUpdate = true) { Improbable.Vector3f[] vertices = noFlyZone.vertices.ToArray(); Improbable.Vector3f previousWaypoint = vertices[0]; for (int i = 1; i < vertices.Length; i++) { Improbable.Vector3f currentWaypoint = vertices[i]; setLine(previousWaypoint, currentWaypoint); previousWaypoint = currentWaypoint; } setLine(previousWaypoint, vertices[0]); // setting the final line if (sendUpdate) { sendGridUpdate(); } }
private void OnEnable() { BIT_SIZE = SimulationSettings.BIT_SIZE; // meters that each bit in the grid corresponds to SIZE_OF_A_STEP = SimulationSettings.SIZE_OF_A_STEP; // used when setting bits from a no fly zone //BitmapWriter.ComponentUpdated.Add(HandleAction); if (BitmapWriter.Data.initialised) { TopLeft = BitmapWriter.Data.topLeft; BottomRight = BitmapWriter.Data.bottomRight; Width = BitmapWriter.Data.width; Height = BitmapWriter.Data.height; GridHeight = BitmapWriter.Data.gridHeight; GridWidth = BitmapWriter.Data.gridWidth; Grid = BitmapWriter.Data.grid; } }
private static EntityTemplate CreatePlayerEntityTemplate(string workerId, Improbable.Vector3f position) { var clientAttribute = $"workerId:{workerId}"; var serverAttribute = WorkerType; var template = new EntityTemplate(); template.AddComponent(new Position.Snapshot(), clientAttribute); template.AddComponent(new Metadata.Snapshot { EntityType = "Player" }, serverAttribute); TransformSynchronizationHelper.AddTransformSynchronizationComponents(template, clientAttribute); PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerId, clientAttribute, serverAttribute); template.SetReadAccess(UnityClientConnector.WorkerType, AndroidClientWorkerConnector.WorkerType, iOSClientWorkerConnector.WorkerType, serverAttribute); template.SetComponentWriteAccess(EntityAcl.ComponentId, serverAttribute); return(template); }
public void InitialiseBitmap(Improbable.Vector3f topLeft, Improbable.Vector3f bottomRight) { //Debug.LogWarning("first if"); if (BitmapWriter.Data.initialised) { Debug.LogError("Bitmap already initialised"); return; } //Debug.LogWarning("second if"); if (topLeft.x > bottomRight.x || bottomRight.z > topLeft.z) { Debug.LogError("Unsupported grid coordinates"); return; } //Debug.LogWarning("harmless shit"); TopLeft = topLeft; BottomRight = bottomRight; Width = (int)Math.Ceiling(Math.Abs(bottomRight.x - topLeft.x)); Height = (int)Math.Ceiling(Math.Abs(topLeft.z - bottomRight.z)); //Debug.LogWarning("potential danger"); createBitmapOfGivenSize(Width, Height); //Debug.LogWarning("init || topLeft: " + TopLeft); BitmapWriter.Send(new BitmapComponent.Update() .SetTopLeft(TopLeft) .SetBottomRight(BottomRight) .SetWidth(Width) .SetHeight(Height) .SetGrid(Grid) .SetGridWidth(GridWidth) .SetGridHeight(GridHeight) .SetInitialised(true)); WaitUntil.Equals(BitmapWriter.Data.initialised, true); }
public static bool hasCollidedWith(Improbable.Controller.NoFlyZone nfz, Improbable.Vector3f point) { return(isInPolygon(nfz, point)); }
public Improbable.Vector3f nearestNoFlyZonePoint(Improbable.Vector3f point) { // Find out where point is in the grid int[] gridCo = findGridCoordinatesOfPoint(point); int x = gridCo[0]; int z = gridCo[1]; GridLocation anchor = new GridLocation(x, z); double nearestDistance = Double.PositiveInfinity; GridLocation nearestLocation = new GridLocation(0, 0); // placeholder. bool foundNoFlyZone = false; for (int layer = 1, maxLayers = 6; !foundNoFlyZone && layer <= maxLayers; ++layer) { // Placeholders. int k, xi; // Loop from top left to top right. int zi = z - layer; if (0 <= zi && zi < Height) { for (k = -layer; k < layer; ++k) { xi = x + k; if (InGridBounds(xi, zi) && getGridCell(xi, zi) == GridType.IN) { foundNoFlyZone = true; GridLocation candidate = new GridLocation(xi, zi); double dist = anchor.distanceTo(candidate); if (dist < nearestDistance) { nearestDistance = Math.Min(nearestDistance, dist); nearestLocation = candidate; } } } } // Loop from top right to bottom right. xi = x + layer; if (0 <= xi && xi < Width) { for (k = -layer; k < layer; ++k) { zi = z + k; if (InGridBounds(xi, zi) && getGridCell(xi, zi) == GridType.IN) { foundNoFlyZone = true; GridLocation candidate = new GridLocation(xi, zi); double dist = anchor.distanceTo(candidate); if (dist < nearestDistance) { nearestDistance = Math.Min(nearestDistance, dist); nearestLocation = candidate; } } } } // Loop from bottom right to bottom left. if (0 <= zi && zi < Height) { zi = z + layer; for (k = layer; k > -layer; --k) { xi = x + k; if (InGridBounds(xi, zi) && getGridCell(xi, zi) == GridType.IN) { foundNoFlyZone = true; GridLocation candidate = new GridLocation(xi, zi); double dist = anchor.distanceTo(candidate); if (dist < nearestDistance) { nearestDistance = Math.Min(nearestDistance, dist); nearestLocation = candidate; } } } } // Loop from bottom left to top left. xi = x - layer; if (0 <= xi && xi < Width) { for (k = layer; k > -layer; --k) { zi = z + k; if (InGridBounds(xi, zi) && getGridCell(xi, zi) == GridType.IN) { foundNoFlyZone = true; GridLocation candidate = new GridLocation(xi, zi); double dist = anchor.distanceTo(candidate); if (dist < nearestDistance) { nearestDistance = Math.Min(nearestDistance, dist); nearestLocation = candidate; } } } } } if (!foundNoFlyZone) { return(new Improbable.Vector3f(0, -1, 0)); } //given set point, convert grid -> real world Improbable.Vector3f nearPoint = getPointFromGridCoordinates(new int[] { nearestLocation.x, nearestLocation.z }); //assert vector.y is 0 //TODO: assert nearPoint only has 2 non-zero elements return(nearPoint); }
public Improbable.Vector3f GetClosestVector3fOnGrid(Improbable.Vector3f original) { return(bitmap.getPointFromGridCoordinates(bitmap.findGridCoordinatesOfPoint(original))); }
public double distanceToNoFlyZone(Improbable.Vector3f point) { return(bitmap.distanceToNoFlyZone(point)); }
/** * 'point' has components in cartesian format */ public bool isNoFlyZone(Improbable.Vector3f point) { int[] coord = findGridCoordinatesOfPoint(point); return(isGridCellNoFlyZone(coord[0], coord[1])); }