コード例 #1
0
ファイル: SoftDemo.cs プロジェクト: superowner/BulletSharp
 void InitClusterCollide2()
 {
     for (int i = 0; i < 3; ++i)
     {
         SoftBody psb = SoftBodyHelpers.CreateFromTriMesh(softBodyWorldInfo, TorusMesh.Vertices, TorusMesh.Indices);
         Material pm  = psb.AppendMaterial();
         pm.Flags -= FMaterial.DebugDraw;
         psb.GenerateBendingConstraints(2, pm);
         psb.Cfg.PIterations = 2;
         psb.Cfg.DF          = 1;
         psb.Cfg.SshrCl      = 1;
         psb.Cfg.SSSplitCl   = 0;
         psb.Cfg.SkhrCl      = 0.1f;
         psb.Cfg.SKSplitCl   = 1;
         psb.Cfg.Collisions  = FCollisions.CLSS | FCollisions.CLRS;
         psb.RandomizeConstraints();
         Matrix m = Matrix.RotationYawPitchRoll((float)Math.PI / 2 * (i & 1), (float)Math.PI / 2 * (1 - (i & 1)), 0)
                    * Matrix.Translation(3 * i, 2, 0);
         psb.Transform(m);
         psb.Scale(new Vector3(2, 2, 2));
         psb.SetTotalMass(50, true);
         psb.GenerateClusters(16);
         SoftWorld.AddSoftBody(psb);
     }
 }
コード例 #2
0
        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);
        }
