public void FixedUpdate()
 {
     BulletSharp.Math.Matrix trans;
     ghostObject.GetWorldTransform(out trans);
     transform.position   = BSExtensionMethods2.ExtractTranslationFromMatrix(ref trans);
     transform.rotation   = BSExtensionMethods2.ExtractRotationFromMatrix(ref trans);
     transform.localScale = BSExtensionMethods2.ExtractScaleFromMatrix(ref trans);
 }
示例#2
0
    // Update is called once per frame
    void Update()
    {
        Matrix4x4 m = target.WorldTransform.ToUnity();

        transform.position   = BSExtensionMethods2.ExtractTranslationFromMatrix(ref m);
        transform.rotation   = BSExtensionMethods2.ExtractRotationFromMatrix(ref m);
        transform.localScale = BSExtensionMethods2.ExtractScaleFromMatrix(ref m);
    }
示例#3
0
    public void CreateUnityMultiBodyLinkColliderProxy(MultiBodyLinkCollider body)
    {
        GameObject     cube = Instantiate <GameObject>(cubePrefab);
        CollisionShape cs   = body.CollisionShape;

        if (cs is BoxShape)
        {
            BoxShape bxcs = cs as BoxShape;
            BulletSharp.Math.Vector3 s  = bxcs.HalfExtentsWithMargin;
            MeshRenderer             mr = cube.GetComponentInChildren <MeshRenderer>();
            mr.transform.localScale = s.ToUnity() * 2f;
            Matrix4x4 m = body.WorldTransform.ToUnity();
            cube.transform.position   = BSExtensionMethods2.ExtractTranslationFromMatrix(ref m);
            cube.transform.rotation   = BSExtensionMethods2.ExtractRotationFromMatrix(ref m);
            cube.transform.localScale = BSExtensionMethods2.ExtractScaleFromMatrix(ref m);
            Destroy(cube.GetComponent <BulletRigidBodyProxy>());
            BulletMultiBodyLinkColliderProxy cp = cube.AddComponent <BulletMultiBodyLinkColliderProxy>();
            cp.target = body;
        }
        else
        {
            Debug.LogError("Not implemented");
        }
    }
示例#4
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);
        }
    }
示例#5
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);
    }