Пример #1
0
        protected virtual void OnUpdate()
        {
            FrameDelta        = clock.GetFrameDelta();
            frameAccumulator += FrameDelta;
            if (frameAccumulator >= 1.0f)
            {
                SetInfoText();

                frameAccumulator   = 0.0f;
                _interpolatedSteps = 0;
                clock.Reset();
            }

            if (PhysicsContext.World != null)
            {
                clock.StartPhysics();
                int steps = PhysicsContext.World.StepSimulation(FrameDelta);
                clock.StopPhysics();
                if (steps == 0)
                {
                    _interpolatedSteps++;
                }
            }

            if (Freelook.Update(FrameDelta))
            {
                UpdateView();
            }

            Input.ClearKeyCache();
        }
Пример #2
0
        public virtual void OnUpdate()
        {
            _frameDelta       = clock.Update();
            frameAccumulator += _frameDelta;
            ++frameCount;
            if (frameAccumulator >= 1.0f)
            {
                FramesPerSecond = frameCount / frameAccumulator;

                frameAccumulator = 0.0f;
                frameCount       = 0;
            }

            if (_world != null)
            {
                _world.StepSimulation(_frameDelta);
            }

            if (Freelook.Update(_frameDelta))
            {
                Graphics.UpdateView();
            }

            Input.ClearKeyCache();
        }
Пример #3
0
        public void OnUpdate()
        {
            FrameDelta         = Clock.GetFrameDelta();
            _frameAccumulator += FrameDelta;
            if (_frameAccumulator >= 1.0f)
            {
                FramesPerSecond = Clock.FrameCount / _frameAccumulator;
                SetInfoText();

                _frameAccumulator = 0.0f;
                Clock.Reset();
            }

            if (_updateReceiver != null)
            {
                _updateReceiver.Update(this);
            }
            HandleKeyboardInput();
            _bodyPicker.Update();

            Clock.StartPhysics();
            int substepsPassed = Simulation.World.StepSimulation(FrameDelta);

            Clock.StopPhysics(substepsPassed);

            if (FreeLook.Update(FrameDelta))
            {
                Graphics.UpdateView();
            }

            Input.ClearKeyCache();
        }
Пример #4
0
        public virtual void OnUpdate()
        {
            FrameDelta         = Clock.GetFrameDelta();
            _frameAccumulator += FrameDelta;
            if (_frameAccumulator >= 1.0f)
            {
                FramesPerSecond = Clock.FrameCount / _frameAccumulator;
                SetInfoText();

                _frameAccumulator = 0.0f;
                Clock.Reset();
            }

            if (World != null)
            {
                Clock.StartPhysics();
                World.StepSimulation(FrameDelta);
                Clock.StopPhysics();
            }

            if (Freelook.Update(FrameDelta))
            {
                Graphics.UpdateView();
            }

            Input.ClearKeyCache();
        }
Пример #5
0
        public virtual void OnUpdate()
        {
            _frameDelta       = clock.GetFrameDelta();
            frameAccumulator += _frameDelta;
            if (frameAccumulator >= 1.0f)
            {
                FramesPerSecond = clock.FrameCount / frameAccumulator;

                frameAccumulator = 0.0f;
                clock.Reset();
            }

            if (_world != null)
            {
                clock.StartPhysics();
                _world.StepSimulation(_frameDelta);
                clock.StopPhysics();
            }

            if (Freelook.Update(_frameDelta))
            {
                Graphics.UpdateView();
            }

            Input.ClearKeyCache();
        }