コード例 #3
0
ファイル: SoftDemo.cs プロジェクト: superowner/BulletSharp
        void InitTetraCube()
        {
            string   path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            SoftBody psb  = SoftBodyHelpers.CreateFromTetGenFile(softBodyWorldInfo,
                                                                 path + "\\data\\cube.ele", null, path + "\\data\\cube.node", false, true, true);

            SoftWorld.AddSoftBody(psb);
            psb.Scale(new Vector3(4, 4, 4));
            psb.Translate(0, 5, 0);
            psb.SetVolumeMass(300);

            // fix one vertex
            //psb.SetMass(0,0);
            //psb.SetMass(10,0);
            //psb.SetMass(20,0);
            psb.Cfg.PIterations = 1;
            //psb.GenerateClusters(128);
            psb.GenerateClusters(16);
            //psb.CollisionShape.Margin = 0.5f;

            psb.CollisionShape.Margin = 0.01f;
            psb.Cfg.Collisions        = FCollisions.CLSS | FCollisions.CLRS;
            // | FCollisions.CLSelf;
            psb.Materials[0].Lst = 0.8f;
            cutting = false;

            CullingEnabled = false;
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        public void Evaluate(int SpreadMax)
        {
            if (this.FWorld.PluginIO.IsConnected && this.FShapes.PluginIO.IsConnected)
            {
                List <SoftBody> bodies = new List <SoftBody>();
                for (int i = 0; i < SpreadMax; i++)
                {
                    if (FDoCreate[i])
                    {
                        AbstractSoftShapeDefinition shapedef = this.FShapes[i];

                        SoftBody body = shapedef.GetSoftBody(this.FWorld[0].WorldInfo);

                        body.Translate(this.FPosition[i].ToBulletVector());
                        body.Scale(this.FScale[i].ToBulletVector());
                        body.Rotate(this.FRotate[i].ToBulletQuaternion());

                        body.Friction    = this.FFriction[i];
                        body.Restitution = this.FRestitution[i];

                        SoftBodyCustomData bd = new SoftBodyCustomData();
                        bd.Id           = this.FWorld[0].GetNewBodyId();
                        bd.Custom       = this.FCustom[i];
                        bd.HasUV        = shapedef.HasUV;
                        bd.UV           = shapedef.GetUV(body);
                        body.UserObject = bd;


                        if (this.FCustomObj.PluginIO.IsConnected)
                        {
                            bd.CustomObject = (ICloneable)this.FCustomObj[i].Clone();
                        }
                        else
                        {
                            bd.CustomObject = null;
                        }



                        this.FWorld[0].Register(body);
                        bodies.Add(body);
                    }
                }

                this.FOutBodies.SliceCount = bodies.Count;
                for (int i = 0; i < bodies.Count; i++)
                {
                    this.FOutBodies[i] = bodies[i];
                }
            }
            else
            {
                this.FOutBodies.SliceCount = 0;
            }
        }
コード例 #6
0
ファイル: SoftDemo.cs プロジェクト: superowner/BulletSharp
        void InitBunnyMatch()
        {
            SoftBody psb = SoftBodyHelpers.CreateFromTriMesh(softBodyWorldInfo, BunnyMesh.Vertices, BunnyMesh.Indices);

            psb.Cfg.DF          = 0.5f;
            psb.Cfg.MT          = 0.05f;
            psb.Cfg.PIterations = 5;
            psb.RandomizeConstraints();
            psb.Scale(new Vector3(6, 6, 6));
            psb.SetTotalMass(100, true);
            psb.SetPose(false, true);
            SoftWorld.AddSoftBody(psb);
        }
コード例 #7
0
        public void Evaluate(int SpreadMax)
        {
            if (this.FWorld.PluginIO.IsConnected && this.FShapes.PluginIO.IsConnected)
            {
                List <SoftBody> bodies = new List <SoftBody>();
                for (int i = 0; i < SpreadMax; i++)
                {
                    if (FDoCreate[i])
                    {
                        AbstractSoftShapeDefinition shapedef = this.FShapes[i];

                        SoftBody      body = shapedef.GetSoftBody(this.FWorld[0].WorldInfo);
                        RigidBodyPose pose = this.initialPoseInput.IsConnected ? this.initialPoseInput[i] : RigidBodyPose.Default;

                        body.Translate(pose.Position);
                        body.Scale(this.FScale[i].ToBulletVector());
                        body.Rotate(pose.Orientation);
                        body.Friction    = this.FFriction[i];
                        body.Restitution = this.FRestitution[i];

                        SoftBodyCustomData bd = new SoftBodyCustomData(this.FWorld[0].GetNewSoftBodyId());
                        bd.Custom       = this.FCustom[i];
                        bd.HasUV        = shapedef.HasUV;
                        bd.UV           = shapedef.GetUV(body);
                        bd.Definition   = shapedef;
                        body.UserObject = bd;


                        this.FWorld[0].Register(body);
                        bodies.Add(body);

                        //Attach if to all nodes
                        for (int j = 0; j < body.Nodes.Count; j++)
                        {
                            body.Nodes[j].Tag = (IntPtr)j;
                        }
                    }
                }

                this.FOutBodies.SliceCount = bodies.Count;
                for (int i = 0; i < bodies.Count; i++)
                {
                    this.FOutBodies[i] = bodies[i];
                }
            }
            else
            {
                this.FOutBodies.SliceCount = 0;
            }
        }
コード例 #8
0
ファイル: SoftDemo.cs プロジェクト: superowner/BulletSharp
        void InitTorusMatch()
        {
            SoftBody psb = SoftBodyHelpers.CreateFromTriMesh(softBodyWorldInfo, TorusMesh.Vertices, TorusMesh.Indices);

            psb.Materials[0].Lst = 0.1f;
            psb.Cfg.MT           = 0.05f;
            psb.RandomizeConstraints();
            Matrix m = Matrix.RotationYawPitchRoll(0, (float)Math.PI / 2, 0) *
                       Matrix.Translation(0, 4, 0);

            psb.Transform(m);
            psb.Scale(new Vector3(2, 2, 2));
            psb.SetTotalMass(50, true);
            psb.SetPose(false, true);
            SoftWorld.AddSoftBody(psb);
        }
コード例 #9
0
ファイル: SoftDemo.cs プロジェクト: superowner/BulletSharp
        void InitTorus()
        {
            SoftBody psb = SoftBodyHelpers.CreateFromTriMesh(softBodyWorldInfo, TorusMesh.Vertices, TorusMesh.Indices);

            psb.GenerateBendingConstraints(2);
            psb.Cfg.PIterations = 2;
            psb.RandomizeConstraints();
            Matrix m = Matrix.RotationYawPitchRoll(0, (float)Math.PI / 2, 0) *
                       Matrix.Translation(0, 4, 0);

            psb.Transform(m);
            psb.Scale(new Vector3(2, 2, 2));
            psb.SetTotalMass(50, true);
            SoftWorld.AddSoftBody(psb);
            cutting = true;
        }
コード例 #10
0
ファイル: SoftDemo.cs プロジェクト: superowner/BulletSharp
 void InitCollide()
 {
     for (int i = 0; i < 3; ++i)
     {
         SoftBody psb = SoftBodyHelpers.CreateFromTriMesh(softBodyWorldInfo, TorusMesh.Vertices, TorusMesh.Indices);
         psb.GenerateBendingConstraints(2);
         psb.Cfg.PIterations = 2;
         psb.Cfg.Collisions |= FCollisions.VFSS;
         psb.RandomizeConstraints();
         Matrix m = Matrix.RotationYawPitchRoll((float)Math.PI / 2 * (i & 1), (float)Math.PI / 2 * (1 - (i & 1)), 0) *
                    Matrix.Translation(3 * i, 2, 0);
         psb.Transform(m);
         psb.Scale(new Vector3(2, 2, 2));
         psb.SetTotalMass(50, true);
         SoftWorld.AddSoftBody(psb);
     }
     cutting = true;
 }
コード例 #11
0
ファイル: SoftDemo.cs プロジェクト: superowner/BulletSharp
        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;
        }
コード例 #12
0
ファイル: SoftDemo.cs プロジェクト: superowner/BulletSharp
        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);
        }
コード例 #13
0
        /// <summary>
        /// Apply these SoftBody settings to this SoftBody
        /// </summary>
        /// <param name="softBody"></param>
        public void ConfigureSoftBody(SoftBody softBody)
        {
            softBody.Scale(scale.ToBullet());

            BulletSharp.SoftBody.Material pm = softBody.Materials[0];

            sBMaterial.SetSBMaterial(pm);

            config.CopyToBulletSBConfig(softBody.Cfg);

            if (allNodeBendingConstraints)
            {
                for (int i = 0; i < softBody.Nodes.Count - 1; ++i)
                {
                    softBody.GenerateBendingConstraints(1 + i);
                }
            }
            else
            {
                softBody.GenerateBendingConstraints(bendingConstraintDistance, pm);
            }

            if (randomizeConstraints)
            {
                softBody.RandomizeConstraints();
            }

            if (generateClusters)
            {
                softBody.GenerateClusters(0);
            }

            softBody.SetTotalMass(totalMass, fromFaces);

            softBody.SetPose(bvolume, bframe);
        }
コード例 #14
0
    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);
    }