Protogame.FirstPersonCameraComponent.ComputeWorldSpaceVectorFromLocalSpace C# (CSharp) Метод

ComputeWorldSpaceVectorFromLocalSpace() публичный Метод

Computes a world space directional vector based on a directional vector local to the camera, using the camera's view matrix. If you pass Vector3.Forward to this method, you'll get a world space vector that roughly translates to what is "forward" for the player.

If the camera is angled upward, this vector will include that upward component. For a value more suited towards movement calculations, see GetWorldSpaceLateralLookAtVector.

public ComputeWorldSpaceVectorFromLocalSpace ( Vector3 local ) : Vector3
local Vector3
Результат Vector3
        public Vector3 ComputeWorldSpaceVectorFromLocalSpace(Vector3 local)
        {
            var parentFinalTransform = (_node?.Parent?.UntypedValue as IHasTransform)?.FinalTransform;
            if (parentFinalTransform == null)
            {
                return Vector3.Forward;
            }

            var sourceLocal = HeadOffset;
            var lookAtLocal = HeadOffset + Vector3.Transform(local, Transform.LocalMatrix);
            return Vector3.Transform(lookAtLocal, parentFinalTransform.AbsoluteMatrix) -
                   Vector3.Transform(sourceLocal, parentFinalTransform.AbsoluteMatrix);
        }

Usage Example

Пример #1
0
        public bool Handle(ComponentizedEntity componentizedEntity, IGameContext gameContext,
                           IEventEngine <IGameContext> eventEngine, Event @event)
        {
            if (!Enabled)
            {
                return(false);
            }

            var gamepadEvent = @event as GamePadEvent;
            var keyHeldEvent = @event as KeyHeldEvent;
            var mouseEvent   = @event as MouseEvent;

            if (gamepadEvent != null)
            {
                _firstPersonCameraComponent.Yaw -= gamepadEvent.GamePadState.ThumbSticks.Right.X *
                                                   ThumbstickLookSensitivity;
                _firstPersonCameraComponent.Pitch += gamepadEvent.GamePadState.ThumbSticks.Right.Y *
                                                     ThumbstickLookSensitivity;

                var limit = MathHelper.PiOver2 - MathHelper.ToRadians(5);
                _firstPersonCameraComponent.Pitch = MathHelper.Clamp(_firstPersonCameraComponent.Pitch, -limit, limit);

                var lookAt = _firstPersonCameraComponent.Transform.LocalMatrix;
                var relativeMovementVector = new Vector3(
                    gamepadEvent.GamePadState.ThumbSticks.Left.X * ThumbstickMoveSensitivity * MovementSpeed,
                    0f,
                    -gamepadEvent.GamePadState.ThumbSticks.Left.Y * ThumbstickMoveSensitivity * MovementSpeed);

                var absoluteMovementVector =
                    _firstPersonCameraComponent.ComputeWorldSpaceVectorFromLocalSpace(relativeMovementVector);

                _firstPersonControllerPhysicsComponent.TargetVelocity = absoluteMovementVector;
                _didSetVelocityLastFrame = true;

                return(true);
            }

            if (mouseEvent != null && MouseLock)
            {
                var centerX = gameContext.Window.ClientBounds.Width / 2;
                var centerY = gameContext.Window.ClientBounds.Height / 2;

                _firstPersonCameraComponent.Yaw   += (centerX - mouseEvent.MouseState.X) / 1000f;
                _firstPersonCameraComponent.Pitch += (centerY - mouseEvent.MouseState.Y) / 1000f;

                var limit = MathHelper.PiOver2 - MathHelper.ToRadians(5);
                _firstPersonCameraComponent.Pitch = MathHelper.Clamp(_firstPersonCameraComponent.Pitch, -limit, limit);

                Mouse.SetPosition(centerX, centerY);

                return(true);
            }

            if (keyHeldEvent != null)
            {
                var didConsume = false;
                var moveX      = 0;
                var moveZ      = 0;

                if (keyHeldEvent.Key == Keys.A)
                {
                    _cumulativeMoveX -= 1;
                    didConsume        = true;
                }
                if (keyHeldEvent.Key == Keys.D)
                {
                    _cumulativeMoveX += 1;
                    didConsume        = true;
                }
                if (keyHeldEvent.Key == Keys.W)
                {
                    _cumulativeMoveZ += 1;
                    didConsume        = true;
                }
                if (keyHeldEvent.Key == Keys.S)
                {
                    _cumulativeMoveZ -= 1;
                    didConsume        = true;
                }

                var relativeMovementVector = new Vector3(
                    _cumulativeMoveX * ThumbstickMoveSensitivity * MovementSpeed,
                    0f,
                    -_cumulativeMoveZ * ThumbstickMoveSensitivity * MovementSpeed);
                var absoluteMovementVector =
                    _firstPersonCameraComponent.ComputeWorldSpaceVectorFromLocalSpace(relativeMovementVector);

                _firstPersonControllerPhysicsComponent.TargetVelocity = absoluteMovementVector;
                _didSetVelocityLastFrame = true;

                return(didConsume);
            }

            return(false);
        }