Monogame - 当我的播放器发生碰撞时让相机停止

时间:2016-01-22 21:20:16

标签: c# camera xna collision monogame

我已经在我的3D游戏中占据了一席之地,我现在正试图完善相机。我最终想要的是一个工作的第一人称相机,当它与墙壁碰撞时停止。我假设您通过让相机随模型移动并让模型碰撞来停止相机来实现这一点,我只是遇到了如何做到这一点的问题。

我让模型工作所以它停止了,我只需要锁定相机也不要移动。到目前为止,这是我的代码,使模型停止与collison:

 if (player_health != 0)
            {
                if (kb.IsKeyDown(Keys.Left))
                {
                    player_x = player_x + 0.05f;
                    world_player = Matrix.CreateTranslation(new Vector3(player_x, player_y, player_z));
                    if (IsCollision(ball, world_player, ball, world_spike))
                    {
                        player_x = player_x - 0.05f;
                        world_player = Matrix.CreateTranslation(new Vector3(player_x, player_y, player_z));
                        player_health = 0;
                    }
                    if (IsCollision(ball, world_player, ball, world_bullet[0]))
                    {
                        player_health = 0;
                    }
                    // use this code for any standard collision
                    if ((IsCollision(ball, world_player, ball, world_cannon)) || (IsCollision(ball, world_player, ball, walls[0])) || (IsCollision(ball, world_player, ball, walls[1])))
                    {
                        player_x = player_x - 0.05f;
                        world_player = Matrix.CreateTranslation(new Vector3(player_x, player_y, player_z));
                    }
                }

我的相机加入了game1课程:

camera = new Camera(this, new Vector3(10f, 3f, 5f), Vector3.Zero, 5f);
            Components.Add(camera);

相机类本身(我被告知这相当复杂?对于相机类,但更简单的没有用):

namespace Game7
{
    class Camera : GameComponent
    {
        private Vector3 cameraPosition;
        private Vector3 cameraRotation;
        private float cameraSpeed;
        private Vector3 cameraLookAt;

        private Vector3 mouseRotationBuffer;
        private MouseState currentMouseState;
        private MouseState previousMouseState;



        // Properties

        public Vector3 Position
        {
            get { return cameraPosition; }
            set
            {
                cameraPosition = value;
                UpdateLookAt();
            }
        }

        public Vector3 Rotation
        {
            get { return cameraRotation; }
            set
            {
                cameraRotation = value;
                UpdateLookAt();
            }
        }

        public Matrix Projection
        {
            get;
            protected set;
        }

        public Matrix View
        {
            get
            {
                return Matrix.CreateLookAt(cameraPosition, cameraLookAt, Vector3.Up);
            }
        }

        //Constructor 
        public Camera(Game game, Vector3 position, Vector3 rotation, float speed)
            : base(game)
        {
            cameraSpeed = speed;

            // projection matrix
            Projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4, 
                Game.GraphicsDevice.Viewport.AspectRatio,
                0.05f, 
                1000.0f);

            // set camera positiona nd rotation
            MoveTo(position, rotation);

            previousMouseState = Mouse.GetState();
        }

        // set Camera's position and rotation
        private void MoveTo(Vector3 pos, Vector3 rot)
        {
            Position = pos;
            Rotation = rot;
        }

        //update the look at vector
        private void UpdateLookAt()
        {
            // build rotation matrix
            Matrix rotationMatrix = Matrix.CreateRotationX(cameraRotation.X) * Matrix.CreateRotationY(cameraRotation.Y);
            // Look at ofset, change of look at
            Vector3 lookAtOffset = Vector3.Transform(Vector3.UnitZ, rotationMatrix);
            // update our cameras look at vector
            cameraLookAt = cameraPosition + lookAtOffset;
        }

        // Simulated movement
        private Vector3 PreviewMove(Vector3 amount)
        {
                // Create rotate matrix
                Matrix rotate = Matrix.CreateRotationY(cameraRotation.Y);
                // Create a movement vector
                Vector3 movement = new Vector3(amount.X, amount.Y, amount.Z);
                movement = Vector3.Transform(movement, rotate);

                return cameraPosition + movement;
        }

        // Actually move the camera
        private void Move(Vector3 scale)
        {
            MoveTo(PreviewMove(scale), Rotation);
        }

        // updat method
        public override void Update(GameTime gameTime)
        {
            // smooth mouse?
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            currentMouseState = Mouse.GetState();

            KeyboardState ks = Keyboard.GetState();

            // input

            Vector3 moveVector = Vector3.Zero;

            if (ks.IsKeyDown(Keys.W))
                moveVector.Z = 1;
            if (ks.IsKeyDown(Keys.S))
                moveVector.Z = -1;
            if (ks.IsKeyDown(Keys.A))
                moveVector.X = 1;
            if (ks.IsKeyDown(Keys.D))
                moveVector.X = -1;

            if (moveVector != Vector3.Zero)
            {
                //normalize it
                //so that we dont move faster diagonally
                moveVector.Normalize();
                // now smooth and speed
                moveVector *= dt * cameraSpeed;
                // move camera
                Move(moveVector);
            }

            // Handle mouse input

            float deltaX;
            float deltaY;

            if(currentMouseState != previousMouseState)
            {
                //Cache mouse location
                deltaX = currentMouseState.X - (Game.GraphicsDevice.Viewport.Width / 2);
                deltaY = currentMouseState.Y - (Game.GraphicsDevice.Viewport.Height / 2);

                // smooth mouse ? rotation
                mouseRotationBuffer.X -= 0.01f * deltaX * dt;
                mouseRotationBuffer.Y -= 0.01f * deltaY * dt;

                if (mouseRotationBuffer.Y < MathHelper.ToRadians(-75.0f))
                    mouseRotationBuffer.Y = mouseRotationBuffer.Y - (mouseRotationBuffer.Y - MathHelper.ToRadians(-75.0f));
                if (mouseRotationBuffer.Y > MathHelper.ToRadians(75.0f))
                    mouseRotationBuffer.Y = mouseRotationBuffer.Y - (mouseRotationBuffer.Y - MathHelper.ToRadians(75.0f));

                Rotation = new Vector3(-MathHelper.Clamp(mouseRotationBuffer.Y, MathHelper.ToRadians(-75.0f), MathHelper.ToRadians(75.0f)), MathHelper.WrapAngle(mouseRotationBuffer.X), 0);

                deltaX = 0;
                deltaY = 0;

            }

            // Alt + F4 to close now.
            // Makes sure the mouse doesn't wander across the screen (might be a little buggy by showing the mouse)
            Mouse.SetPosition(Game.GraphicsDevice.Viewport.Width / 2, Game.GraphicsDevice.Viewport.Height / 2);

            previousMouseState = currentMouseState;

                base.Update(gameTime);
        }


    }
}