Пример #6
0
        /// <summary>
        /// Runs the demo.
        /// </summary>
        public void Run()
        {
            bool isFormClosed   = false;
            bool formIsResizing = false;

            Form = new RenderForm();

            /*
             * currentFormWindowState = Form.WindowState;
             * Form.Resize += (o, args) =>
             * {
             *  if (Form.WindowState != currentFormWindowState)
             *  {
             *      if (togglingFullScreen == false)
             *          HandleResize(o, args);
             *  }
             *
             *  currentFormWindowState = Form.WindowState;
             * };
             */
            Form.ResizeBegin += (o, args) => { formIsResizing = true; };
            Form.ResizeEnd   += (o, args) =>
            {
                Width  = Form.ClientSize.Width;
                Height = Form.ClientSize.Height;

                renderView.Dispose();
                depthView.Dispose();
                _swapChain.ResizeBuffers(_swapChain.Description.BufferCount, Width, Height, _swapChain.Description.ModeDescription.Format, _swapChain.Description.Flags);

                CreateBuffers();

                SetSceneConstants();
                formIsResizing = false;
            };

            //Form.Closed += (o, args) => { isFormClosed = true; };

            Input = new Input(Form);

            Width            = 1024;
            Height           = 768;
            FullScreenWidth  = Screen.PrimaryScreen.Bounds.Width;
            FullScreenHeight = Screen.PrimaryScreen.Bounds.Height;
            NearPlane        = 1.0f;
            FarPlane         = 200.0f;

            FieldOfView = (float)Math.PI / 4;
            Freelook    = new FreeLook(Input);
            ambient     = new Color4(Color.Gray.ToArgb());

            try
            {
                OnInitializeDevice();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Could not create DirectX 10 device.");
                return;
            }

            Initialize();

            clock.Start();
            RenderLoop.Run(Form, () =>
            {
                OnHandleInput();
                Update();
                Render();
                Input.ClearKeyCache();
            });
        }
Пример #7
0
        /// <summary>
        /// Runs the game.
        /// </summary>
        public void Run()
        {
            bool isFormClosed   = false;
            bool formIsResizing = false;

            Form = new RenderForm();

            currentFormWindowState = Form.WindowState;
            Form.Resize           += (o, args) =>
            {
                if (Form.WindowState != currentFormWindowState)
                {
                    if (togglingFullScreen == false)
                    {
                        HandleResize(o, args);
                    }
                }

                currentFormWindowState = Form.WindowState;
            };

            Form.ResizeBegin += (o, args) => { formIsResizing = true; };
            Form.ResizeEnd   += (o, args) =>
            {
                formIsResizing = false;
                HandleResize(o, args);
            };

            Form.Closed += (o, args) => { isFormClosed = true; };


            // initialize input
            SlimDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.Keyboard, DeviceFlags.None);
            SlimDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.Mouse, DeviceFlags.None);

            Input = new Input(Form);

            SlimDX.RawInput.Device.KeyboardInput += Input.Device_KeyboardInput;
            SlimDX.RawInput.Device.MouseInput    += Input.Device_MouseInput;


            Width            = 1024;
            Height           = 768;
            FullScreenWidth  = Screen.PrimaryScreen.Bounds.Width;
            FullScreenHeight = Screen.PrimaryScreen.Bounds.Height;
            NearPlane        = 0.1f;
            FarPlane         = 200f;
            FieldOfView      = (float)Math.PI / 4;
            Freelook         = new FreeLook(Input);
            Ambient          = Color.Gray.ToArgb();

            OnInitializeDevice();

            Fps         = new FpsDisplay(Device);
            MeshFactory = new GraphicObjectFactory(Device);

            OnInitialize();
            OnResetDevice();

            clock.Start();
            MessagePump.Run(Form, () =>
            {
                OnHandleInput();
                Update();
                Input.ClearKeyCache();

                if (isFormClosed)
                {
                    return;
                }

                if (!formIsResizing)
                {
                    Render();
                }
            });

            SlimDX.RawInput.Device.KeyboardInput -= Input.Device_KeyboardInput;
            SlimDX.RawInput.Device.MouseInput    -= Input.Device_MouseInput;

            OnLostDevice();
        }
