Пример #1
0
        //----------------------------------------------------------------------------------------------

        public override void ClientResetScene()
        {
            //#ifdef SHOW_NUM_DEEP_PENETRATIONS
            gNumDeepPenetrationChecks = 0;
            gNumGjkChecks             = 0;
            //#endif //SHOW_NUM_DEEP_PENETRATIONS

            gNumClampedCcdMotions = 0;
            int numObjects = 0;

            foreach (DiscreteDynamicsWorld world in m_worlds)
            {
                // Prefer a better place for this...
                world.SetDebugDrawer(m_debugDraw);
                numObjects = world.GetNumCollisionObjects();

                IList <CollisionObject> copyArray = world.GetCollisionObjectArray();

                for (int i = 0; i < numObjects; i++)
                {
                    CollisionObject colObj = copyArray[i];
                    RigidBody       body   = RigidBody.Upcast(colObj);
                    if (body != null)
                    {
                        if (body.GetMotionState() != null)
                        {
                            DefaultMotionState myMotionState = (DefaultMotionState)body.GetMotionState();
                            myMotionState.m_graphicsWorldTrans = myMotionState.m_startWorldTrans;
                            body.SetCenterOfMassTransform(ref myMotionState.m_graphicsWorldTrans);
                            colObj.SetInterpolationWorldTransform(ref myMotionState.m_startWorldTrans);
                            if (colObj.GetActivationState() != ActivationState.DISABLE_DEACTIVATION)
                            {
                                colObj.ForceActivationState(ActivationState.ACTIVE_TAG);
                                colObj.Activate();
                                colObj.SetDeactivationTime(0);
                            }
                            //colObj.setActivationState(WANTS_DEACTIVATION);
                        }
                        //removed cached contact points (this is not necessary if all objects have been removed from the dynamics world)
                        world.GetBroadphase().GetOverlappingPairCache().CleanProxyFromPairs(colObj.GetBroadphaseHandle(), world.GetDispatcher());

                        if (!body.IsStaticObject())
                        {
                            IndexedVector3 zero = IndexedVector3.Zero;
                            body.SetLinearVelocity(ref zero);
                            body.SetAngularVelocity(ref zero);
                        }
                    }
                }
                ///reset some internal cached data in the broadphase
                world.GetBroadphase().ResetPool(world.GetDispatcher());
                world.GetConstraintSolver().Reset();
            }
        }
Пример #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);
        }