2D自上而下碰撞检测?

时间:2013-10-14 19:19:47

标签: c# xna

我一直试图像游戏一样为“自上而下的视图”创建一个碰撞。我已经制作了一个玩家类和一个块类,在我的Game1类中我检查了碰撞,不知道这是不是正确的方法,但它现在只是一个测试。所以如果我和他们相交,我不知道下一步该做什么。

这是我的Game1.cs

using System; using System.Collections.Generic; using System.Linq; 
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media;

namespace Collision_Testing {
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Player player = new Player(100, 100);
        Block block = new Block(500, 500);


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            block.LoadContent(Content);
            player.LoadContent(Content);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();



            block.Update(gameTime);
            player.Update(gameTime);
            base.Update(gameTime);

            if (player.boundingBox.Intersects(block.boundingBox))
            {
                player.playerPos.X -= player.speed;
                player.playerPos.Y -= player.speed;
            }
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            player.Draw(spriteBatch);
            block.Draw(spriteBatch);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    } }

这是我的Player.cs

using System; using System.Collections.Generic; using System.Linq; 
using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input;

namespace Collision_Testing {
    class Player
    {
        public Vector2 playerPos;
        public Texture2D playerTex;
        public int speed;
        KeyboardState oldState;
        public Rectangle boundingBox;

        public Player(int positionX, int positionY)
        {
            playerPos.X = positionX;
            playerPos.Y = positionY;


            speed = 14;
        }

        public void LoadContent(ContentManager Content)
        {
            playerTex = Content.Load<Texture2D>("player");
            boundingBox = new Rectangle((int)playerPos.X, (int)playerPos.Y, playerTex.Width, playerTex.Height);
        }

        public void Update(GameTime gameTime)
        {
            KeyboardState newState = Keyboard.GetState();

            if(newState.IsKeyDown(Keys.Right))
            {
                playerPos.X += speed;
            }

            if (newState.IsKeyDown(Keys.Left))
            {
                playerPos.X -= speed;
            }

            if (newState.IsKeyDown(Keys.Up))
            {
                playerPos.Y -= speed;
            }

            if (newState.IsKeyDown(Keys.Down))
            {
                playerPos.Y += speed;
            }


            oldState = newState;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(playerTex, playerPos, Color.White);
        }
    } }

这是我的Block.cs

using System; using System.Collections.Generic; using System.Linq; 
using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input;

namespace Collision_Testing {
    class Block
    {
        public Vector2 blockPos;
        public Texture2D blockTex;

        public Rectangle boundingBox;

        public Block(int blockX, int blockY)
        {
            blockPos.X = blockX;
            blockPos.Y = blockY;
        }

        public void LoadContent(ContentManager Content)
        {
            blockTex = Content.Load<Texture2D>("wall");
            boundingBox = new Rectangle((int)blockPos.X, (int)blockPos.Y, blockTex.Width, blockTex.Height);
        }

        public void Update(GameTime gameTime)
        {

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(blockTex, blockPos, Color.White);
        }
    } }

我认为我为初学者做了很好的工作,但不知道下一步该做什么。

2 个答案:

答案 0 :(得分:1)

在我看来,速度是一个标量。所以当你与盒子相交时,

if (player.boundingBox.Intersects(block.boundingBox))
{
    player.playerPos.X -= player.speed;
    player.playerPos.Y -= player.speed;
}

你只是向左移动14个单位,向上移动14个单位,无论玩家在哪里。

看起来你打算减去速度。为此,首先必须声明速度

public Vector2 playerVelocity;

确保在构造函数中初始化playerVelocity

然后你应该在测量输入时修改速度而不是位置

if(newState.IsKeyDown(Keys.Right))
{
    playerVelocity.X = speed;
}
else if (newState.IsKeyDown(Keys.Left))
{
    playerVelocity.X  = -speed;
}
else
{
    playerVelocity.X = 0;
}
...


playerPos += playerVelocity;

// i believe you can just add Vectors in XNA like this
// but if it makes you feel better you can do playerPos.X += playerVelocity.X

然后你可以在碰撞时扭转玩家的位置

if (player.boundingBox.Intersects(block.boundingBox))
{
    player.playerPos -= player.playerVelocity;
}

答案 1 :(得分:1)

如果你的角色没有停止移动,我想你会错过这样的事情:

if (newState.IsKeyUp(Keys.Up) && newState.IsKeyUp(Keys.Down) &&
    newState.IsKeyUp(Keys.Left) && newState.IsKeyUp(Keys.Right))
{
   playerVelocity = Vector2.Zero;
}