예제 #1
0
    void InitializeVertices()
    {
        _nVertices = new NativeArray <VerletVertex>(VertexCount, Allocator.Persistent);

        // add each attachment at its coordinates
        foreach (var atch in Attatchments)
        {
            int index = (atch.Coord.y * (Width + 1)) + atch.Coord.x;

            var type = (atch.Rbody == null) ? VertexType.Transform : VertexType.Rbody;
            var pos  = (type == VertexType.Rbody) ?
                       atch.Rbody.position : atch.Transform.position;
            _nVertices[index] = new VerletVertex()
            {
                Type   = type,
                Pos    = pos,
                OldPos = pos
            };
        }

        for (int i = 0; i < VertexCount; i++)
        {
            // if this has been set as an attachment, skip
            if (_nVertices[i].Type != VertexType.Verlet)
            {
                continue;
            }

            int row = i / (Width + 1);
            int col = i % (Width + 1);

            _nVertices[i] = new VerletVertex()
            {
                Type   = VertexType.Verlet,
                Pos    = transform.position /*+ new Vector3(col * SegmentLength, -row * SegmentLength, 0)*/,
                OldPos = transform.position
            };
        }

        //for (int i = 0; i < Width + 1; i++)
        //{
        //    bool skip = false;
        //    foreach (var atch in Attatchments)
        //    {
        //        // setup an attachment vertex
        //        if (atch.VertexIndex == i)
        //        {
        //            var type = (atch.Rbody == null) ? VertexType.Transform : VertexType.Rbody;
        //            var pos = (type == VertexType.Rbody) ?
        //                atch.Rbody.position: atch.Transform.position ;
        //            _nVertices[i] = new VerletVertex()
        //            {
        //                Type = type,
        //                Pos = pos,
        //                OldPos = pos
        //            };
        //            skip = true;
        //            break;
        //        }
        //    }

        //    // setup a normal verlet vertex
        //    if (!skip)
        //    {
        //        _nVertices[i] = new VerletVertex()
        //        {
        //            Type = VertexType.Verlet,
        //            Pos = transform.position,
        //            OldPos = transform.position
        //        };
        //    }
        //}
    }
예제 #2
0
    public void Execute(int i)
    {
        VerletVertex vertex = Vertices[i];
        // to describe this vertex's movement, we build a vector
        // by adding each of its adjacent constraint deltas together
        Vec3 changeVect = Vec3.zero;

        int row = i / (Width + 1);
        int col = i % (Width + 1);

        // if this is a transform, we're not moving it
        if (vertex.Type == VertexType.Transform)
        {
            return;
        }

        // is there a vert to LEFT?
        if (col > 0)
        {
            var leftVert = VerticesRO[i - 1];
            // constraint index
            int  conIndex   = i - (row + 1);
            Vec3 constraint = HorzConstraints[conIndex];



            if (leftVert.Type != VertexType.Transform)
            {
                changeVect -= constraint / 2;
            }
            else
            {
                changeVect -= constraint;
            }
        }

        // is there a vert to RIGHT?
        if (col < Width)
        {
            var rightVert = VerticesRO[i + 1];
            // constraint index
            int  conIndex   = i - row;
            Vec3 constraint = HorzConstraints[conIndex];

            if (rightVert.Type != VertexType.Transform)
            {
                changeVect += constraint / 2;
            }
            else
            {
                changeVect += constraint;
            }
        }

        // is there a vert to TOP?
        if (row > 0)
        {
            var topVert = VerticesRO[i - (Width + 1)];
            // constraint index
            int  conIndex   = i - (Width + 1);
            Vec3 constraint = VertConstraints[conIndex];

            if (topVert.Type != VertexType.Transform)
            {
                changeVect -= constraint / 2;
            }
            else
            {
                changeVect -= constraint;
            }
        }

        // is there a vert to BOTTOM?
        if (row < Height)
        {
            var bottomVert = VerticesRO[i + (Width + 1)];
            // constraint index
            int  conIndex   = i;
            Vec3 constraint = VertConstraints[conIndex];

            if (bottomVert.Type != VertexType.Transform)
            {
                changeVect += constraint / 2;
            }
            else
            {
                changeVect += constraint;
            }
        }

        vertex.Pos += changeVect;
        Vertices[i] = vertex;
    }