Пример #8
0
        public virtual void OnHandleInput()
        {
            if (Input.KeysPressed.Count != 0)
            {
                switch (Input.KeysPressed[0])
                {
                case Keys.Escape:
                case Keys.Q:
                    Graphics.Form.Close();
                    return;

                case Keys.F3:
                    IsDebugDrawEnabled = !IsDebugDrawEnabled;
                    break;

                case Keys.F8:
                    Input.ClearKeyCache();
                    GraphicsLibraryManager.ExitWithReload = true;
                    Graphics.Form.Close();
                    break;

                case Keys.F11:
                    Graphics.IsFullScreen = !Graphics.IsFullScreen;
                    break;

                case (Keys.Control | Keys.F):
                    const int         maxSerializeBufferSize = 1024 * 1024 * 5;
                    DefaultSerializer serializer             = new DefaultSerializer(maxSerializeBufferSize);
                    World.Serialize(serializer);

                    byte[] dataBytes = new byte[serializer.CurrentBufferSize];
                    Marshal.Copy(serializer.BufferPointer, dataBytes, 0, dataBytes.Length);

                    System.IO.FileStream file = new System.IO.FileStream("world.bullet", System.IO.FileMode.Create);
                    file.Write(dataBytes, 0, dataBytes.Length);
                    file.Dispose();
                    break;

                case Keys.G:
                    //shadowsEnabled = !shadowsEnabled;
                    break;

                case Keys.Space:
                    ShootBox(Freelook.Eye, GetRayTo(Input.MousePoint, Freelook.Eye, Freelook.Target, Graphics.FieldOfView));
                    break;

                case Keys.Return:
                    ClientResetScene();
                    break;
                }
            }

            if (Input.MousePressed != MouseButtons.None)
            {
                Vector3 rayTo = GetRayTo(Input.MousePoint, Freelook.Eye, Freelook.Target, Graphics.FieldOfView);

                if (Input.MousePressed == MouseButtons.Right)
                {
                    if (_world != null)
                    {
                        Vector3 rayFrom = Freelook.Eye;

                        ClosestRayResultCallback rayCallback = new ClosestRayResultCallback(ref rayFrom, ref rayTo);
                        _world.RayTestRef(ref rayFrom, ref rayTo, rayCallback);
                        if (rayCallback.HasHit)
                        {
                            RigidBody body = rayCallback.CollisionObject as RigidBody;
                            if (body != null)
                            {
                                if (!(body.IsStaticObject || body.IsKinematicObject))
                                {
                                    pickedBody = body;
                                    pickedBody.ActivationState = ActivationState.DisableDeactivation;

                                    Vector3 pickPos    = rayCallback.HitPointWorld;
                                    Vector3 localPivot = Vector3.TransformCoordinate(pickPos, Matrix.Invert(body.CenterOfMassTransform));

                                    if (Input.KeysDown.Contains(Keys.ShiftKey))
                                    {
                                        Generic6DofConstraint dof6 = new Generic6DofConstraint(body, Matrix.Translation(localPivot), false)
                                        {
                                            LinearLowerLimit  = Vector3.Zero,
                                            LinearUpperLimit  = Vector3.Zero,
                                            AngularLowerLimit = Vector3.Zero,
                                            AngularUpperLimit = Vector3.Zero
                                        };

                                        _world.AddConstraint(dof6);
                                        pickConstraint = dof6;

                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 0);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 1);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 2);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 3);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 4);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 5);

                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 0);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 1);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 2);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 3);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 4);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 5);
                                    }
                                    else
                                    {
                                        Point2PointConstraint p2p = new Point2PointConstraint(body, localPivot);
                                        _world.AddConstraint(p2p);
                                        pickConstraint           = p2p;
                                        p2p.Setting.ImpulseClamp = 30;
                                        //very weak constraint for picking
                                        p2p.Setting.Tau = 0.001f;

                                        /*
                                         * p2p.SetParam(ConstraintParams.Cfm, 0.8f, 0);
                                         * p2p.SetParam(ConstraintParams.Cfm, 0.8f, 1);
                                         * p2p.SetParam(ConstraintParams.Cfm, 0.8f, 2);
                                         * p2p.SetParam(ConstraintParams.Erp, 0.1f, 0);
                                         * p2p.SetParam(ConstraintParams.Erp, 0.1f, 1);
                                         * p2p.SetParam(ConstraintParams.Erp, 0.1f, 2);
                                         */
                                    }

                                    oldPickingDist = (pickPos - rayFrom).Length;
                                }
                            }
                        }
                        rayCallback.Dispose();
                    }
                }
            }
            else if (Input.MouseReleased == MouseButtons.Right)
            {
                RemovePickingConstraint();
            }

            // Mouse movement
            if (Input.MouseDown == MouseButtons.Right)
            {
                if (pickConstraint != null)
                {
                    Vector3 newRayTo = GetRayTo(Input.MousePoint, Freelook.Eye, Freelook.Target, Graphics.FieldOfView);

                    if (pickConstraint.ConstraintType == TypedConstraintType.D6)
                    {
                        Generic6DofConstraint pickCon = pickConstraint as Generic6DofConstraint;

                        //keep it at the same picking distance
                        Vector3 rayFrom = Freelook.Eye;
                        Vector3 dir     = newRayTo - rayFrom;
                        dir.Normalize();
                        dir *= oldPickingDist;
                        Vector3 newPivotB = rayFrom + dir;

                        Matrix tempFrameOffsetA = pickCon.FrameOffsetA;
                        tempFrameOffsetA.M41 = newPivotB.X;
                        tempFrameOffsetA.M42 = newPivotB.Y;
                        tempFrameOffsetA.M43 = newPivotB.Z;
                        pickCon.SetFrames(tempFrameOffsetA, pickCon.FrameOffsetB);
                    }
                    else
                    {
                        Point2PointConstraint pickCon = pickConstraint as Point2PointConstraint;

                        //keep it at the same picking distance
                        Vector3 rayFrom = Freelook.Eye;
                        Vector3 dir     = newRayTo - rayFrom;
                        dir.Normalize();
                        dir *= oldPickingDist;
                        pickCon.PivotInB = rayFrom + dir;
                    }
                }
            }
        }