相机类的预览移动部分意味着它检测到碰撞的位置,至少我所遵循的指南是如此,我只是没有看到如何整合它。感谢您的任何帮助或输入,即使它只是指向其他指南或资源的链接,也将非常感谢!

1 个答案:

答案 0 :(得分:0)

如果您的英雄模特(玩家)已经具有移动功能和碰撞功能,并且您希望相机遵循该模型,那么您可以极大地简化相机更新。

假设在移动玩家后,玩家的新位置和/或方向在player_world中表示,只需借用存储在player_world矩阵中的位置和方向信息来构建视图矩阵帧。

//after moving player and setting its matrix accordingly:
view = Matrix.Invert(player_world);

这是您完整的相机类更新。这将创建一个与玩家处于相同位置且面向相同方向的视图矩阵。如果玩家模型由于撞到墙壁而停止,那么相机(视图矩阵)也会停止,因为它是根据玩家的“停止”矩阵构建的。当移动和旋转与播放器相同时,无需创建用于移动和旋转相机的整个类。

有时本地玩家模型的起源位于玩家的脚或腹部,你希望相机在眼睛所在的头部。如果是这样,只需应用以下内容:

//after moving player and setting its matrix accordingly:
Matrix camera_world = player_world;
camera_world *= Matrix.CreateTranslation(Vector3.Up * ??f);// set ??f to the height difference between feet and head
view = Matrix.Invert(camera_world);
相关问题