//Create a World
    //Add a single cube
    //Step the simulation 300 steps
    //Clean up.
    void Start()
    {
        List <CollisionShape> CollisionShapes = new List <CollisionShape>();

        DefaultCollisionConfiguration CollisionConf = new DefaultCollisionConfiguration();
        CollisionDispatcher           Dispatcher    = new CollisionDispatcher(CollisionConf);

        DbvtBroadphase Broadphase = new DbvtBroadphase();

        DiscreteDynamicsWorld World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConf);

        World.Gravity = new BulletSharp.Math.Vector3(0, -10, 0);


        // create a few dynamic rigidbodies
        const float mass = 1.0f;

        RigidBody fallRigidBody;
        BoxShape  shape = new BoxShape(1f, 1f, 1f);

        BulletSharp.Math.Vector3 localInertia = BulletSharp.Math.Vector3.Zero;
        shape.CalculateLocalInertia(mass, out localInertia);
        RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, null, shape, localInertia);

        fallRigidBody = new RigidBody(rbInfo);
        rbInfo.Dispose();
        Matrix st = Matrix.Translation(new BulletSharp.Math.Vector3(0f, 10f, 0f));

        fallRigidBody.WorldTransform = st;
        World.AddRigidBody(fallRigidBody);

        for (int i = 0; i < 300; i++)
        {
            World.StepSimulation(1f / 60f, 10);

            Matrix trans;
            fallRigidBody.GetWorldTransform(out trans);

            Debug.Log("box height: " + trans.Origin);
        }

        World.RemoveRigidBody(fallRigidBody);
        fallRigidBody.Dispose();

        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 ss in CollisionShapes)
            {
                ss.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();
        }
    }
Ejemplo n.º 2
0
        /// <summary>
        /// Called when the game has determined that game logic needs to be processed.
        /// </summary>
        /// <param name="gameTime">Time passed since the last call to this function.</param>
        protected override void Update(GameTime gameTime)
        {
            MouseState mouseState = Mouse.GetState();
            Vector3    rayTo      = getRayTo(mouseState.X, mouseState.Y);

            if (mouseState.LeftButton == ButtonState.Pressed && _prevMouseState.LeftButton == ButtonState.Released)
            {
                shootBox(rayTo);
            }
            if (mouseState.MiddleButton == ButtonState.Pressed && _prevMouseState.MiddleButton == ButtonState.Released)
            {
                if (_world != null)
                {
                    CollisionWorld.ClosestRayResultCallback rayCallback = new CollisionWorld.ClosestRayResultCallback(_camera.Position, rayTo);
                    _world.RayTest(_camera.Position, rayTo, rayCallback);
                    if (rayCallback.HasHit)
                    {
                        RigidBody body = RigidBody.Upcast(rayCallback.CollisionObject);
                        if (body != null)
                        {
                            //other exclusions?
                            if (!(body.IsStaticObject || body.IsKinematicObject))
                            {
                                _pickedBody = body;
                                _pickedBody.ActivationState = ActivationState.DisableDeactivation;

                                Vector3 pickPos = rayCallback.HitPointWorld;

                                Vector3 localPivot = Vector3.Transform(pickPos, XnaDevRu.BulletX.MathHelper.InvertMatrix(body.CenterOfMassTransform));

                                Point2PointConstraint p2p = new Point2PointConstraint(body, localPivot);
                                _world.AddConstraint(p2p);
                                _pickConstraint = p2p;

                                //save mouse position for dragging
                                _oldPickingPos = rayTo;

                                Vector3 eyePos = new Vector3(_camera.Position.X, _camera.Position.Y, _camera.Position.Z);

                                _oldPickingDist = (eyePos - pickPos).Length();

                                //very weak constraint for picking
                                p2p.Settings.Tau = 1.1f;
                            }
                        }
                    }
                }
            }
            else if (mouseState.MiddleButton == ButtonState.Released && _prevMouseState.MiddleButton == ButtonState.Pressed)
            {
                if (_pickConstraint != null && _world != null)
                {
                    _world.RemoveConstraint(_pickConstraint);
                    _pickConstraint = null;
                    _pickedBody.ForceActivationState(ActivationState.Active);
                    _pickedBody.DeactivationTime = 0f;
                    _pickedBody = null;
                }
            }

            if (_pickConstraint != null)
            {
                //move the constraint pivot
                Point2PointConstraint p2p = _pickConstraint as Point2PointConstraint;
                if (p2p != null)
                {
                    //keep it at the same picking distance
                    Vector3 dir = rayTo - _camera.Position;
                    dir.Normalize();
                    dir *= _oldPickingDist;

                    Vector3 newPos = _camera.Position + dir;
                    p2p.PivotInB = newPos;
                }
            }

            _prevMouseState = mouseState;

            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                //world.stepSimulation(1.0f/60.0f,0);
                int numObjects = _world.CollisionObjectsCount;

                for (int i = 0; i < numObjects; i++)
                {
                    CollisionObject colObj = _world.CollisionObjects[i];
                    RigidBody       body   = RigidBody.Upcast(colObj);
                    if (body != null)
                    {
                        if (body.MotionState != null)
                        {
                            DefaultMotionState myMotionState = (DefaultMotionState)body.MotionState;
                            myMotionState.GraphicsWorldTransform = myMotionState.StartWorldTransform;
                            colObj.WorldTransform = myMotionState.GraphicsWorldTransform;
                            colObj.InterpolationWorldTransform = myMotionState.StartWorldTransform;
                            colObj.Activate();
                        }

                        //removed cached contact points
                        _world.Broadphase.CleanProxyFromPairs(colObj.Broadphase);

                        if (body != null && !body.IsStaticObject)
                        {
                            RigidBody.Upcast(colObj).LinearVelocity  = new Vector3(0, 0, 0);
                            RigidBody.Upcast(colObj).AngularVelocity = new Vector3(0, 0, 0);
                        }
                    }
                }
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Escape) || GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                Exit();
            }
            else
            {
                //world.stepSimulation(1.0f / 60.0f, 1);
            }

            base.Update(gameTime);
        }