Пример #9
0
        private void HandleKeyboardInput()
        {
            if (Input.KeysPressed.Count == 0)
            {
                return;
            }

            switch (Input.KeysPressed[0])
            {
            case Keys.Escape:
            case Keys.Q:
                Graphics.Form.Close();
                return;

            case Keys.F1:
                MessageBox.Show(
                    "WASD + Shift\tMove\n" +
                    "Left click\t\tPoint camera\n" +
                    "Right click\tPick up an object using a Point2PointConstraint\n" +
                    "Shift + Right click\tPick up an object using a fixed constraint\n" +
                    "Space\t\tShoot box\n" +
                    "Return\t\tReset\n" +
                    "F11\t\tFullscreen\n" +
                    "Q\t\tQuit\n\n",
                    "Help");
                // Key release won't be captured
                Input.KeysDown.Remove(Keys.F1);
                break;

            case Keys.F3:
                IsDebugDrawEnabled = !IsDebugDrawEnabled;
                break;

            case Keys.F5:
                var debugForm = new DebugInfoForm(this);
                debugForm.Show();
                break;

            case Keys.F8:
                Input.ClearKeyCache();
                GraphicsLibraryManager.ExitWithReload = true;
                Graphics.Form.Close();
                break;

            case Keys.F11:
                Graphics.IsFullScreen = !Graphics.IsFullScreen;
                break;

            case (Keys.Control | Keys.F):
                const int maxSerializeBufferSize = 1024 * 1024 * 5;
                using (var serializer = new DefaultSerializer(maxSerializeBufferSize))
                {
                    Simulation.World.Serialize(serializer);
                    var dataBytes = new byte[serializer.CurrentBufferSize];
                    Marshal.Copy(serializer.BufferPointer, dataBytes, 0,
                                 dataBytes.Length);
                    using (var file = new System.IO.FileStream("world.bullet", System.IO.FileMode.Create))
                    {
                        file.Write(dataBytes, 0, dataBytes.Length);
                    }
                }
                break;

            case Keys.G:
                //shadowsEnabled = !shadowsEnabled;
                break;

            case Keys.Space:
                Vector3 destination = GetCameraRayTo();
                _boxShooter.Shoot(FreeLook.Eye, GetCameraRayTo());
                break;

            case Keys.Return:
                ResetScene();
                break;
            }
        }
