Example #1
0
        protected override void Update(GameTime gameTime)
        {
            padState   = GamePad.GetState(PlayerIndex.One);
            keyState   = Keyboard.GetState();
            mouseState = Mouse.GetState();

            if (PressedOnce(Keys.Escape, Buttons.Back))
            {
                Exit();
            }

            if (PressedOnce(Keys.M, Buttons.A))
            {
                multithread = !multithread;
            }

            if (PressedOnce(Keys.P, Buttons.A))
            {
                var e = World.RigidBodies.GetEnumerator();
                e.MoveNext(); e.MoveNext(); e.MoveNext();
                e.MoveNext(); e.MoveNext(); e.MoveNext();
                e.MoveNext(); e.MoveNext(); e.MoveNext();
                (e.Current as RigidBody).IsStatic = true;
                e.MoveNext();
                (e.Current as RigidBody).IsStatic = true;
            }

            if ((mouseState.LeftButton == ButtonState.Pressed &&
                 mousePreviousState.LeftButton == ButtonState.Released) ||
                (padState.IsButtonDown(Buttons.RightThumbstickDown) &&
                 gamePadPreviousState.IsButtonUp(Buttons.RightThumbstickUp)))
            {
                var ray  = Conversion.ToJitterVector(RayTo(mouseState.X, mouseState.Y));
                var camp = Conversion.ToJitterVector(Camera.Position);

                ray = JVector.Normalize(ray) * 100;

                bool result = World.CollisionSystem.Raycast(camp, ray, RaycastCallback, out grabBody, out hitNormal, out float fraction);

                if (result)
                {
                    hitPoint = camp + (fraction * ray);

                    if (grabConstraint != null)
                    {
                        World.RemoveConstraint(grabConstraint);
                    }

                    var lanchor = hitPoint - grabBody.Position;
                    lanchor = JVector.Transform(lanchor, JMatrix.Transpose(grabBody.Orientation));

                    grabConstraint = new SingleBodyConstraints.PointOnPoint(grabBody, lanchor)
                    {
                        Softness   = 0.01f,
                        BiasFactor = 0.1f
                    };

                    World.AddConstraint(grabConstraint);
                    hitDistance           = (Conversion.ToXNAVector(hitPoint) - Camera.Position).Length();
                    scrollWheel           = mouseState.ScrollWheelValue;
                    grabConstraint.Anchor = hitPoint;
                }
            }

            if (mouseState.LeftButton == ButtonState.Pressed || padState.IsButtonDown(Buttons.RightThumbstickDown))
            {
                hitDistance += (mouseState.ScrollWheelValue - scrollWheel) * 0.01f;
                scrollWheel  = mouseState.ScrollWheelValue;

                if (grabBody != null)
                {
                    var ray = RayTo(mouseState.X, mouseState.Y); ray.Normalize();
                    grabConstraint.Anchor = Conversion.ToJitterVector(Camera.Position + (ray * hitDistance));
                    grabBody.IsActive     = true;
                    if (!grabBody.IsStatic)
                    {
                        grabBody.LinearVelocity  *= 0.98f;
                        grabBody.AngularVelocity *= 0.98f;
                    }
                }
            }
            else
            {
                if (grabConstraint != null)
                {
                    World.RemoveConstraint(grabConstraint);
                }

                grabBody       = null;
                grabConstraint = null;
            }

            if (PressedOnce(Keys.Space, Buttons.B))
            {
                SpawnRandomPrimitive(Conversion.ToJitterVector(Camera.Position),
                                     Conversion.ToJitterVector((Camera.Target - Camera.Position) * 40.0f));
            }

            if (PressedOnce(Keys.Add, Buttons.X))
            {
                DestroyCurrentScene();
                currentScene++;
                currentScene = currentScene % PhysicScenes.Count;
                PhysicScenes[currentScene].Build();
            }

            if (PressedOnce(Keys.Subtract, Buttons.Y))
            {
                DestroyCurrentScene();
                currentScene += PhysicScenes.Count - 1;
                currentScene  = currentScene % PhysicScenes.Count;
                PhysicScenes[currentScene].Build();
            }

            UpdateDisplayText(gameTime);

            float step = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (step > 1.0f / 100.0f)
            {
                step = 1.0f / 100.0f;
            }

            World.Step(step, multithread);

            gamePadPreviousState  = padState;
            keyboardPreviousState = keyState;
            mousePreviousState    = mouseState;

            base.Update(gameTime);
        }
        /// <summary>
        /// Searches for physics objects to "grab" based on in input location.
        /// </summary>
        /// <param name="location"></param>
        private void HandleGrabBody(Vector2 location)
        {
            Ray ray = RayTo(location);

            float rayLength = 100f;

            float fraction; // represents distance ray travelled before colliding

            bool result = World.CollisionSystem.Raycast(toJVector(ray.Position), toJVector(ray.Direction) * rayLength, RaycastCallback, out grabBody, out hitNormal, out fraction);

            if (result)
            {
                hitPoint = toJVector(ray.Position + fraction * (ray.Direction * rayLength));

                if (grabConstraint != null)
                {
                    World.RemoveConstraint(grabConstraint);
                }

                JVector lanchor = hitPoint - grabBody.Position;
                lanchor = JVector.Transform(lanchor, JMatrix.Transpose(grabBody.Orientation));

                grabConstraint = new SingleBodyConstraints.PointOnPoint(grabBody, lanchor);
                grabConstraint.Softness = 0.01f;
                grabConstraint.BiasFactor = 0.1f;

                World.AddConstraint(grabConstraint);
                hitDistance = (toVector3(hitPoint) - ray.Position).Length();
                grabConstraint.Anchor = hitPoint;

            }
        }