Ejemplo n.º 3
0
        protected void Dispose(bool disposing)
        {
            if (debugType >= BDebug.DebugType.Debug)
            {
                Debug.Log("BDynamicsWorld Disposing physics.");
            }

            if (lateUpdateHelper != null)
            {
                lateUpdateHelper.m_ddWorld = null;
                lateUpdateHelper.m_world   = null;
            }
            if (m_world != null)
            {
                //remove/dispose constraints
                int i;
                if (_ddWorld != null)
                {
                    if (debugType >= BDebug.DebugType.Debug)
                    {
                        Debug.LogFormat("Removing Constraints {0}", _ddWorld.NumConstraints);
                    }
                    for (i = _ddWorld.NumConstraints - 1; i >= 0; i--)
                    {
                        TypedConstraint constraint = _ddWorld.GetConstraint(i);
                        _ddWorld.RemoveConstraint(constraint);
                        if (constraint.Userobject is BTypedConstraint)
                        {
                            ((BTypedConstraint)constraint.Userobject).m_isInWorld = false;
                        }
                        if (debugType >= BDebug.DebugType.Debug)
                        {
                            Debug.LogFormat("Removed Constaint {0}", constraint.Userobject);
                        }
                        constraint.Dispose();
                    }
                }

                if (debugType >= BDebug.DebugType.Debug)
                {
                    Debug.LogFormat("Removing Collision Objects {0}", _ddWorld.NumCollisionObjects);
                }
                //remove the rigidbodies from the dynamics world and delete them
                for (i = m_world.NumCollisionObjects - 1; i >= 0; i--)
                {
                    CollisionObject obj  = m_world.CollisionObjectArray[i];
                    RigidBody       body = obj as RigidBody;
                    if (body != null && body.MotionState != null)
                    {
                        Debug.Assert(body.NumConstraintRefs == 0, "Rigid body still had constraints");
                        body.MotionState.Dispose();
                    }
                    m_world.RemoveCollisionObject(obj);
                    if (obj.UserObject is BCollisionObject)
                    {
                        ((BCollisionObject)obj.UserObject).isInWorld = false;
                    }
                    if (debugType >= BDebug.DebugType.Debug)
                    {
                        Debug.LogFormat("Removed CollisionObject {0}", obj.UserObject);
                    }
                    obj.Dispose();
                }

                if (m_world.DebugDrawer != null)
                {
                    if (m_world.DebugDrawer is IDisposable)
                    {
                        IDisposable dis = (IDisposable)m_world.DebugDrawer;
                        dis.Dispose();
                    }
                }

                m_world.Dispose();
                Broadphase.Dispose();
                Dispatcher.Dispose();
                CollisionConf.Dispose();
                _ddWorld = null;
                m_world  = null;
            }

            if (Broadphase != null)
            {
                Broadphase.Dispose();
                Broadphase = null;
            }
            if (Dispatcher != null)
            {
                Dispatcher.Dispose();
                Dispatcher = null;
            }
            if (CollisionConf != null)
            {
                CollisionConf.Dispose();
                CollisionConf = null;
            }
            if (Solver != null)
            {
                Solver.Dispose();
                Solver = null;
            }
            if (softBodyWorldInfo != null)
            {
                softBodyWorldInfo.Dispose();
                softBodyWorldInfo = null;
            }
            _isDisposed = true;
            singleton   = null;
        }
Ejemplo n.º 4
0
        /*This Function is called at:
         * public static void Main()
         * {
         *     var app = new BulletTest();
         *      app.Run();
         *      _physic.World.Dispose();
         * }
         * definitely the wrong place!!!!!!!!
         * TODO: call it at the right place
         */
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        public void Dispose()
        {
            if (BtWorld != null)
            {
                /* for (int d = 0; d < BtWorld.Dispatcher.NumManifolds; d++)
                 * {
                 *  var m = BtWorld.Dispatcher.GetManifoldByIndexInternal(d);
                 *  BtWorld.Dispatcher.ReleaseManifold(m);
                 *  ;
                 * }*/


                //remove/dispose constraints
                int i;
                for (i = BtWorld.NumConstraints - 1; i >= 0; i--)
                {
                    TypedConstraint constraint = BtWorld.GetConstraint(i);
                    BtWorld.RemoveConstraint(constraint);
                    constraint.Dispose();
                }


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

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



                BtWorld.Dispose();
                BtBroadphase.Dispose();
                BtDispatcher.Dispose();
                BtCollisionConf.Dispose();
            }

            if (BtBroadphase != null)
            {
                BtBroadphase.Dispose();
            }
            if (BtDispatcher != null)
            {
                BtDispatcher.Dispose();
            }
            if (BtCollisionConf != null)
            {
                BtCollisionConf.Dispose();
            }
        }