Пример #10
0
        public virtual void OnHandleInput()
        {
            if (Input.KeysPressed.Count != 0)
            {
                switch (Input.KeysPressed[0])
                {
                case Keys.Escape:
                case Keys.Q:
                    Graphics.Form.Close();
                    return;

                case Keys.F1:
                    MessageBox.Show(
                        "Move using WASD + shift\n" +
                        "Left click - point camera\n" +
                        "Right click - pick up an object using a Point2PointConstraint\n" +
                        "Right click + shift - pick up an object using a fixed Generic6DofConstraint\n" +
                        "Space - shoot box\n" +
                        "Q - quit\n" +
                        Graphics.InfoText,
                        "Help");
                    // Key release won't be captured
                    Input.KeysDown.Remove(Keys.F1);
                    break;

                case Keys.F3:
                    IsDebugDrawEnabled = !IsDebugDrawEnabled;
                    break;

                case Keys.F8:
                    Input.ClearKeyCache();
                    GraphicsLibraryManager.ExitWithReload = true;
                    Graphics.Form.Close();
                    break;

                case Keys.F11:
                    Graphics.IsFullScreen = !Graphics.IsFullScreen;
                    break;

                case (Keys.Control | Keys.F):
                    const int maxSerializeBufferSize = 1024 * 1024 * 5;
                    using (var serializer = new DefaultSerializer(maxSerializeBufferSize))
                    {
                        World.Serialize(serializer);
                        byte[] dataBytes = new byte[serializer.CurrentBufferSize];
                        System.Runtime.InteropServices.Marshal.Copy(serializer.BufferPointer, dataBytes, 0,
                                                                    dataBytes.Length);
                        using (var file = new System.IO.FileStream("world.bullet", System.IO.FileMode.Create))
                        {
                            file.Write(dataBytes, 0, dataBytes.Length);
                        }
                    }
                    break;

                case Keys.G:
                    //shadowsEnabled = !shadowsEnabled;
                    break;

                case Keys.Space:
                    ShootBox(Freelook.Eye, GetCameraRayTo());
                    break;

                case Keys.Return:
                    ClientResetScene();
                    break;
                }
            }

            _bodyPicker.Update();
        }
