Beispiel #1
0
        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;
        }
        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);
        }
Beispiel #4
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;
            }
        }
        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;
            }
        }
Beispiel #6
0
        SoftBody CreateSoftBoulder(Vector3 p, Vector3 s, int np)
        {
            Random random = new Random();

            Vector3[] pts = new Vector3[np];
            for (int i = 0; i < np; ++i)
            {
                pts[i] = GetRandomVector(random) * s;
            }

            SoftBody psb = SoftBodyHelpers.CreateFromConvexHull(softBodyWorldInfo, pts);

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

            return(psb);
        }
        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);
        }
Beispiel #8
0
        void InitClusterCar()
        {
            //SetAzi(180);
            Vector3     origin      = new Vector3(100, 80, 0);
            Quaternion  orientation = Quaternion.RotationYawPitchRoll(-(float)Math.PI / 2, 0, 0);
            const float widthf      = 8;
            const float widthr      = 9;
            const float length      = 8;
            const float height      = 4;

            Vector3[] wheels =
            {
                new Vector3(+widthf, -height, +length), // Front left
                new Vector3(-widthf, -height, +length), // Front right
                new Vector3(+widthr, -height, -length), // Rear left
                new Vector3(-widthr, -height, -length), // Rear right
            };
            SoftBody pa  = CreateClusterBunny(Vector3.Zero, Vector3.Zero);
            SoftBody pfl = CreateClusterTorus(wheels[0], new Vector3(0, 0, (float)Math.PI / 2), new Vector3(2, 4, 2));
            SoftBody pfr = CreateClusterTorus(wheels[1], new Vector3(0, 0, (float)Math.PI / 2), new Vector3(2, 4, 2));
            SoftBody prl = CreateClusterTorus(wheels[2], new Vector3(0, 0, (float)Math.PI / 2), new Vector3(2, 5, 2));
            SoftBody prr = CreateClusterTorus(wheels[3], new Vector3(0, 0, (float)Math.PI / 2), new Vector3(2, 5, 2));

            pfl.Cfg.DF             =
                pfr.Cfg.DF         =
                    prl.Cfg.DF     =
                        prr.Cfg.DF = 1;

            using (var lspecs = new LJoint.Specs())
            {
                lspecs.Cfm      = 1;
                lspecs.Erp      = 1;
                lspecs.Position = wheels[0]; pa.AppendLinearJoint(lspecs, pfl);
                lspecs.Position = wheels[1]; pa.AppendLinearJoint(lspecs, pfr);
                lspecs.Position = wheels[2]; pa.AppendLinearJoint(lspecs, prl);
                lspecs.Position = wheels[3]; pa.AppendLinearJoint(lspecs, prr);
            }

            using (var aspecs = new AJoint.Specs())
            {
                aspecs.Cfm  = 1;
                aspecs.Erp  = 1;
                aspecs.Axis = new Vector3(1, 0, 0);

                aspecs.Control = steerControlF;
                pa.AppendAngularJoint(aspecs, pfl);
                pa.AppendAngularJoint(aspecs, pfr);

                aspecs.Control = motorControl;
                pa.AppendAngularJoint(aspecs, prl);
                pa.AppendAngularJoint(aspecs, prr);
            }

            pa.Rotate(orientation);
            pfl.Rotate(orientation);
            pfr.Rotate(orientation);
            prl.Rotate(orientation);
            prr.Rotate(orientation);
            pa.Translate(origin);
            pfl.Translate(origin);
            pfr.Translate(origin);
            prl.Translate(origin);
            prr.Translate(origin);
            pfl.Cfg.PIterations                     =
                pfr.Cfg.PIterations                 =
                    prl.Cfg.PIterations             =
                        prr.Cfg.PIterations         = 1;
            pfl.Clusters[0].Matching                =
                pfr.Clusters[0].Matching            =
                    prl.Clusters[0].Matching        =
                        prr.Clusters[0].Matching    = 0.05f;
            pfl.Clusters[0].NodeDamping             =
                pfr.Clusters[0].NodeDamping         =
                    prl.Clusters[0].NodeDamping     =
                        prr.Clusters[0].NodeDamping = 0.05f;

            CreateStairs(20, new Vector3(0, -8, 0), new Vector3(3, 2, 40));
            CreateRigidBodyStack(50);
        }
Beispiel #9
0
        public override void Build()
        {
            AddGround();

            for (int i = 0; i < 15; i++)
            {
                bool even = (i % 2 == 0);

                for (int e = 0; e < 3; e++)
                {
                    JVector   size = (even) ? new JVector(1, 1, 3) : new JVector(3, 1, 1);
                    RigidBody body = new RigidBody(new BoxShape(size));
                    body.Position = new JVector(3.0f + (even ? e : 1.0f), i + 0.5f, -5.0f + (even ? 1.0f : e));

                    Demo.World.AddBody(body);
                }
            }


            /*Model model = this.Demo.Content.Load<Model>("Model/torus");
             *
             * List<TriangleVertexIndices> indices = new List<TriangleVertexIndices>();
             * List<JVector> vertices = new List<JVector>();
             *
             * ConvexHullObject.ExtractData(vertices, indices, model);
             * RemoveDuplicateVertices(indices, vertices);
             *
             * softBodyTorus = new SoftBody(indices, vertices);
             *
             * softBodyTorus.Translate(new JVector(10, 5, 0));
             * softBodyTorus.Pressure = 50.0f;
             * softBodyTorus.SetSpringValues(0.2f, 0.005f);
             * //softBodyTorus.SelfCollision = true; ;
             * softBodyTorus.Material.KineticFriction = 0.9f;
             * softBodyTorus.Material.StaticFriction = 0.95f;
             *
             * Demo.World.AddBody(softBodyTorus);*/



            softBodyCloth = new SoftBody(20, 20, 0.4f);

            // ##### Uncomment for selfcollision, all 3 lines
            //cloth.SelfCollision = true;
            //cloth.TriangleExpansion = 0.05f;
            //cloth.VertexExpansion = 0.05f;

            softBodyCloth.Translate(new JVector(0, 10, 10));

            softBodyCloth.Material.KineticFriction = 0.9f;
            softBodyCloth.Material.StaticFriction  = 0.95f;

            softBodyCloth.VertexBodies[0].IsStatic   = true;
            softBodyCloth.VertexBodies[380].IsStatic = true;
            softBodyCloth.VertexBodies[19].IsStatic  = true;
            softBodyCloth.VertexBodies[399].IsStatic = true;

            softBodyCloth.SetSpringValues(SoftBody.SpringType.EdgeSpring, 0.1f, 0.01f);
            softBodyCloth.SetSpringValues(SoftBody.SpringType.ShearSpring, 0.1f, 0.03f);
            softBodyCloth.SetSpringValues(SoftBody.SpringType.BendSpring, 0.1f, 0.03f);

            // ###### Uncomment here for a better visualization
            //Demo.Components.Add(new ClothObject(Demo, cloth));

            Demo.World.AddBody(softBodyCloth);
        }
    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);
    }