簡體   English   中英

2D自上而下碰撞檢測?

[英]2D top down collision detection?

我一直試圖為游戲中的“自上而下的視圖”創建一個碰撞。 我已經制作了一個玩家類和一個塊類,在我的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);
        }
    } }

我覺得我為初學者做了很好的工作,但不知道下一步該做什么。

它看起來像速度是一個標量。 所以當你與盒子相交時,

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;
}

如果你的角色沒有停止移動,我想你會錯過這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM