0

我一直在尝试为类似游戏的“自上而下视图”创建碰撞。我做了一个玩家类和一个方块类,在我的 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);
        }
    } }

我认为我已经为初学者做得很好,但不知道下一步该做什么。

4

2 回答 2

1

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

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

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

看起来您打算减去velocity。为此,您首先必须声明一个速度

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;
}
于 2013-10-14T19:50:43.750 回答
1

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

if (newState.IsKeyUp(Keys.Up) && newState.IsKeyUp(Keys.Down) &&
    newState.IsKeyUp(Keys.Left) && newState.IsKeyUp(Keys.Right))
{
   playerVelocity = Vector2.Zero;
}
于 2013-10-14T20:29:37.063 回答