Пример #11
0
        public virtual void OnHandleInput()
        {
            if (Input.KeysPressed.Count != 0)
            {
                switch (Input.KeysPressed[0])
                {
                case Keys.Escape:
                case Keys.Q:
                    Graphics.Form.Close();
                    return;

                case Keys.F1:
                    MessageBox.Show(
                        "Move using WASD + shift\n" +
                        "Left click - point camera\n" +
                        "Right click - pick up an object using a Point2PointConstraint\n" +
                        "Right click + shift - pick up an object using a fixed Generic6DofConstraint\n" +
                        "Space - shoot box\n" +
                        "Q - quit\n" +
                        Graphics.InfoText,
                        "Help");
                    // Key release won't be captured
                    Input.KeysDown.Remove(Keys.F1);
                    break;

                case Keys.F3:
                    IsDebugDrawEnabled = !IsDebugDrawEnabled;
                    break;

                case Keys.F8:
                    Input.ClearKeyCache();
                    GraphicsLibraryManager.ExitWithReload = true;
                    Graphics.Form.Close();
                    break;

                case Keys.F11:
                    Graphics.IsFullScreen = !Graphics.IsFullScreen;
                    break;

                case (Keys.Control | Keys.F):
                    const int maxSerializeBufferSize = 1024 * 1024 * 5;
                    using (var serializer = new DefaultSerializer(maxSerializeBufferSize))
                    {
                        World.Serialize(serializer);
                        byte[] dataBytes = new byte[serializer.CurrentBufferSize];
                        System.Runtime.InteropServices.Marshal.Copy(serializer.BufferPointer, dataBytes, 0,
                                                                    dataBytes.Length);
                        using (var file = new System.IO.FileStream("world.bullet", System.IO.FileMode.Create))
                        {
                            file.Write(dataBytes, 0, dataBytes.Length);
                        }
                    }
                    break;

                case Keys.G:
                    //shadowsEnabled = !shadowsEnabled;
                    break;

                case Keys.Space:
                    ShootBox(Freelook.Eye, GetRayTo(Input.MousePoint, Freelook.Eye, Freelook.Target, Graphics.FieldOfView));
                    break;

                case Keys.Return:
                    ClientResetScene();
                    break;
                }
            }

            if (Input.MousePressed != MouseButtons.None)
            {
                Vector3 rayTo = GetRayTo(Input.MousePoint, Freelook.Eye, Freelook.Target, Graphics.FieldOfView);

                if (Input.MousePressed == MouseButtons.Right)
                {
                    if (World != null)
                    {
                        Vector3 rayFrom = Freelook.Eye;

                        ClosestRayResultCallback rayCallback = new ClosestRayResultCallback(ref rayFrom, ref rayTo);
                        World.RayTest(ref rayFrom, ref rayTo, rayCallback);
                        if (rayCallback.HasHit)
                        {
                            Vector3   pickPos = rayCallback.HitPointWorld;
                            RigidBody body    = rayCallback.CollisionObject as RigidBody;
                            if (body != null)
                            {
                                if (!(body.IsStaticObject || body.IsKinematicObject))
                                {
                                    pickedBody = body;
                                    pickedBody.ActivationState = ActivationState.DisableDeactivation;

                                    Vector3 localPivot = Vector3.TransformCoordinate(pickPos, Matrix.Invert(body.CenterOfMassTransform));

                                    if (Input.KeysDown.Contains(Keys.ShiftKey))
                                    {
                                        Generic6DofConstraint dof6 = new Generic6DofConstraint(body, Matrix.Translation(localPivot), false)
                                        {
                                            LinearLowerLimit  = Vector3.Zero,
                                            LinearUpperLimit  = Vector3.Zero,
                                            AngularLowerLimit = Vector3.Zero,
                                            AngularUpperLimit = Vector3.Zero
                                        };

                                        World.AddConstraint(dof6);
                                        pickConstraint = dof6;

                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 0);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 1);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 2);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 3);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 4);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 5);

                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 0);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 1);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 2);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 3);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 4);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 5);
                                    }
                                    else
                                    {
                                        Point2PointConstraint p2p = new Point2PointConstraint(body, localPivot);
                                        World.AddConstraint(p2p);
                                        pickConstraint           = p2p;
                                        p2p.Setting.ImpulseClamp = 30;
                                        //very weak constraint for picking
                                        p2p.Setting.Tau = 0.001f;

                                        /*
                                         * p2p.SetParam(ConstraintParams.Cfm, 0.8f, 0);
                                         * p2p.SetParam(ConstraintParams.Cfm, 0.8f, 1);
                                         * p2p.SetParam(ConstraintParams.Cfm, 0.8f, 2);
                                         * p2p.SetParam(ConstraintParams.Erp, 0.1f, 0);
                                         * p2p.SetParam(ConstraintParams.Erp, 0.1f, 1);
                                         * p2p.SetParam(ConstraintParams.Erp, 0.1f, 2);
                                         */
                                    }
                                }
                            }
                            else
                            {
                                MultiBodyLinkCollider multiCol = rayCallback.CollisionObject as MultiBodyLinkCollider;
                                if (multiCol != null && multiCol.MultiBody != null)
                                {
                                    MultiBody mb = multiCol.MultiBody;

                                    prevCanSleep = mb.CanSleep;
                                    mb.CanSleep  = false;
                                    Vector3 pivotInA = mb.WorldPosToLocal(multiCol.Link, pickPos);

                                    MultiBodyPoint2Point p2p = new MultiBodyPoint2Point(mb, multiCol.Link, null, pivotInA, pickPos);
                                    p2p.MaxAppliedImpulse = 2;

                                    (World as MultiBodyDynamicsWorld).AddMultiBodyConstraint(p2p);
                                    pickingMultiBodyPoint2Point = p2p;
                                }
                            }
                            oldPickingDist = (pickPos - rayFrom).Length;
                        }
                        rayCallback.Dispose();
                    }
                }
            }
            else if (Input.MouseReleased == MouseButtons.Right)
            {
                RemovePickingConstraint();
            }

            // Mouse movement
            if (Input.MouseDown == MouseButtons.Right)
            {
                MovePickedBody();
            }
        }
