Ejemplo n.º 1
0
    void GenerateNewAsteroid()
    {
        BGAsteroid asteroid;

        try
        {
            asteroid = asteroidPool.GetPooledObject().GetComponent <BGAsteroid>();
        }
        catch (System.NullReferenceException)
        {
            return;
        }

        MeshData data = CyclicPolygonGenerator.GeneratePolygon(AsteroidGenerationRNG.Instance.Next(minimumVertices, maximumVertices), semiMajorAxis * AsteroidGenerationRNG.Instance.NextFloat(0.8f, 1.25f), semiMinorAxis * AsteroidGenerationRNG.Instance.NextFloat(0.8f, 1.25f), thetaDeviation);

        asteroid.gameObject.SetActive(true);
        asteroid.SetMesh(data);
        asteroid.InitPosition();
        asteroid.SetColor(GameManager.Instance.backgroundColor);

        asteroid.InitialMove();

        asteroid.parentGenerator = this;

        currentRate = spawnRate;
    }
Ejemplo n.º 2
0
    void GenerateNewAsteroid()
    {
        Asteroid asteroid = asteroidPool.GetPooledObject().GetComponent <Asteroid>();

        MeshData data = CyclicPolygonGenerator.GeneratePolygon(AsteroidGenerationRNG.Instance.Next(minimumVertices, maximumVertices), semiMajorAxis, semiMinorAxis, thetaDeviation);

        asteroid.SetMesh(data);
        asteroid.gameObject.SetActive(true);

        asteroid.InitialMove();

        asteroid.parentGenerator = this;

        currentRate = spawnRate;
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Slices a mesh given by data into two parts, along a line that intersects lineP1 and lineP2.
    /// </summary>
    /// <param name="data">MeshData of the mesh to slice.</param>
    /// <param name="raycastDir">Direction along which to raycast from the hit point.</param>
    /// <param name="hitPoint">First point that defines the slice line.</param>
    /// <param name="meshPos">Position of the mesh to cut.</param>
    /// <param name="negCentroid">Position of the negative mesh's centroid.</param>
    /// <param name="posCentroid">Position of the positive mesh's centroid.</param>
    /// <returns>Two MeshDatas of sliced meshes. Index 0 corresponds to the left-side slice, index 1 to the right-side slice.</returns>
    public static MeshData[] Slice(MeshData data, Vector2 raycastDir, Vector2 hitPoint, Vector2 meshPos, Quaternion meshRotation, out Vector2 posCentroid, out Vector2 negCentroid)
    {
        // Find hit points
        Ray          ray      = new Ray(hitPoint, raycastDir);
        RaycastHit2D firstHit = Physics2D.Raycast(ray.origin, ray.direction, 0.5f, LayerMask.GetMask("Asteroid"));
        RaycastHit2D otherHit = Physics2D.Raycast(new Vector3(firstHit.point.x, firstHit.point.y) + ray.direction * 0.05f, ray.direction, 2f, LayerMask.GetMask("Asteroid"));

        if (firstHit.collider == null)
        {
            Debug.LogError("No firstHit collider found!");
            Debug.Log($"Bullet pos: {hitPoint.x} {hitPoint.y}");

            //Time.timeScale = 0;

            posCentroid = Vector2.zero;
            negCentroid = Vector2.zero;
            return(null);
        }

        hitPoint = firstHit.point - meshPos;

        if (otherHit.collider == null)
        {
            Debug.LogError("No slicePoint collider found!");
            Debug.Log($"First hit point: {firstHit.point.x} {firstHit.point.y}");

            //Time.timeScale = 0;

            posCentroid = Vector2.zero;
            negCentroid = Vector2.zero;
            return(null);
        }

        Vector2 slicePoint = otherHit.point - meshPos;

        MeshData positiveSliceData = new MeshData(), negativeSliceData = new MeshData();

        // Can be optimized

        List <Vector3> positiveVertices = new List <Vector3>(), negativeVertices = new List <Vector3>();

        foreach (Vector2 v in data.GetRotatedVertices(-meshRotation.eulerAngles.z))
        {
            float pointSide = GetLineSideOfPoint(hitPoint, slicePoint, v);

            if (pointSide > 0)
            {
                positiveVertices.Add(v);
            }
            if (pointSide < 0)
            {
                negativeVertices.Add(v);
            }
        }

        positiveVertices.Add(hitPoint);
        positiveVertices.Add(slicePoint);
        negativeVertices.Add(hitPoint);
        negativeVertices.Add(slicePoint);

        posCentroid = GetCentroid(positiveVertices);
        negCentroid = GetCentroid(negativeVertices);

        Vector3 pc = posCentroid, nc = negCentroid;

        for (int i = 0; i < positiveVertices.Count; i++)
        {
            positiveVertices[i] -= pc;
        }
        for (int i = 0; i < negativeVertices.Count; i++)
        {
            negativeVertices[i] -= nc;
        }

        positiveVertices = positiveVertices.OrderBy(v => Mathf.Atan2(v.y - pc.y, v.x - pc.x)).ToList();
        negativeVertices = negativeVertices.OrderBy(v => Mathf.Atan2(v.y - nc.y, v.x - nc.x)).ToList();

        posCentroid += meshPos;
        negCentroid += meshPos;

        positiveSliceData.triangles = CyclicPolygonGenerator.TriangulateConvexPolygon(positiveVertices.Count).ToArray();
        negativeSliceData.triangles = CyclicPolygonGenerator.TriangulateConvexPolygon(negativeVertices.Count).ToArray();

        positiveSliceData.vertices = positiveVertices.ToArray();
        negativeSliceData.vertices = negativeVertices.ToArray();


        return(new MeshData[2] {
            negativeSliceData, positiveSliceData
        });
    }