Exemple #1
0
        void Init_ClothAttach()
        {
            float    s   = 4;
            float    h   = 6;
            int      r   = 9;
            SoftBody psb = SoftBodyHelpers.CreatePatch(softBodyWorldInfo, new Vector3(-s, h, -s),
                                                       new Vector3(+s, h, -s),
                                                       new Vector3(-s, h, +s),
                                                       new Vector3(+s, h, +s), r, r, 4 + 8, true);

            SoftWorld.AddSoftBody(psb);

            RigidBody body = LocalCreateRigidBody(20, Matrix.Translation(0, h, -(s + 3.5f)), new BoxShape(s, 1, 3));

            psb.AppendAnchor(0, body);
            psb.AppendAnchor(r - 1, body);
            body.UserObject = "LargeBox";
            cutting         = true;
        }
Exemple #2
0
        void InitRopeAttach()
        {
            softBodyWorldInfo.SparseSdf.RemoveReferences(null);
            RigidBody body = LocalCreateRigidBody(50, Matrix.Translation(12, 8, 0), new BoxShape(2, 6, 2));
            SoftBody  psb0 = CreateRope(new Vector3(0, 8, -1));
            SoftBody  psb1 = CreateRope(new Vector3(0, 8, +1));

            psb0.AppendAnchor(psb0.Nodes.Count - 1, body);
            psb1.AppendAnchor(psb1.Nodes.Count - 1, body);
        }
Exemple #3
0
        internal override bool _BuildCollisionObject()
        {
            if (meshSettings.numPointsInRope < 2)
            {
                Debug.LogError("There must be at least two points in the rope");
                return(false);
            }
            if (SoftBodySettings.totalMass <= 0f)
            {
                Debug.LogError("The rope must have a positive mass");
                return(false);
            }

            SoftBody m_BSoftBody = SoftBodyHelpers.CreateRope(World.WorldInfo,
                                                              meshSettings.startPoint.ToBullet(), meshSettings.endPoint.ToBullet(), meshSettings.numPointsInRope, 0);

            m_collisionObject = m_BSoftBody;

            verts = new Vector3[m_BSoftBody.Nodes.Count];
            norms = new Vector3[m_BSoftBody.Nodes.Count];

            for (int i = 0; i < m_BSoftBody.Nodes.Count; i++)
            {
                verts[i] = m_BSoftBody.Nodes[i].Position.ToUnity();
                norms[i] = m_BSoftBody.Nodes[i].Normal.ToUnity();
            }

            //Set SB settings
            SoftBodySettings.ConfigureSoftBody(m_BSoftBody);

            foreach (RopeAnchor anchor in ropeAnchors)
            {
                //anchorNode point 0 to 1, rounds to node #
                int node = (int)Mathf.Floor(Mathf.Lerp(0, m_BSoftBody.Nodes.Count - 1, anchor.anchorNodePoint));

                if (anchor.body != null)
                {
                    m_BSoftBody.AppendAnchor(node, (BulletSharp.RigidBody)anchor.body.GetCollisionObject());
                }
                else
                {
                    m_BSoftBody.SetMass(node, 0);  //setting node mass to 0 fixes it in space apparently
                }
            }

            //TODO: lr, Doesnt always work in editor
            LineRenderer lr = GetComponent <LineRenderer>();

            lr.useWorldSpace = false;

            lr.SetVertexCount(verts.Length);
            lr.SetWidth(meshSettings.width, meshSettings.width);
            lr.SetColors(meshSettings.startColor, meshSettings.endColor);

            //Set SB position to GO position
            //m_BSoftBody.Rotate(transform.rotation.ToBullet());
            //m_BSoftBody.Translate(transform.position.ToBullet());
            //m_BSoftBody.Scale(transform.localScale.ToBullet());

            UpdateMesh();
            return(true);
        }
    internal override bool _BuildCollisionObject()
    {
        if (World == null)
        {
            return(false);
        }
        if (transform.localScale != Vector3.one)
        {
            Debug.LogError("The scale must be 1,1,1");
        }
        if (bone2idxMap == null || bone2idxMap.Length == 0)
        {
            Debug.LogError("No bones have been mapped to soft body nodes for object " + name);
        }
        for (int i = 0; i < anchors.Length; i++)
        {
            if (anchors[i].anchorRigidBody == null)
            {
                Debug.LogError("No anchor rigid body has been set for anchor " + i);
            }
            if (anchors[i].anchorNodeIndexes == null || anchors[i].anchorNodeIndexes.Count == 0)
            {
                Debug.LogError("No nodes have been identified as anchors. Soft body will not be attached to RigidBody anchor " + anchors[i].anchorRigidBody);
            }
        }

        if (physicsSimMesh == null)
        {
            physicsSimMesh = GetComponent <MeshFilter>();
        }
        Mesh mesh = physicsSimMesh.sharedMesh;

        //convert the mesh data to Bullet data and create DoftBody
        //todo should these be in world coordinates
        BulletSharp.Math.Vector3[] bVerts = new BulletSharp.Math.Vector3[mesh.vertexCount];
        Vector3[] verts = mesh.vertices;
        for (int i = 0; i < mesh.vertexCount; i++)
        {
            bVerts[i] = verts[i].ToBullet();
        }

        SoftBody m_BSoftBody = SoftBodyHelpers.CreateFromTriMesh(World.WorldInfo, bVerts, mesh.triangles);

        m_collisionObject = m_BSoftBody;
        SoftBodySettings.ConfigureSoftBody(m_BSoftBody);         //Set SB settings

        //Set SB position to GO position
        m_BSoftBody.Rotate(physicsSimMesh.transform.rotation.ToBullet());
        m_BSoftBody.Translate(physicsSimMesh.transform.position.ToBullet());
        m_BSoftBody.Scale(physicsSimMesh.transform.localScale.ToBullet());

        for (int i = 0; i < anchors.Length; i++)
        {
            BAnchor a = anchors[i];
            for (int j = 0; j < a.anchorNodeIndexes.Count; j++)
            {
                m_BSoftBody.AppendAnchor(a.anchorNodeIndexes[j], (RigidBody)a.anchorRigidBody.GetCollisionObject(), false, a.anchorNodeStrength[j]);
            }
        }

        MeshRenderer mr = physicsSimMesh.GetComponent <MeshRenderer>();

        if (mr != null)
        {
            if (debugDisplaySimulatedMesh)
            {
                mr.enabled = true;
            }
            else
            {
                mr.enabled = false;
            }
        }

        if (norms.Length == 0 || norms.Length != verts.Length)
        {
            norms = new Vector3[m_BSoftBody.Nodes.Count];
            verts = new Vector3[m_BSoftBody.Nodes.Count];
        }
        for (int i = 0; i < m_BSoftBody.Nodes.Count; i++)
        {
            norms[i] = m_BSoftBody.Nodes[i].Normal.ToUnity();
            verts[i] = m_BSoftBody.Nodes[i].Position.ToUnity();
        }
        for (int i = 0; i < bone2idxMap.Length; i++)
        {
            bone2idxMap[i].bindNormal       = norms[bone2idxMap[i].nodeIdx];
            bone2idxMap[i].bindBoneRotation = bone2idxMap[i].bone.rotation;

            for (int j = 0; j < bone2idxMap[i].edges.Length; j++)
            {
                bone2idxMap[i].edges[j].bindEdgeXnorm = Vector3.Cross(verts[bone2idxMap[i].edges[j].nodeIdx] - verts[bone2idxMap[i].nodeIdx], norms[bone2idxMap[i].nodeIdx]).normalized;
            }
        }

        return(true);
    }