public void RemoveSoftBody(BulletSharp.SoftBody.SoftBody softBody)
 {
     if (!_isDisposed && World is BulletSharp.SoftBody.SoftRigidDynamicsWorld)
     {
         Debug.LogFormat("Removing softbody {0} from world", softBody);
         ((BulletSharp.SoftBody.SoftRigidDynamicsWorld)World).RemoveSoftBody(softBody);
     }
 }
Example #2
0
    public GameObject CreateUnitySoftBodyRope(BulletSharp.SoftBody.SoftBody body)
    {
        //determine what kind of soft body it is
        //rope
        GameObject   rope = Instantiate <GameObject>(ropePrefab);
        LineRenderer lr   = rope.GetComponent <LineRenderer>();

        lr.SetVertexCount(body.Nodes.Count);
        BulletRopeProxy ropeProxy = rope.GetComponent <BulletRopeProxy>();

        ropeProxy.target = body;
        return(rope);
    }
        public static bool IntersectsConvexHull(Plane[] convexPlanes, BulletSharp.SoftBody.SoftBody body)
        {
            var nodes  = body.Nodes;
            var nnodes = nodes.Count;

            for (int i = 0; i < nnodes; i++)
            {
                if (MathAlgorithms.IsVertexInsideConvexHull(convexPlanes, ToVector3(nodes[i].Position)))
                {
                    return(true);
                }
            }

            return(false);
        }
    public GameObject CreateUnitySoftBodyCloth(BulletSharp.SoftBody.SoftBody body)
    {
        //build nodes 2 verts map
        Dictionary <BulletSharp.SoftBody.Node, int> node2vertIdx = new Dictionary <BulletSharp.SoftBody.Node, int>();

        for (int i = 0; i < body.Nodes.Count; i++)
        {
            node2vertIdx.Add(body.Nodes[i], i);
        }

        List <int> tris = new List <int>();

        for (int i = 0; i < body.Faces.Count; i++)
        {
            BulletSharp.SoftBody.Face f = body.Faces[i];
            if (f.Nodes.Count != 3)
            {
                Debug.LogError("Face was not a triangle");
                continue;
            }
            for (int j = 0; j < f.Nodes.Count; j++)
            {
                tris.Add(node2vertIdx[f.Nodes[j]]);
            }
        }

        GameObject          go      = Instantiate(softBodyPrefab);
        BulletSoftBodyProxy sbp     = go.GetComponent <BulletSoftBodyProxy>();
        List <int>          trisRev = new List <int>();

        for (int i = 0; i < tris.Count; i += 3)
        {
            trisRev.Add(tris[i]);
            trisRev.Add(tris[i + 2]);
            trisRev.Add(tris[i + 1]);
        }

        tris.AddRange(trisRev);
        sbp.target = body;
        sbp.verts  = new Vector3[body.Nodes.Count];
        sbp.tris   = tris.ToArray();

        return(go);
    }
Example #5
0
    public void PostOnInitializePhysics()
    {
        for (int i = 0; i < demo.World.CollisionObjectArray.Count; i++)
        {
            CollisionObject co = demo.World.CollisionObjectArray[i];
            CollisionShape  cs = co.CollisionShape;
            GameObject      go;
            if (cs.ShapeType == BroadphaseNativeType.SoftBodyShape)
            {
                BulletSharp.SoftBody.SoftBody sb = (BulletSharp.SoftBody.SoftBody)co;
                if (sb.Faces.Count == 0)
                {
                    //rope
                    go = CreateUnitySoftBodyRope(sb);
                }
                else
                {
                    go = CreateUnitySoftBodyCloth(sb);
                }
            }
            else
            {
                //rigid body
                if (cs.ShapeType == BroadphaseNativeType.CompoundShape)
                {
                    BulletSharp.Math.Matrix transform = co.WorldTransform;
                    go = new GameObject("Compund Shape");
                    BulletRigidBodyProxy rbp = go.AddComponent <BulletRigidBodyProxy>();
                    rbp.target = co as RigidBody;
                    foreach (BulletSharp.CompoundShapeChild child in (cs as CompoundShape).ChildList)
                    {
                        BulletSharp.Math.Matrix childTransform = child.Transform;
                        GameObject ggo = new GameObject(child.ToString());
                        MeshFilter mf  = ggo.AddComponent <MeshFilter>();
                        Mesh       m   = mf.mesh;
                        MeshFactory2.CreateShape(child.ChildShape, m);
                        MeshRenderer mr = ggo.AddComponent <MeshRenderer>();
                        mr.sharedMaterial = mat;
                        ggo.transform.SetParent(go.transform);
                        Matrix4x4 mt = childTransform.ToUnity();
                        ggo.transform.localPosition = BSExtensionMethods2.ExtractTranslationFromMatrix(ref mt);
                        ggo.transform.localRotation = BSExtensionMethods2.ExtractRotationFromMatrix(ref mt);
                        ggo.transform.localScale    = BSExtensionMethods2.ExtractScaleFromMatrix(ref mt);

                        /*
                         * BulletRigidBodyProxy rbp = ggo.AddComponent<BulletRigidBodyProxy>();
                         * rbp.target = body;
                         * return go;
                         */
                        //InitRigidBodyInstance(colObj, child.ChildShape, ref childTransform);
                    }
                }
                else if (cs.ShapeType == BroadphaseNativeType.CapsuleShape)
                {
                    GameObject ggo = GameObject.CreatePrimitive(PrimitiveType.Capsule);
                    Destroy(ggo.GetComponent <Collider>());
                    go = new GameObject();
                    ggo.transform.parent        = go.transform;
                    ggo.transform.localPosition = Vector3.zero;
                    ggo.transform.localRotation = Quaternion.identity;
                    BulletRigidBodyProxy rbp = go.AddComponent <BulletRigidBodyProxy>();
                    rbp.target = co as RigidBody;
                }
                else
                {
                    Debug.Log("Creating " + cs.ShapeType + " for " + co.ToString());
                    go = CreateUnityCollisionObjectProxy(co as CollisionObject);
                }
            }
            createdObjs.Add(go);
            Debug.Log("Created Unity Shape for " + co);
        }
    }