Пример #12
0
        /// <summary>
        /// Runs the game.
        /// </summary>
        public void Run()
        {
            bool isFormClosed = false;
            bool formIsResizing = false;

            Form = new RenderForm();

            currentFormWindowState = Form.WindowState;
            Form.Resize += (o, args) =>
            {
                if (Form.WindowState != currentFormWindowState)
                {
                    if (togglingFullScreen == false)
                        HandleResize(o, args);
                }

                currentFormWindowState = Form.WindowState;
            };

            Form.ResizeBegin += (o, args) => { formIsResizing = true; };
            Form.ResizeEnd += (o, args) =>
            {
                formIsResizing = false;
                HandleResize(o, args);
            };

            Form.Closed += (o, args) => { isFormClosed = true; };

            // initialize input
            SlimDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.Keyboard, DeviceFlags.None);
            SlimDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.Mouse, DeviceFlags.None);

            Input = new Input(Form);

            SlimDX.RawInput.Device.KeyboardInput += Input.Device_KeyboardInput;
            SlimDX.RawInput.Device.MouseInput += Input.Device_MouseInput;

            Width = 1024;
            Height = 768;
            FullScreenWidth = Screen.PrimaryScreen.Bounds.Width;
            FullScreenHeight = Screen.PrimaryScreen.Bounds.Height;
            NearPlane = 0.1f;
            FarPlane = 200f;
            FieldOfView = (float)Math.PI / 4;
            Freelook = new FreeLook(Input);
            Ambient = Color.Gray.ToArgb();

            OnInitializeDevice();

            Fps = new FpsDisplay(Device);
            MeshFactory = new GraphicObjectFactory(Device);

            OnInitialize();
            OnResetDevice();

            clock.Start();
            MessagePump.Run(Form, () =>
            {
                OnHandleInput();
                Update();
                Input.ClearKeyCache();

                if (isFormClosed)
                    return;

                if (!formIsResizing)
                    Render();
            });

            SlimDX.RawInput.Device.KeyboardInput -= Input.Device_KeyboardInput;
            SlimDX.RawInput.Device.MouseInput -= Input.Device_MouseInput;

            OnLostDevice();
        }
Пример #13
0
        /// <summary>
        /// Runs the demo.
        /// </summary>
        public void Run()
        {
            bool isFormClosed = false;
            bool formIsResizing = false;

            Form = new RenderForm();
            /*
            currentFormWindowState = Form.WindowState;
            Form.Resize += (o, args) =>
            {
                if (Form.WindowState != currentFormWindowState)
                {
                    if (togglingFullScreen == false)
                        HandleResize(o, args);
                }

                currentFormWindowState = Form.WindowState;
            };
            */
            Form.ResizeBegin += (o, args) => { formIsResizing = true; };
            Form.ResizeEnd += (o, args) =>
            {
                Width = Form.ClientSize.Width;
                Height = Form.ClientSize.Height;

                renderView.Dispose();
                depthView.Dispose();
                _swapChain.ResizeBuffers(_swapChain.Description.BufferCount, Width, Height, _swapChain.Description.ModeDescription.Format, _swapChain.Description.Flags);

                CreateBuffers();

                SetSceneConstants();
                formIsResizing = false;
            };

            //Form.Closed += (o, args) => { isFormClosed = true; };

            Input = new Input(Form);

            Width = 1024;
            Height = 768;
            FullScreenWidth = Screen.PrimaryScreen.Bounds.Width;
            FullScreenHeight = Screen.PrimaryScreen.Bounds.Height;
            NearPlane = 1.0f;
            FarPlane = 200.0f;

            FieldOfView = (float)Math.PI / 4;
            Freelook = new FreeLook(Input);
            ambient = new Color4(Color.Gray.ToArgb());

            try
            {
                OnInitializeDevice();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Could not create DirectX 10 device.");
                return;
            }

            Initialize();

            clock.Start();
            RenderLoop.Run(Form, () =>
            {
                OnHandleInput();
                Update();
                Render();
                Input.ClearKeyCache();
            });
        }
