Esempio n. 1
0
        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyState = Keyboard.GetState();
            MouseState mouseState = Mouse.GetState();

            if (keyState.IsKeyDown(Keys.Escape))
                this.Exit();

            #region Picking Objects with the mouse
            if (mouseState.MiddleButton == ButtonState.Pressed)
            {
                if (middleButton == false)
                {
                    Vector3 ray = RayTo(mouseState.X, mouseState.Y);
                    float frac; CollisionSkin skin;
                    Vector3 pos, normal;

                    ImmovableSkinPredicate pred = new ImmovableSkinPredicate();

                    physicSystem.CollisionSystem.SegmentIntersect(out frac, out skin, out pos, out normal,
                        new Segment(camera.Position, ray * 1000.0f), pred);

                    if (skin != null && (skin.Owner != null))
                    {
                        if (!skin.Owner.Immovable)
                        {
                            Vector3 delta = pos - skin.Owner.Position;
                            delta = Vector3.Transform(delta, Matrix.Transpose(skin.Owner.Orientation));

                            camPickDistance = (camera.Position - pos).Length();
                            oldWheel = mouseState.ScrollWheelValue;

                            skin.Owner.SetActive();
                            objectController.Destroy();
                            damperController.Destroy();
                            objectController.Initialise(skin.Owner, delta, pos);
                            damperController.Initialise(skin.Owner, ConstraintVelocity.ReferenceFrame.Body, Vector3.Zero, Vector3.Zero);
                            objectController.EnableConstraint();
                            damperController.EnableConstraint();
                        }
                    }

                    middleButton = true;
                }

                if (objectController.IsConstraintEnabled && (objectController.Body != null))
                {
                    Vector3 delta = objectController.Body.Position - camera.Position;
                    Vector3 ray = RayTo(mouseState.X, mouseState.Y); ray.Normalize();
                    float deltaWheel = mouseState.ScrollWheelValue - oldWheel;
                    camPickDistance += deltaWheel * 0.01f;
                    Vector3 result = camera.Position + camPickDistance * ray;
                    oldWheel = mouseState.ScrollWheelValue;
                    objectController.WorldPosition = result;
                    objectController.Body.SetActive();
                }
            }
            else
            {
                objectController.DisableConstraint();
                damperController.DisableConstraint();
                middleButton = false;
            }
            #endregion

            if (mouseState.LeftButton == ButtonState.Pressed && leftButton == false)
            {
                PhysicObject physObj = SpawnPrimitive(camera.Position, Matrix.CreateRotationX(0.5f));
                physObj.PhysicsBody.Velocity = (camera.Target - camera.Position) * 20.0f;
                Components.Add(physObj);
                leftButton = true;
            }

            if (mouseState.LeftButton == ButtonState.Released) leftButton = false;

            Keys[] pressedKeys = keyState.GetPressedKeys();

            if (pressedKeys.Length != 0)
            {
                switch (pressedKeys[0])
                {
                    case Keys.D1: ResetScene(); CreateScene1(9); break;
                    case Keys.D2: ResetScene(); CreateScene2(); break;
                    case Keys.D3: ResetScene(); CreateScene3(); break;
                    case Keys.D4: ResetScene(); CreateScene4(12); break;
                    case Keys.D5: ResetScene(); CreateScene5(10); break;
                    case Keys.D6: ResetScene(); CreateScene6(); break;
                    case Keys.D7: ResetScene(); CreateScene7(); break;
                    case Keys.D8: ResetScene(); CreateScene8(); break;
                    case Keys.D9: ResetScene(); CreateScene9(); break;
                    case Keys.D0: ResetScene(); CreateScene0(); break;
                }
            }

            debugDrawer.Enabled = keyState.IsKeyDown(Keys.C);

            if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.Down))
            {
                if (keyState.IsKeyDown(Keys.Up))
                    carObject.Car.Accelerate = 1.0f;
                else
                    carObject.Car.Accelerate = -1.0f;
            }
            else
                carObject.Car.Accelerate = 0.0f;

            if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.Right))
            {
                if (keyState.IsKeyDown(Keys.Left))
                    carObject.Car.Steer = 1.0f;
                else
                    carObject.Car.Steer = -1.0f;
            }
            else
                carObject.Car.Steer = 0.0f;

            if (keyState.IsKeyDown(Keys.B))
                carObject.Car.HBrake = 1.0f;
            else
                carObject.Car.HBrake = 0.0f;

            if (singleStep == true && keyState.IsKeyDown(Keys.Space) == false)
            {
                // don't intergrate so we can step at will
            }
            else
            {
                float timeStep = (float)gameTime.ElapsedGameTime.Ticks / TimeSpan.TicksPerSecond;
                if (timeStep < 1.0f / 60.0f) physicSystem.Integrate(timeStep);
                else physicSystem.Integrate(1.0f / 60.0f);
            }

            base.Update(gameTime);
        }
