Beispiel #1
0
        void Init_TetraBunny()
        {
            SoftBody psb = SoftBodyHelpers.CreateFromTetGenData(softBodyWorldInfo,
                                                                Bunny.GetElements(), null, Bunny.GetNodes(), false, true, true);

            SoftWorld.AddSoftBody(psb);
            psb.Rotate(Quaternion.RotationYawPitchRoll((float)Math.PI / 2, 0, 0));
            psb.SetVolumeMass(150);
            psb.Cfg.PIterations = 2;
            //psb.Cfg.PIterations = 1;
            cutting = false;
            //psb.CollisionShape.Margin = 0.01f;
            psb.Cfg.Collisions = FCollisions.CLSS | FCollisions.CLRS; //| FCollisions.CLSelf;

            ///pass zero in generateClusters to create  cluster for each tetrahedron or triangle
            psb.GenerateClusters(0);
            //psb.Materials[0].Lst = 0.2f;
            psb.Cfg.DF = 10;
        }
Beispiel #2
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;
        }
Beispiel #3
0
        void InitBunny()
        {
            SoftBody psb = SoftBodyHelpers.CreateFromTriMesh(softBodyWorldInfo, BunnyMesh.Vertices, BunnyMesh.Indices);
            Material pm  = psb.AppendMaterial();

            pm.Lst    = 0.5f;
            pm.Flags -= FMaterial.DebugDraw;
            psb.GenerateBendingConstraints(2, pm);
            psb.Cfg.PIterations = 2;
            psb.Cfg.DF          = 0.5f;
            psb.RandomizeConstraints();
            Matrix m = Matrix.RotationYawPitchRoll(0, (float)Math.PI / 2, 0) *
                       Matrix.Translation(0, 4, 0);

            psb.Transform(m);
            psb.Scale(new Vector3(6, 6, 6));
            psb.SetTotalMass(100, true);
            SoftWorld.AddSoftBody(psb);
            cutting = true;
        }
Beispiel #4
0
        SoftBody CreateClusterTorus(Vector3 x, Vector3 a, Vector3 s)
        {
            SoftBody psb = SoftBodyHelpers.CreateFromTriMesh(softBodyWorldInfo, TorusMesh.Vertices, TorusMesh.Indices);
            Material pm  = psb.AppendMaterial();

            pm.Lst    = 1;
            pm.Flags -= FMaterial.DebugDraw;
            psb.GenerateBendingConstraints(2, pm);
            psb.Cfg.PIterations = 2;
            psb.Cfg.Collisions  = FCollisions.CLSS | FCollisions.CLRS;
            psb.RandomizeConstraints();
            psb.Scale(s);
            Matrix m = Matrix.RotationYawPitchRoll(a.X, a.Y, a.Z) * Matrix.Translation(x);

            psb.Transform(m);
            psb.SetTotalMass(50, true);
            psb.GenerateClusters(64);
            SoftWorld.AddSoftBody(psb);
            return(psb);
        }
Beispiel #5
0
        void InitCutting1()
        {
            const float s = 6;
            const float h = 2;
            const int   r = 16;

            Vector3[] p =
            {
                new Vector3(+s, h, -s),
                new Vector3(-s, h, -s),
                new Vector3(+s, h, +s),
                new Vector3(-s, h, +s)
            };
            SoftBody psb = SoftBodyHelpers.CreatePatch(softBodyWorldInfo, p[0], p[1], p[2], p[3], r, r, 1 + 2 + 4 + 8, true);

            SoftWorld.AddSoftBody(psb);
            psb.Cfg.PIterations = 1;
            cutting             = true;

            CullingEnabled = false;
        }