Пример #14
0
        public virtual void OnHandleInput()
        {
            if (Input.KeysPressed.Count != 0)
            {
                switch (Input.KeysPressed[0])
                {
                case Keys.Escape:
                case Keys.Q:
                    Graphics.Form.Close();
                    return;

                case Keys.F3:
                    IsDebugDrawEnabled = !IsDebugDrawEnabled;
                    break;

                case Keys.F8:
                    Input.ClearKeyCache();
                    GraphicsLibraryManager.ExitWithReload = true;
                    Graphics.Form.Close();
                    break;

                case Keys.F11:
                    Graphics.IsFullScreen = !Graphics.IsFullScreen;
                    break;

                case Keys.G:
                    //shadowsEnabled = !shadowsEnabled;
                    break;

                case Keys.Space:
                    ShootBox(Freelook.Eye, GetRayTo(Input.MousePoint, Freelook.Eye, Freelook.Target, Graphics.FieldOfView));
                    break;

                case Keys.Return:
                    ClientResetScene();
                    break;
                }
            }

            if (Input.MousePressed != MouseButtons.None)
            {
                Vector3 rayTo = GetRayTo(Input.MousePoint, Freelook.Eye, Freelook.Target, Graphics.FieldOfView);

                if (Input.MousePressed == MouseButtons.Right)
                {
                    if (_world != null)
                    {
                        Vector3 rayFrom = Freelook.Eye;

                        ClosestRayResultCallback rayCallback = new ClosestRayResultCallback(ref rayFrom, ref rayTo);
                        _world.RayTest(ref rayFrom, ref rayTo, rayCallback);
                        if (rayCallback.HasHit)
                        {
                            Vector3   pickPos = rayCallback.HitPointWorld;
                            RigidBody body    = rayCallback.CollisionObject as RigidBody;
                            if (body != null)
                            {
                                if (!(body.IsStaticObject || body.IsKinematicObject))
                                {
                                    pickedBody = body;
                                    pickedBody.ActivationState = ActivationState.DisableDeactivation;

                                    Vector3 localPivot = Vector3.TransformCoordinate(pickPos, Matrix.Invert(body.CenterOfMassTransform));

                                    if (Input.KeysDown.Contains(Keys.ShiftKey))
                                    {
                                        Generic6DofConstraint dof6 = new Generic6DofConstraint(body, Matrix.Translation(localPivot), false)
                                        {
                                            LinearLowerLimit  = Vector3.Zero,
                                            LinearUpperLimit  = Vector3.Zero,
                                            AngularLowerLimit = Vector3.Zero,
                                            AngularUpperLimit = Vector3.Zero
                                        };

                                        _world.AddConstraint(dof6);
                                        pickConstraint = dof6;

                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 0);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 1);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 2);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 3);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 4);
                                        dof6.SetParam(ConstraintParam.StopCfm, 0.8f, 5);

                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 0);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 1);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 2);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 3);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 4);
                                        dof6.SetParam(ConstraintParam.StopErp, 0.1f, 5);
                                    }
                                    else
                                    {
                                        Point2PointConstraint p2p = new Point2PointConstraint(body, localPivot);
                                        _world.AddConstraint(p2p);
                                        pickConstraint           = p2p;
                                        p2p.Setting.ImpulseClamp = 30;
                                        //very weak constraint for picking
                                        p2p.Setting.Tau = 0.001f;

                                        /*
                                         * p2p.SetParam(ConstraintParams.Cfm, 0.8f, 0);
                                         * p2p.SetParam(ConstraintParams.Cfm, 0.8f, 1);
                                         * p2p.SetParam(ConstraintParams.Cfm, 0.8f, 2);
                                         * p2p.SetParam(ConstraintParams.Erp, 0.1f, 0);
                                         * p2p.SetParam(ConstraintParams.Erp, 0.1f, 1);
                                         * p2p.SetParam(ConstraintParams.Erp, 0.1f, 2);
                                         */
                                    }
                                }
                            }
                            else
                            {
                                MultiBodyLinkCollider multiCol = rayCallback.CollisionObject as MultiBodyLinkCollider;
                                if (multiCol != null && multiCol.MultiBody != null)
                                {
                                    MultiBody mb = multiCol.MultiBody;

                                    prevCanSleep = mb.CanSleep;
                                    mb.CanSleep  = false;
                                    Vector3 pivotInA = mb.WorldPosToLocal(multiCol.Link, pickPos);

                                    MultiBodyPoint2Point p2p = new MultiBodyPoint2Point(mb, multiCol.Link, null, pivotInA, pickPos);
                                    p2p.MaxAppliedImpulse = 2;

                                    (World as MultiBodyDynamicsWorld).AddMultiBodyConstraint(p2p);
                                    pickingMultiBodyPoint2Point = p2p;
                                }
                            }
                            oldPickingDist = (pickPos - rayFrom).Length;
                        }
                        rayCallback.Dispose();
                    }
                }
            }
            else if (Input.MouseReleased == MouseButtons.Right)
            {
                RemovePickingConstraint();
            }

            // Mouse movement
            if (Input.MouseDown == MouseButtons.Right)
            {
                MovePickedBody();
            }
        }