Example #3
0
        protected override void Update(GameTime gameTime)
        {
            GamePadState padState = GamePad.GetState(PlayerIndex.One);
            KeyboardState keyState = Keyboard.GetState();
            MouseState mouseState = Mouse.GetState();

            // let the user escape the demo
            if (keyState.IsKeyDown(Keys.Escape) || 
                padState.IsButtonDown(Buttons.Back)) this.Exit();

            // change threading mode
            if (keyState.IsKeyDown(Keys.M) && !keyboardPreviousState.IsKeyDown(Keys.M))
                multithread = !multithread;

            if (keyState.IsKeyDown(Keys.P) && !keyboardPreviousState.IsKeyDown(Keys.P))
                debugDraw = !debugDraw;

            #region drag and drop physical objects with the mouse
            if (mouseState.LeftButton == ButtonState.Pressed &&
                mousePreviousState.LeftButton == ButtonState.Released)
            {
                JVector ray = Conversion.ToJitterVector(RayTo(mouseState.X, mouseState.Y));
                JVector camp = Conversion.ToJitterVector(Camera.Position);

                ray = JVector.Normalize(ray) * 100;

                float fraction;
                bool result = World.CollisionSystem.Raycast(camp, ray, RaycastCallback, out grabBody, out hitNormal, out fraction);

                if (result)
                {
                    hitPoint = camp + fraction * ray;

                    if (grabConstraint != null) World.RemoveConstraint(grabConstraint);

                    JVector lanchor = hitPoint - grabBody.Position;
                    lanchor = JVector.Transform(lanchor, JMatrix.Transpose(grabBody.Orientation));

                    grabConstraint = new SingleBodyConstraints.PointOnPoint(grabBody, lanchor);
                    grabConstraint.Softness = 0.01f;
                    grabConstraint.BiasFactor = 0.1f;
                    
                    World.AddConstraint(grabConstraint);
                    hitDistance = (Conversion.ToXNAVector(hitPoint) - Camera.Position).Length();
                    scrollWheel = mouseState.ScrollWheelValue;
                    grabConstraint.Anchor = hitPoint;
                }
            }

            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                hitDistance += (mouseState.ScrollWheelValue - scrollWheel) * 0.01f;
                scrollWheel = mouseState.ScrollWheelValue;

                if (grabBody != null)
                {
                    Vector3 ray = RayTo(mouseState.X, mouseState.Y); ray.Normalize();
                    grabConstraint.Anchor = Conversion.ToJitterVector(Camera.Position + ray * hitDistance);
                    grabBody.IsActive = true;
                    if (!grabBody.IsStatic)
                    {
                        grabBody.LinearVelocity *= 0.98f;
                        grabBody.AngularVelocity *= 0.98f;
                    }
                }
            }
            else
            {
                grabBody = null;
                if (grabConstraint != null) World.RemoveConstraint(grabConstraint);
            }
            #endregion

            #region create random primitives

            if (keyState.IsKeyDown(Keys.Space) && !keyboardPreviousState.IsKeyDown(Keys.Space))
            {
                SpawnRandomPrimitive(Conversion.ToJitterVector(Camera.Position),
                    Conversion.ToJitterVector((Camera.Target - Camera.Position) * 40.0f));

            }
            #endregion

            #region switch through physic scenes
            if (keyState.IsKeyDown(Keys.Add) &&
                !keyboardPreviousState.IsKeyDown(Keys.Add) ||
                keyState.IsKeyDown(Keys.OemPlus) &&
                !keyboardPreviousState.IsKeyDown(Keys.OemPlus))
            {
                PhysicScenes[currentScene].Destroy();
                RemoveComponents();
                currentScene++;
                currentScene = currentScene % PhysicScenes.Count;
                PhysicScenes[currentScene].Build();
            }

            if (keyState.IsKeyDown(Keys.OemMinus) &&
                 !keyboardPreviousState.IsKeyDown(Keys.OemMinus) ||
                 keyState.IsKeyDown(Keys.Subtract) &&
                !keyboardPreviousState.IsKeyDown(Keys.Subtract))
            {
                PhysicScenes[currentScene].Destroy();
                RemoveComponents();
                currentScene += PhysicScenes.Count - 1;
                currentScene = currentScene % PhysicScenes.Count;
                PhysicScenes[currentScene].Build();
            }
            #endregion


            UpdateDisplayText(gameTime);

            float step = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (step > 1.0f / 100.0f) step = 1.0f / 100.0f;

            World.Step(step, multithread);

            gamePadPreviousState = padState;
            keyboardPreviousState = keyState;
            mousePreviousState = mouseState;

            base.Update(gameTime);
        }
 /// <summary>
 /// Releases physics body (if applicable) on release of pointer, either mouse button up or removal of touch input
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="args"></param>
 public void OnManipulationCompleted(GestureRecognizer sender, ManipulationCompletedEventArgs args)
 {
     if (grabConstraint != null)
     {
         World.RemoveConstraint(grabConstraint);
     }
     grabBody = null;
     grabConstraint = null;
 }