Beispiel #6
0
        void InitCapsuleCollision()
        {
            float s = 4;
            float h = 6;
            int   r = 20;

            Matrix startTransform = Matrix.Translation(0, h - 2, 0);

            CollisionShape capsuleShape = new CapsuleShapeX(1, 5);

            capsuleShape.Margin = 0.5f;

            //capsuleShape.LocalScaling = new Vector3(5, 1, 1);
            //RigidBody body = LocalCreateRigidBody(20, startTransform, capsuleShape);
            RigidBody body = LocalCreateRigidBody(0, startTransform, capsuleShape);

            body.Friction = 0.8f;

            const int fixeds = 0; //4+8;
            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, fixeds, true);

            SoftWorld.AddSoftBody(psb);
            psb.TotalMass = 0.1f;

            psb.Cfg.PIterations = 10;
            psb.Cfg.CIterations = 10;
            psb.Cfg.DIterations = 10;
            //psb.Cfg.VIterations = 10;


            //psb.AppendAnchor(0, body);
            //psb.AppendAnchor(r-1, body);
            //cutting = true;

            CullingEnabled = false;
        }
        SoftBody Create_SoftBox(Vector3 p, Vector3 s)
        {
            Vector3 h = s * 0.5f;

            Vector3[] c = new Vector3[] {
                h *new Vector3(-1, -1, -1),
                h *new Vector3(+1, -1, -1),
                h *new Vector3(-1, +1, -1),
                h *new Vector3(+1, +1, -1),
                h *new Vector3(-1, -1, +1),
                h *new Vector3(+1, -1, +1),
                h *new Vector3(-1, +1, +1),
                h *new Vector3(+1, +1, +1)
            };
            SoftBody psb = SoftBodyHelpers.CreateFromConvexHull(softBodyWorldInfo, c);

            psb.GenerateBendingConstraints(2);
            psb.Translate(p);
            SoftWorld.AddSoftBody(psb);

            return(psb);
        }
        public void SoftBodyDebugDrawHelpersTest()
        {
            var softBodyWorldInfo = new SoftBodyWorldInfo();

            using (var debugDrawer = new DebugDrawer())
            {
                using (var softBody = SoftBodyHelpers.CreateEllipsoid(softBodyWorldInfo, Vector3.Zero, new Vector3(1), 4))
                {
                    SoftBodyHelpers.DrawInfos(softBody, debugDrawer, true, true, true);
                    Assert.That(debugDrawer.Draw3DTextCalled, Is.True);

                    SoftBodyHelpers.Draw(softBody, debugDrawer, SoftDrawFlags);
                    SoftBodyHelpers.DrawClusterTree(softBody, debugDrawer);
                    SoftBodyHelpers.DrawFaceTree(softBody, debugDrawer);
                    SoftBodyHelpers.DrawFrame(softBody, debugDrawer);
                    SoftBodyHelpers.DrawNodeTree(softBody, debugDrawer);
                }
                Assert.That(debugDrawer.DrawLineCalled, Is.True);
            }

            softBodyWorldInfo.Dispose();
        }
Beispiel #9
0
        void Init_Cloth()
        {
            float    s   = 8;
            SoftBody psb = SoftBodyHelpers.CreatePatch(softBodyWorldInfo, new Vector3(-s, 0, -s),
                                                       new Vector3(+s, 0, -s),
                                                       new Vector3(-s, 0, +s),
                                                       new Vector3(+s, 0, +s),
                                                       31, 31,
                                                       1 + 2 + 4 + 8, true);

            psb.CollisionShape.Margin = 0.5f;
            Material pm = psb.AppendMaterial();

            pm.Lst    = 0.4f;
            pm.Flags -= FMaterial.DebugDraw;
            psb.GenerateBendingConstraints(2, pm);
            psb.TotalMass = 150;
            SoftWorld.AddSoftBody(psb);

            Create_RbUpStack(10);
            cutting = true;
        }
Beispiel #10
0
        void InitCollide3()
        {
            float    s   = 8;
            SoftBody psb = SoftBodyHelpers.CreatePatch(softBodyWorldInfo, new Vector3(-s, 0, -s),
                                                       new Vector3(+s, 0, -s),
                                                       new Vector3(-s, 0, +s),
                                                       new Vector3(+s, 0, +s),
                                                       15, 15, 1 + 2 + 4 + 8, true);

            psb.Materials[0].Lst = 0.4f;
            psb.Cfg.Collisions  |= FCollisions.VFSS;
            psb.TotalMass        = 150;
            SoftWorld.AddSoftBody(psb);

            s = 4;
            Vector3 o = new Vector3(5, 10, 0);

            psb = SoftBodyHelpers.CreatePatch(softBodyWorldInfo,
                                              new Vector3(-s, 0, -s) + o,
                                              new Vector3(+s, 0, -s) + o,
                                              new Vector3(-s, 0, +s) + o,
                                              new Vector3(+s, 0, +s) + o,
                                              7, 7, 0, true);
            Material pm = psb.AppendMaterial();

            pm.Lst    = 0.1f;
            pm.Flags -= FMaterial.DebugDraw;
            psb.GenerateBendingConstraints(2, pm);
            psb.Materials[0].Lst = 0.5f;
            psb.Cfg.Collisions  |= FCollisions.VFSS;
            psb.TotalMass        = 150;
            SoftWorld.AddSoftBody(psb);
            cutting = true;

            CullingEnabled = false;
        }
        internal override bool _BuildCollisionObject()
        {
            Mesh mesh = meshSettings.Build();

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

            GetComponent <MeshFilter>().sharedMesh = mesh;

            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 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);
        }
Beispiel #12
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);
    }