Beispiel #1
0
 //singleton not sure if it needs to be
 public static BulletExampleRunner Get()
 {
     if (singleton == null)
     {
         BulletExampleRunner[] ws = FindObjectsOfType <BulletExampleRunner>();
         if (ws.Length == 1)
         {
             singleton = ws[0];
         }
         else if (ws.Length == 0)
         {
             Debug.LogError("Need to add a BulletExampleRunner to the scene");
         }
         else
         {
             Debug.LogError("Found more than one dynamics world.");
             singleton = ws[0];
             for (int i = 1; i < ws.Length; i++)
             {
                 GameObject.Destroy(ws[i].gameObject);
             }
         }
     }
     return(singleton);
 }
Beispiel #2
0
        public virtual void ExitPhysics()
        {
            BulletExampleRunner.Get().ExitPhysics();
            UnityEngine.Debug.Log("ExitPhysics");
            if (_world != null)
            {
                //remove/dispose constraints
                int i;
                for (i = _world.NumConstraints - 1; i >= 0; i--)
                {
                    TypedConstraint constraint = _world.GetConstraint(i);
                    _world.RemoveConstraint(constraint);
                    constraint.Dispose();
                }

                //remove the rigidbodies from the dynamics world and delete them
                for (i = _world.NumCollisionObjects - 1; i >= 0; i--)
                {
                    CollisionObject obj  = _world.CollisionObjectArray[i];
                    RigidBody       body = obj as RigidBody;
                    if (body != null && body.MotionState != null)
                    {
                        body.MotionState.Dispose();
                    }
                    _world.RemoveCollisionObject(obj);
                    obj.Dispose();
                }

                //delete collision shapes
                foreach (CollisionShape shape in CollisionShapes)
                {
                    shape.Dispose();
                }
                CollisionShapes.Clear();

                _world.Dispose();
                Broadphase.Dispose();
                Dispatcher.Dispose();
                CollisionConf.Dispose();
            }

            if (Broadphase != null)
            {
                Broadphase.Dispose();
            }
            if (Dispatcher != null)
            {
                Dispatcher.Dispose();
            }
            if (CollisionConf != null)
            {
                CollisionConf.Dispose();
            }
        }
Beispiel #3
0
 public virtual void ClientResetScene()
 {
     RemovePickingConstraint();
     ExitPhysics();
     OnInitializePhysics();
     BulletExampleRunner.Get().PostOnInitializePhysics();
     if (World != null && _debugDrawer != null)
     {
         World.DebugDrawer = _debugDrawer;
     }
 }
Beispiel #4
0
        public void Run()
        {
            using (Graphics = GraphicsLibraryManager.GetGraphics(this))
            {
                Input    = new Input(Graphics.Form);
                Freelook = new FreeLook(Input);

                Graphics.Initialize();
                Graphics.CullingEnabled = isCullingEnabled;
                OnInitialize();

                if (World == null)
                {
                    OnInitializePhysics();
                    BulletExampleRunner.Get().PostOnInitializePhysics();
                }

                if (_isDebugDrawEnabled)
                {
                    if (_debugDrawer == null)
                    {
                        _debugDrawer           = Graphics.GetPhysicsDebugDrawer();
                        _debugDrawer.DebugMode = DebugDrawMode;
                    }
                    if (World != null)
                    {
                        World.DebugDrawer = _debugDrawer;
                    }
                }

                Graphics.UpdateView();

                clock.Start();

                Graphics.Run();

                /*
                 * if (_debugDrawer != null)
                 * {
                 * if (World != null)
                 * {
                 * World.DebugDrawer = null;
                 * }
                 * if (_debugDrawer is IDisposable)
                 * {
                 * (_debugDrawer as IDisposable).Dispose();
                 * }
                 * _debugDrawer = null;
                 * }
                 */
            }
            //Graphics = null;
        }
        void AddColliders(MultiBody multiBody, Vector3 baseHalfExtents, Vector3 linkHalfExtents)
        {
            // Add a collider for the base
            Quaternion[] worldToLocal = new Quaternion[multiBody.NumLinks + 1];
            Vector3[]    localOrigin  = new Vector3[multiBody.NumLinks + 1];

            worldToLocal[0] = multiBody.WorldToBaseRot;
            localOrigin[0]  = multiBody.BasePosition;

            //if (true)
            {
                var collider = new MultiBodyLinkCollider(multiBody, -1);
                collider.CollisionShape = new BoxShape(baseHalfExtents);

                Matrix tr = Matrix.RotationQuaternion(worldToLocal[0].Inverse);
                tr.Origin = localOrigin[0];
                collider.WorldTransform = tr;

                World.AddCollisionObject(collider, CollisionFilterGroups.StaticFilter,
                                         CollisionFilterGroups.DefaultFilter | CollisionFilterGroups.StaticFilter);
                BulletExampleRunner.Get().CreateUnityMultiBodyLinkColliderProxy(collider);
                collider.Friction      = Friction;
                multiBody.BaseCollider = collider;
            }

            for (int i = 0; i < multiBody.NumLinks; i++)
            {
                int parent = multiBody.GetParent(i);
                worldToLocal[i + 1] = multiBody.GetParentToLocalRot(i) * worldToLocal[parent + 1];
                localOrigin[i + 1]  = localOrigin[parent + 1] + (worldToLocal[i + 1].Inverse.Rotate(multiBody.GetRVector(i)));
            }

            for (int i = 0; i < multiBody.NumLinks; i++)
            {
                var collider = new MultiBodyLinkCollider(multiBody, i);
                collider.CollisionShape = new BoxShape(linkHalfExtents);
                Matrix tr = Matrix.RotationQuaternion(worldToLocal[i + 1].Inverse) * Matrix.Translation(localOrigin[i + 1]);
                collider.WorldTransform = tr;
                World.AddCollisionObject(collider, CollisionFilterGroups.StaticFilter,
                                         CollisionFilterGroups.DefaultFilter | CollisionFilterGroups.StaticFilter);
                collider.Friction = Friction;
                BulletExampleRunner.Get().CreateUnityMultiBodyLinkColliderProxy(collider);
                multiBody.GetLink(i).Collider = collider;
            }
        }
 //singleton not sure if it needs to be
 public static BulletExampleRunner Get()
 {
     if (singleton == null) {
         BulletExampleRunner[] ws = FindObjectsOfType<BulletExampleRunner>();
         if (ws.Length == 1) {
             singleton = ws[0];
         } else if (ws.Length == 0) {
             Debug.LogError("Need to add a BulletExampleRunner to the scene");
         } else {
             Debug.LogError("Found more than one dynamics world.");
             singleton = ws[0];
             for (int i = 1; i < ws.Length; i++) {
                 GameObject.Destroy(ws[i].gameObject);
             }
         }
     }
     return singleton;
 }