Exemplo n.º 1
0
        private void Move()
        {
            if (!FreeCam.FreeCTRL)
            {
                Vector3D walkdir = Vector3D.Zero;

                Vector3D lookDir = FPSCamera.WObject.Forward;

                Vector3D walkForward = new Vector3D(lookDir.X, 0, lookDir.Z).Normalize();

                Vector3D rightDir = FPSCamera.WObject.Right;

                Vector3D walkRight = new Vector3D(rightDir.X, 0, rightDir.Z).Normalize();

                bool run = false;



                if (Input.IsPressed(GameInput.Key("Forward")))
                {
                    if (Input.IsPressed(GameInput.Key("Run")))
                    {
                        run = true;
                    }
                    walkdir += walkForward;
                }
                if (Input.IsPressed(GameInput.Key("Backward")))
                {
                    walkdir -= walkForward;
                }

                if (Input.IsPressed(GameInput.Key("Right")))
                {
                    walkdir -= walkRight;
                }
                if (Input.IsPressed(GameInput.Key("Left")))
                {
                    walkdir += walkRight;
                }

                Vector3D walkDirBase = walkdir.Normalize();

                float force = 0.0F;
                CheckGrounded();
                if (Grounded)
                {
                    force = run ? RunForce : WalkForce;
                }
                else
                {
                    force = run ? AirRunForce : AirWalkForce;
                }

                this._Rb.Velocity += walkdir * force * Time.DeltaTime;

                float MaxHorizontalSpeed = 0.0F;

                if (Grounded)
                {
                    MaxHorizontalSpeed = run ? RunMaxSpeed : WalkMaxSpeed;
                }

                else
                {
                    MaxHorizontalSpeed = run ? AirRunMaxSpeed : AirWalkMaxSpeed;
                }


                if (this._Rb.Velocity.XZ.Length > MaxHorizontalSpeed)
                {
                    double velY = this._Rb.Velocity.Y;
                    this._Rb.Velocity = new Vector3D(this._Rb.Velocity.X, 0, this._Rb.Velocity.Z).Normalized *MaxHorizontalSpeed;

                    this._Rb.Velocity += Vector3D.Up * velY;
                }

                if (this._Rb.Velocity.Y < this.FallMaxSpeed)
                {
                    double fallSpeed = this._Rb.Velocity.Y;

                    this._Rb.Velocity *= new Vector3D(1, 0, 1);
                    this._Rb.Velocity += new Vector3D(0, fallSpeed, 0);
                }


                Vector3D flattenVel = new Vector3D(this._Rb.Velocity.X, 0, this._Rb.Velocity.Z);

                //slowdown
                if (force == 0.0F || walkDirBase == Vector3D.Zero)
                {
                    Vector2D flatVel = this._Rb.Velocity.XZ;

                    Vector3D horVel = new Vector3D(flatVel.X, 0, flatVel.Y);

                    if (flatVel.Length < 1.0F)
                    {
                        this._Rb.Velocity *= Vector3D.Up;
                    }

                    else
                    {
                        this._Rb.Velocity -= horVel.Normalized * SlowFactor * Time.DeltaTime;
                    }
                }

                else
                {
                    double angleVelFwd = Vector3D.Angle(walkDirBase, _Rb.Velocity.Normalized);

                    this._Rb.Velocity.RotateAround(Vector3D.Zero, Vector3D.Up, (float)angleVelFwd * TurnFactor * (float)Time.DeltaTime);
                }
            }
        }