Example #6
0
    // Creates a Unity game object from the given Bullet CollisionObject.
    protected void AddUnityObject(CollisionObject co, Material mat)
    {
        CollisionShape cs = co.CollisionShape;
        GameObject     go;

        if (cs.ShapeType == BroadphaseNativeType.SoftBodyShape)
        {
            BulletSharp.SoftBody.SoftBody sb = (BulletSharp.SoftBody.SoftBody)co;
            if (sb.Faces.Count == 0)
            {
                //rope
                go = CreateUnitySoftBodyRope(sb);
            }
            else
            {
                go = CreateUnitySoftBodyCloth(sb);
            }
        }
        else
        {
            //rigid body
            if (cs.ShapeType == BroadphaseNativeType.CompoundShape)
            {
                //BulletSharp.Math.Matrix transform = co.WorldTransform;
                go = new GameObject("Compund Shape");
                BulletRigidBodyProxy rbp = go.AddComponent <BulletRigidBodyProxy>();
                rbp.target = co as RigidBody;
                foreach (BulletSharp.CompoundShapeChild child in (cs as CompoundShape).ChildList)
                {
                    BulletSharp.Math.Matrix childTransform = child.Transform;
                    GameObject ggo = new GameObject(child.ToString());
                    MeshFilter mf  = ggo.AddComponent <MeshFilter>();
                    Mesh       m   = mf.mesh;
                    MeshFactory2.CreateShape(child.ChildShape, m);
                    MeshRenderer mr = ggo.AddComponent <MeshRenderer>();
                    mr.sharedMaterial = mat;
                    ggo.transform.SetParent(go.transform);
                    Matrix4x4 mt = childTransform.ToUnity();
                    ggo.transform.localPosition = BSExtensionMethods2.ExtractTranslationFromMatrix(ref mt);
                    ggo.transform.localRotation = BSExtensionMethods2.ExtractRotationFromMatrix(ref mt);
                    ggo.transform.localScale    = BSExtensionMethods2.ExtractScaleFromMatrix(ref mt);

                    /*
                     * BulletRigidBodyProxy rbp = ggo.AddComponent<BulletRigidBodyProxy>();
                     * rbp.target = body;
                     * return go;
                     */
                    //InitRigidBodyInstance(colObj, child.ChildShape, ref childTransform);
                }
            }
            else if (cs.ShapeType == BroadphaseNativeType.CapsuleShape)
            {
                CapsuleShape css = (CapsuleShape)cs;
                GameObject   ggo = GameObject.CreatePrimitive(PrimitiveType.Capsule);
                Destroy(ggo.GetComponent <Collider>());
                go = new GameObject();
                ggo.transform.parent        = go.transform;
                ggo.transform.localPosition = UnityEngine.Vector3.zero;
                ggo.transform.localRotation = UnityEngine.Quaternion.identity;
                ggo.transform.localScale    = new UnityEngine.Vector3(css.Radius * 2f, css.HalfHeight * 2f, css.Radius * 2f);
                BulletRigidBodyProxy rbp = go.AddComponent <BulletRigidBodyProxy>();
                rbp.target = co;
            }
            else
            {
                //Debug.Log("Creating " + cs.ShapeType + " for " + co.ToString());
                go = CreateUnityCollisionObjectProxy(co as CollisionObject, mat);
            }
        }
        m_createdObjs.Add(go);
    }