Пример #1
0
    // Start is called before the first frame update
    void Start()
    {
        mesh = GetComponent <MeshFilter>().mesh;

        if (!mesh)
        {
            return;
        }

        mesh.MarkDynamic();

        Vector3[] vertices = mesh.vertices;
        uvs       = mesh.uv;
        triangles = mesh.triangles;

        points           = new Point[vertices.Length];
        pointsPrevious   = new Point[vertices.Length];
        forceAccumulator = new Vector3[vertices.Length];
        thisFrameForce   = new Vector3[vertices.Length];

        constraints = new Constraint[vertices.Length * 2];
        Bounds  bounds         = mesh.bounds;
        Vector3 anchorDistance = (bounds.max - bounds.min) * 0.5f * drift;

        for (int i = 0; i < vertices.Length; i++)
        {
            points[i]              = new Point(vertices[i]);
            pointsPrevious[i]      = new Point(vertices[i]);
            forceAccumulator[i]    = new Vector3();
            thisFrameForce[i]      = new Vector3();
            constraints[i * 2]     = new BoxConstraint(points[i], bounds);
            constraints[i * 2 + 1] = new AnchorConstraint(points[i], anchorDistance);
        }
    }
Пример #2
0
Файл: Wire.cs Проект: jaoel/LD46
    private void Start()
    {
        int segmentCount = (int)(length / segmentLength) + 1;

        points = new Point[segmentCount + 1];
        for (int i = 0; i < points.Length; i++)
        {
            Vector3 position = Vector3.Lerp(transform.position, plug.transform.position, i / (float)points.Length) + Random.insideUnitSphere * 0.05f;
            points[i] = new Point(position);
            VerletPhysicsManager.AddPoint(points[i]);

            if (i == 0)
            {
                anchorA = VerletPhysicsManager.AddAnchorConstraint(points[i], transform.position + transform.forward * thickness);
            }
            else if (i == points.Length - 1)
            {
                anchorB = VerletPhysicsManager.AddAnchorConstraint(points[i], plug.transform.position + plug.transform.forward * thickness);
            }

            if (i > 0)
            {
                EdgeConstraint edgeConstraint = VerletPhysicsManager.AddEdgeConstraint(points[i], points[i - 1], segmentLength);
                if (i == points.Length - 1)
                {
                    edge = edgeConstraint;
                }
            }

            if (i > 1)
            {
                VerletPhysicsManager.AddEdgeConstraint(points[i], points[i - 2], segmentLength * 2f - 0.001f, EdgeConstraintType.MinimumDistance);
            }
        }

        tubePositions = new Vector3[points.Length + 2];
        tubeRenderer._useWorldSpace = true;
        UpdateTubePositions();
    }