Esempio n. 2
0
        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyState   = Keyboard.GetState();
            MouseState    mouseState = Mouse.GetState();

            if (keyState.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }


            #region Picking Objects with the mouse
            if (mouseState.MiddleButton == ButtonState.Pressed)
            {
                if (middleButton == false)
                {
                    Vector3 ray = RayTo(mouseState.X, mouseState.Y);
                    float   frac; CollisionSkin skin;
                    Vector3 pos, normal;

                    ImmovableSkinPredicate pred = new ImmovableSkinPredicate();

                    physicSystem.CollisionSystem.SegmentIntersect(out frac, out skin, out pos, out normal,
                                                                  new Segment(camera.Position, ray * 1000.0f), pred);

                    if (skin != null && (skin.Owner != null))
                    {
                        if (!skin.Owner.Immovable)
                        {
                            Vector3 delta = pos - skin.Owner.Position;
                            delta = Vector3.Transform(delta, Matrix.Transpose(skin.Owner.Orientation));

                            camPickDistance = (camera.Position - pos).Length();
                            oldWheel        = mouseState.ScrollWheelValue;

                            skin.Owner.SetActive();
                            objectController.Destroy();
                            damperController.Destroy();
                            objectController.Initialise(skin.Owner, delta, pos);
                            damperController.Initialise(skin.Owner, ConstraintVelocity.ReferenceFrame.Body, Vector3.Zero, Vector3.Zero);
                            objectController.EnableConstraint();
                            damperController.EnableConstraint();
                        }
                    }

                    middleButton = true;
                }

                if (objectController.IsConstraintEnabled && (objectController.Body != null))
                {
                    Vector3 delta      = objectController.Body.Position - camera.Position;
                    Vector3 ray        = RayTo(mouseState.X, mouseState.Y); ray.Normalize();
                    float   deltaWheel = mouseState.ScrollWheelValue - oldWheel;
                    camPickDistance += deltaWheel * 0.01f;
                    Vector3 result = camera.Position + camPickDistance * ray;
                    oldWheel = mouseState.ScrollWheelValue;
                    objectController.WorldPosition = result;
                    objectController.Body.SetActive();
                }
            }
            else
            {
                objectController.DisableConstraint();
                damperController.DisableConstraint();
                middleButton = false;
            }
            #endregion

            if (mouseState.LeftButton == ButtonState.Pressed && leftButton == false)
            {
                PhysicObject physObj = SpawnPrimitive(camera.Position, Matrix.CreateRotationX(0.5f));
                physObj.PhysicsBody.Velocity = (camera.Target - camera.Position) * 20.0f;
                Components.Add(physObj);
                leftButton = true;
            }

            if (mouseState.LeftButton == ButtonState.Released)
            {
                leftButton = false;
            }

            Keys[] pressedKeys = keyState.GetPressedKeys();

            if (pressedKeys.Length != 0)
            {
                switch (pressedKeys[0])
                {
                case Keys.D1: ResetScene(); CreateScene1(9); break;

                case Keys.D2: ResetScene(); CreateScene2(); break;

                case Keys.D3: ResetScene(); CreateScene3(); break;

                case Keys.D4: ResetScene(); CreateScene4(12); break;

                case Keys.D5: ResetScene(); CreateScene5(20); break;

                case Keys.D6: ResetScene(); CreateScene6(); break;

                case Keys.D7: ResetScene(); CreateScene7(); break;

                case Keys.D8: ResetScene(); CreateScene8(); break;

                case Keys.D9: ResetScene(); CreateScene9(); break;

                case Keys.D0: ResetScene(); CreateScene0(); break;
                }
            }

            debugDrawer.Enabled = keyState.IsKeyDown(Keys.C);

            if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.Down))
            {
                if (keyState.IsKeyDown(Keys.Up))
                {
                    carObject.Car.Accelerate = 1.0f;
                }
                else
                {
                    carObject.Car.Accelerate = -1.0f;
                }
            }
            else
            {
                carObject.Car.Accelerate = 0.0f;
            }

            if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.Right))
            {
                if (keyState.IsKeyDown(Keys.Left))
                {
                    carObject.Car.Steer = 1.0f;
                }
                else
                {
                    carObject.Car.Steer = -1.0f;
                }
            }
            else
            {
                carObject.Car.Steer = 0.0f;
            }

            if (keyState.IsKeyDown(Keys.B))
            {
                carObject.Car.HBrake = 1.0f;
            }
            else
            {
                carObject.Car.HBrake = 0.0f;
            }


            if (singleStep == true && keyState.IsKeyDown(Keys.Space) == false)
            {
                // don't intergrate so we can step at will
            }
            else
            {
                float timeStep = (float)gameTime.ElapsedGameTime.Ticks / TimeSpan.TicksPerSecond;
                if (timeStep < 1.0f / 60.0f)
                {
                    physicSystem.Integrate(timeStep);
                }
                else
                {
                    physicSystem.Integrate(1.0f / 60.0f);
                }
            }

            base.Update(gameTime);
        }