示例#1
0
        public override void OnHandleInput()
        {
            Matrix transform = _ghostObject.WorldTransform;

            Vector3 forwardDir = new Vector3(transform.M31, transform.M32, transform.M33);

            forwardDir.Normalize();
            //Console.WriteLine("forwardDir={0}", forwardDir);

            Vector3 upDir = new Vector3(transform.M21, transform.M22, transform.M23);

            upDir.Normalize();

            Vector3     walkDirection = Vector3.Zero;
            const float walkVelocity  = 1.1f * 4.0f;
            float       walkSpeed     = walkVelocity * FrameDelta * 10;// * 0.0001f;
            float       turnSpeed     = FrameDelta * 3;
            Vector3     position      = transform.Origin;

            if (Input.KeysDown.Contains(Keys.Left))
            {
                transform.Origin            = Vector3.Zero;
                transform                  *= Matrix.RotationAxis(upDir, -turnSpeed);
                transform.Origin            = position;
                _ghostObject.WorldTransform = transform;
            }
            if (Input.KeysDown.Contains(Keys.Right))
            {
                transform.Origin            = Vector3.Zero;
                transform                  *= Matrix.RotationAxis(upDir, turnSpeed);
                transform.Origin            = position;
                _ghostObject.WorldTransform = transform;
            }

            if (Input.KeysDown.Contains(Keys.Up))
            {
                walkDirection += forwardDir;
            }
            if (Input.KeysDown.Contains(Keys.Down))
            {
                walkDirection -= forwardDir;
            }

            Vector3 cameraPos = position - forwardDir * 12 + upDir * 5;

            //use the convex sweep test to find a safe position for the camera (not blocked by static geometry)
            _convexResultCallback.ConvexFromWorld    = position;
            _convexResultCallback.ConvexToWorld      = cameraPos;
            _convexResultCallback.ClosestHitFraction = 1.0f;
            World.ConvexSweepTest(_cameraSphere, Matrix.Translation(position), Matrix.Translation(cameraPos), _convexResultCallback);
            if (_convexResultCallback.HasHit)
            {
                cameraPos = Vector3.Lerp(position, cameraPos, _convexResultCallback.ClosestHitFraction);
            }
            Freelook.Eye    = cameraPos;
            Freelook.Target = position;

            _character.SetWalkDirection(walkDirection * walkSpeed);

            if (Input.KeysDown.Contains(Keys.Space))
            {
                _character.Jump();
                return;
            }

            base.OnHandleInput();
        }
示例#2
0
 public void Jump()
 {
     m_characterController.Jump();
 }
        public override void OnHandleInput()
        {
            Matrix xform = ghostObject.WorldTransform;

            Vector3 forwardDir = new Vector3(xform.M31, xform.M32, xform.M33);
            //Console.Write("forwardDir={0},{1},{2}\n", forwardDir[0], forwardDir[1], forwardDir[2]);
            Vector3 upDir = new Vector3(xform.M21, xform.M22, xform.M23);

            forwardDir.Normalize();
            upDir.Normalize();
            Vector3 pos = xform.Origin;

            Vector3     walkDirection = Vector3.Zero;
            const float walkVelocity  = 1.1f * 4.0f;
            float       walkSpeed     = walkVelocity * FrameDelta * 10;// * 0.0001f;
            float       turnSpeed     = FrameDelta * 3;

            if (Input.KeysDown.Contains(Keys.Left))
            {
                Matrix orn = xform;
                orn.Row4 = new Vector4(0, 0, 0, 1);
                orn     *= Matrix.RotationAxis(upDir, -turnSpeed);
                orn.Row4 = new Vector4(pos.X, pos.Y, pos.Z, 1);
                ghostObject.WorldTransform = orn;
            }
            if (Input.KeysDown.Contains(Keys.Right))
            {
                Matrix orn = xform;
                orn.Row4 = new Vector4(0, 0, 0, 1);
                orn     *= Matrix.RotationAxis(upDir, turnSpeed);
                orn.Row4 = new Vector4(pos.X, pos.Y, pos.Z, 1);
                ghostObject.WorldTransform = orn;
            }

            if (Input.KeysDown.Contains(Keys.Up))
            {
                walkDirection += forwardDir;
            }
            if (Input.KeysDown.Contains(Keys.Down))
            {
                walkDirection -= forwardDir;
            }

            Vector3 cameraPos = pos - forwardDir * 12 + upDir * 5;

            //use the convex sweep test to find a safe position for the camera (not blocked by static geometry)
            convexResultCallback.ConvexFromWorld    = pos;
            convexResultCallback.ConvexToWorld      = cameraPos;
            convexResultCallback.ClosestHitFraction = 1.0f;
            World.ConvexSweepTest(cameraSphere, Matrix.Translation(pos), Matrix.Translation(cameraPos), convexResultCallback);
            if (convexResultCallback.HasHit)
            {
                cameraPos = Vector3.Lerp(pos, cameraPos, convexResultCallback.ClosestHitFraction);
            }
            Freelook.SetEyeTarget(cameraPos, pos);

            character.SetWalkDirection(walkDirection * walkSpeed);

            if (Input.KeysDown.Contains(Keys.Space))
            {
                character.Jump();
                return;
            }

            base.OnHandleInput();
        }
示例#4
0
 protected void Jump()
 {
     _charController.Jump();
 }