Example #1
0
        public override bool BuildSoftBody()
        {
            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);
            }

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

            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, anchor.body.GetRigidBody());
                }
                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()
        {
            Mesh mesh = meshSettings.Build();

            GetComponent <MeshFilter>().sharedMesh = mesh;

            //convert the mesh data to Bullet data and create DoftBody
            BulletSharp.Math.Vector3[] bVerts = new BulletSharp.Math.Vector3[mesh.vertexCount];

            for (int i = 0; i < mesh.vertexCount; i++)
            {
                bVerts[i] = mesh.vertices[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(transform.rotation.ToBullet());
            m_BSoftBody.Translate(transform.position.ToBullet());
            m_BSoftBody.Scale(transform.localScale.ToBullet());

            return(true);
        }
        internal override bool _BuildCollisionObject()
        {
            //Mesh mesh = new Mesh();

            //GetComponent<MeshFilter>().sharedMesh = mesh;

            //convert the mesh data to Bullet data and create DoftBody
            BulletSharp.Math.Vector3[] bVerts = new BulletSharp.Math.Vector3[actuallyverts.Count];
            for (int i = 0; i < actuallyverts.Count; i++)
            {
                bVerts[i] = actuallyverts[i].ToBullet();
            }

            SoftBody m_BSoftBody = CreateVolumetricSoftbody(World.WorldInfo, bVerts);

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

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



            return(true);
        }
        internal override bool _BuildCollisionObject()
        {
            Mesh mesh = meshSettings.Build();

            if (mesh == null)
            {
                Debug.LogError("Could not build mesh from meshSettings for " + this, transform);
                return(false);
            }

            GetComponent <MeshFilter>().sharedMesh = mesh;

                        #if UNITY_EDITOR
            if (!UnityEditor.EditorApplication.isPlaying)
            {
                return(false);
            }
                        #endif

            if (World == null)
            {
                return(false);
            }

            //convert the mesh data to Bullet data and create SoftBody
            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        = SoftBodyHelpers.CreateFromTriMesh(World.WorldInfo, bVerts, mesh.triangles);
            collisionObject = softBody;
            SoftBodySettings.ConfigureSoftBody(softBody);//Set SB settings

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

            return(true);
        }
        internal override bool _BuildCollisionObject()
        {
            if (World == null)
            {
                return(false);
            }

            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 = SoftBodyHelpers.CreateRope
                       (
                World.WorldInfo,
                meshSettings.startPoint.ToBullet(),
                meshSettings.endPoint.ToBullet(),
                meshSettings.numPointsInRope,
                0
                       );

            collisionObject = softBody;

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

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

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

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

                if (anchor.body != null)
                {
                    softBody.AppendAnchor(node, (BulletSharp.RigidBody)anchor.body.GetCollisionObject());
                }
                else
                {
                    softBody.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.positionCount = verts.Length;
            lr.startWidth    = meshSettings.width;
            lr.endWidth      = meshSettings.width;
            lr.startColor    = meshSettings.startColor;
            lr.endColor      = meshSettings.endColor;

            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);
    }