示例#1
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);
    }
    //public NativeArray<Matrix4x4> m;

    public void Execute(int i, TransformAccess transform)
    {
        Matrix4x4 m = PhysicsDemo_BulletJobs.CollisionObjects[i].WorldTransform.ToUnity();

        transform.position = BSExtensionMethods2.ExtractTranslationFromMatrix(ref m);
        transform.rotation = BSExtensionMethods2.ExtractRotationFromMatrix(ref m);
    }
 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);
 }
    protected void SaveFinalState()
    {
        m_comf = m_rb.CenterOfMassPosition;
        Matrix curTrans = m_rb.WorldTransform;

        m_posf      = BSExtensionMethods2.ExtractTranslationFromMatrix(ref curTrans);
        m_rotf      = BSExtensionMethods2.ExtractRotationFromMatrix(ref curTrans);
        m_eulerrotf = m_rotf.eulerAngles;
    }
 // Update is called once per frame
 void Update()
 {
     if (target == null)
     {
         return;
     }
     Matrix4x4 m = target.WorldTransform.ToUnity();
     transform.position = BSExtensionMethods2.ExtractTranslationFromMatrix(ref m);
     transform.rotation = BSExtensionMethods2.ExtractRotationFromMatrix(ref m);
     transform.localScale = BSExtensionMethods2.ExtractScaleFromMatrix(ref m);
 }
示例#6
0
        public void Execute(int i)
        {
            Matrix4x4 m = PhysicsDemo_BulletECS.CollisionObjects[i].WorldTransform.ToUnity();

            positions[i] = new Position {
                Value = BSExtensionMethods2.ExtractTranslationFromMatrix(ref m)
            };
            rotations[i] = new Rotation {
                Value = BSExtensionMethods2.ExtractRotationFromMatrix(ref m)
            };
        }
示例#7
0
 void DoEverything()
 {
     OnInitializePhysics();
     for (int i = 0; i < 100; i++)
     {
         OnUpdate();
         for (int j = 0; j < numLinks; j++)
         {
             MultiBodyLinkCollider linkCollider = links[j];
             UnityEngine.Matrix4x4 m            = linkCollider.WorldTransform.ToUnity();
             UnityEngine.Vector3   p            = BSExtensionMethods2.ExtractTranslationFromMatrix(ref m);
             linkPositions[j] = p;
             UnityEngine.Debug.Log("pos " + p.ToString("f3") + " " + j);
         }
     }
     ExitPhysics();
 }
    // The save state functions save various information about the rigid
    // body simulation to global variables to be written out.
    protected void SaveInitState()
    {
        m_com0 = m_rb.CenterOfMassPosition;
        Matrix curTrans = m_rb.WorldTransform;

        m_pos0    = BSExtensionMethods2.ExtractTranslationFromMatrix(ref curTrans);
        m_vel0    = m_rb.LinearVelocity;
        m_angvel0 = m_rb.AngularVelocity;
        if (DEBUG)
        {
            Debug.Log("Init ANGVEL: " + m_angvel0.ToString());
        }
        if (DEBUG)
        {
            Debug.Log("Init ANGVEL len: " + (m_angvel0.Length * Time.fixedDeltaTime).ToString());
        }
        m_rot0      = BSExtensionMethods2.ExtractRotationFromMatrix(ref curTrans);
        m_eulerrot0 = m_rot0.eulerAngles;
    }
示例#9
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");
        }
    }
示例#10
0
    IEnumerator myCoroutine()
    {
        Debug.Log("Waiting to start");
        while (!Input.GetKeyDown(KeyCode.Space))
        {
            yield return(null);
        }
        OnInitializePhysics();

        Debug.Log("Initialized");
        int frame = 0;

        while (frame < 10)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                frame++;
                Debug.Log("Do Update " + frame);
                for (int j = 0; j < numLinks; j++)
                {
                    MultiBodyLinkCollider linkCollider = links[j];
                    for (int k = 0; k < 5; k++)
                    {
                        OnUpdate();
                    }
                    UnityEngine.Matrix4x4 m = linkCollider.WorldTransform.ToUnity();
                    UnityEngine.Vector3   p = BSExtensionMethods2.ExtractTranslationFromMatrix(ref m);
                    linkPositions[j] = p;
                    UnityEngine.Debug.Log("pos " + p.ToString("f3") + " " + j);
                }
            }
            yield return(null);
        }
        Debug.Log("Finalize");
        ExitPhysics();
    }
示例#11
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);
        }
    }
示例#12
0
    void SetGameObjPosFromCollisionObj()
    {
        collisionObject.GetWorldTransform(out Matrix trans);

        transform.position = BSExtensionMethods2.ExtractTranslationFromMatrix(ref trans);
    }
 private void Update()
 {
     target.WorldTransform.ToUnity(ref m);
     _transform.position = BSExtensionMethods2.ExtractTranslationFromMatrix(ref m, ref tempVector3);
     _transform.rotation = BSExtensionMethods2.ExtractRotationFromMatrix(ref m, ref tempQuaternion);
 }
示例#14
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);
    }