public static void loadGrid(GameObject[] sih) { grids = new List <ShapeGrid>(); foreach (GameObject shape in sih) { Debug.Log("Object: " + shape.name); double x = shape.transform.position.x; double y = shape.transform.position.y; double angle = shape.transform.rotation.eulerAngles.z; Debug.Log("Coord: " + x + "," + y); Debug.Log("Angle: " + angle); int index = int.Parse(shape.name); ShapeGrid[] shapeGrids = ShapeGrid.getShapesGrid()[index]; foreach (ShapeGrid grid in shapeGrids) { ShapeGrid gridToAdd = getGridByOffset(grid.x, grid.y, x, y, angle); if (!isGridExist(gridToAdd)) { grids.Add(gridToAdd); Debug.Log("Coord of grid point: " + gridToAdd.x + "," + gridToAdd.y); } } } }
public void AutoFit() { //prepare grids List <ShapeGrid> grids = GridManager.getGrid(); List <ShapeGrid> draggedGrids = new List <ShapeGrid>(); List <ShapeGrid> toMatchGrids = new List <ShapeGrid>(); //get the dragged obj's grids int index = int.Parse(draggedObject.name); ShapeGrid[] shapeGrids = ShapeGrid.getShapesGrid()[index]; double x = draggedObject.transform.position.x; double y = draggedObject.transform.position.y; double angle = draggedObject.transform.rotation.eulerAngles.z; Debug.Log("Angle: " + angle); bool allGridPointHaveCandidate = true; double candidateDistanceTreshold = AppConstant.AUTOFIT_DISTANCE_THRESHOLD; //find candidate grid points foreach (ShapeGrid grid in shapeGrids) { bool match = false; ShapeGrid gridToAdd = GridManager.getGridByOffset(grid.x, grid.y, x, y, angle); Debug.Log("Coord: " + gridToAdd.x + "," + gridToAdd.y); draggedGrids.Add(gridToAdd); foreach (ShapeGrid resultGrid in grids) { double distance = Util.getDistance(gridToAdd.x, gridToAdd.y, resultGrid.x, resultGrid.y); if (distance < candidateDistanceTreshold) { toMatchGrids.Add(resultGrid); match = true; break; } } if (!match) { allGridPointHaveCandidate = false; Debug.Log("No Auto Match"); break; } } //match the position and direction of the matched grid points if (allGridPointHaveCandidate) { float draggedGrids_x1 = (float)draggedGrids[0].x; float draggedGrids_y1 = (float)draggedGrids[0].y; float draggedGrids_x2 = (float)draggedGrids[1].x; float draggedGrids_y2 = (float)draggedGrids[1].y; float toMatchGrids_x1 = (float)toMatchGrids[0].x; float toMatchGrids_y1 = (float)toMatchGrids[0].y; float toMatchGrids_x2 = (float)toMatchGrids[1].x; float toMatchGrids_y2 = (float)toMatchGrids[1].y; //auto rotation Vector2 vFrom = new Vector2((draggedGrids_x2 - draggedGrids_x1), (draggedGrids_y2 - draggedGrids_y1)); Vector2 vTo = new Vector2((toMatchGrids_x2 - toMatchGrids_x1), (toMatchGrids_y2 - toMatchGrids_y1)); float angleDifferent = Vector2.Angle(vFrom, vTo); Vector3 cross = Vector3.Cross(vFrom, vTo); if (cross.z > 0) { angleDifferent = 360 - angleDifferent; } Debug.Log("Angle Diff:" + angleDifferent); Quaternion rotation = Quaternion.Euler(draggedObject.transform.rotation.eulerAngles.x, draggedObject.transform.rotation.eulerAngles.y, draggedObject.transform.rotation.eulerAngles.z - angleDifferent); draggedObject.transform.rotation = rotation; //auto position ShapeGrid newDraggedGrid = GridManager.getGridByOffset(shapeGrids[0].x, shapeGrids[0].y, draggedObject.transform.position.x, draggedObject.transform.position.y, draggedObject.transform.rotation.eulerAngles.z); float newDraggedGrid_x = (float)newDraggedGrid.x; float newDraggedGrid_y = (float)newDraggedGrid.y; float xDifferent = toMatchGrids_x1 - newDraggedGrid_x; float yDifferent = toMatchGrids_y1 - newDraggedGrid_y; Vector3 position = new Vector3(draggedObject.transform.position.x + xDifferent, draggedObject.transform.position.y + yDifferent, draggedObject.transform.position.z); draggedObject.transform.position = position; } }