2011年7月19日火曜日

アクションゲームの当たり判定

アクションゲームの当たり判定を教えてくれと頼まれたので、久しぶりに当たり判定のソースを書いてみました。
C#でXNA用に書いてます。

3段階に分けて書いてみました。
最初は四角と四角の当たり判定。
2つ目は四角と複数の四角の当たり判定。
3つ目はジャンプができる四角と複数の四角の当たり判定。
もっとシンプルにできそうな気もしますが、一応書きたてのものを載せてみました。


四角と四角の当たり判定
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 Hit_Test_01
{
    /// 
    /// This is the main type for your game
    /// 
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D player;
        Texture2D map;

        Vector2 playerPosition;
        Vector2 mapPosition;

        const int PLAYER_WIDTH = 60;
        const int PLAYER_HEITHT = 90;

        const int MAP_WIDTH = 100;
        const int MAP_HEIGHT = 100;

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

        /// 
        /// 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.
        /// 
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            playerPosition = new Vector2(0, 0);
            mapPosition = new Vector2(100, 100);
            base.Initialize();
        }

        /// 
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// 
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            player = Content.Load("players16");
            map = Content.Load("map");
            // TODO: use this.Content to load your game content here
        }

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

        /// 
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// 
        /// Provides a snapshot of timing values.        protected override void Update(GameTime gameTime)
        {
            keyboardState = Keyboard.GetState();

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (keyboardState.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            if (keyboardState.IsKeyDown(Keys.Left))
            {
                playerPosition.X -= 1.0f;
                if (Hit())
                {
                    playerPosition.X = mapPosition.X + MAP_WIDTH;
                }
            }
            if (keyboardState.IsKeyDown(Keys.Right))
            {
                playerPosition.X += 1.0f;
                if (Hit())
                {
                    playerPosition.X = mapPosition.X - PLAYER_WIDTH;
                }
            }
            if (keyboardState.IsKeyDown(Keys.Up))
            {
                playerPosition.Y -= 1.0f;
                if (Hit())
                {
                    playerPosition.Y = mapPosition.Y + MAP_HEIGHT;
                }
            }
            if (keyboardState.IsKeyDown(Keys.Down))
            {
                playerPosition.Y += 1.0f;
                if (Hit())
                {
                    playerPosition.Y = mapPosition.Y - PLAYER_HEITHT;
                }
            }

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        private bool Hit()
        {
            if (playerPosition.X + PLAYER_WIDTH > mapPosition.X &&
                playerPosition.X < mapPosition.X + MAP_WIDTH &&
                playerPosition.Y + PLAYER_HEITHT > mapPosition.Y &&
                playerPosition.Y < mapPosition.Y + MAP_HEIGHT)
            {
                return true;
            }
            return false;
        }
        /// 
        /// This is called when the game should draw itself.
        /// 
        /// Provides a snapshot of timing values.        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(
                map,
                new Rectangle((int)mapPosition.X, (int)mapPosition.Y, MAP_WIDTH, MAP_HEIGHT),
                new Rectangle(0, 0, MAP_WIDTH, MAP_HEIGHT),
                Color.White);
            spriteBatch.Draw(
                player,
                new Rectangle((int)playerPosition.X, (int)playerPosition.Y, PLAYER_WIDTH, PLAYER_HEITHT),
                new Rectangle(0, 0, PLAYER_WIDTH, PLAYER_HEITHT),
                Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}



四角と複数の四角の当たり判定。
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 Hit_Test_02
{
    /// 
    /// This is the main type for your game
    /// 
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D player;
        Texture2D map;

        const int MAP_DIVISION_MAX_X = 8;
        const int MAP_DIVISION_MAX_Y = 5;

        Vector2 playerPosition;
        Vector2 playerSpeed;

        Vector2[,] mapPosition = new Vector2[MAP_DIVISION_MAX_X, MAP_DIVISION_MAX_Y];
        Boolean[,] mapActivity = new Boolean[MAP_DIVISION_MAX_X, MAP_DIVISION_MAX_Y];

        const int PLAYER_WIDTH = 60;
        const int PLAYER_HEITHT = 90;

        const int MAP_WIDTH = 100;
        const int MAP_HEIGHT = 100;

        const int MAP_OFFSET_X = 0;
        const int MAP_OFFSET_Y = 0;

        const float HIT_ADJUST_LENGTH = 1.01f;
        KeyboardState keyboardState;

        enum DIRECTION
        {
            LEFT,
            TOP,
            BOTTOM,
            RIGHT
        }
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// 
        /// 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.
        /// 
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            playerPosition = new Vector2(MAP_WIDTH * 3, MAP_HEIGHT * 1);
            playerSpeed = new Vector2(0.0f, 0.0f);

            for (int x = 0; x < MAP_DIVISION_MAX_X; ++x)
            {
                for (int y = 0; y < MAP_DIVISION_MAX_Y; ++y)
                {
                    mapPosition[x, y] = new Vector2(x * MAP_WIDTH + MAP_OFFSET_X, y * MAP_HEIGHT + MAP_OFFSET_Y);
                    mapActivity[x, y] = true;
                }
//                mapActivity[x, 0] = false;
            }
            mapActivity[3, 1] = false;
            mapActivity[3, 2] = false;
            mapActivity[4, 1] = false;
            mapActivity[4, 2] = false;

            base.Initialize();
        }

        /// 
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// 
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            player = Content.Load("players16");
            map = Content.Load("map");
            // TODO: use this.Content to load your game content here
        }

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

        /// 
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// 
        /// Provides a snapshot of timing values.        protected override void Update(GameTime gameTime)
        {
            keyboardState = Keyboard.GetState();

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (keyboardState.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            if (keyboardState.IsKeyDown(Keys.Left))
            {
                playerSpeed.X = -1.0f;
            }
            if (keyboardState.IsKeyDown(Keys.Right))
            {
                playerSpeed.X = 1.0f;
            }
            if (keyboardState.IsKeyDown(Keys.Up))
            {
                playerSpeed.Y = -1.0f;
            }
            if (keyboardState.IsKeyDown(Keys.Down))
            {
                playerSpeed.Y = 1.0f;
            }

            playerPosition += playerSpeed;
            Hit();
            playerSpeed.X = 0.0f;
            playerSpeed.Y = 0.0f;

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        private void Hit()
        {
            if (playerSpeed.X < 0.0f)
            {
                int x = (int)(playerPosition.X) / MAP_WIDTH;
                int y = (int)(playerPosition.Y + PLAYER_HEITHT - HIT_ADJUST_LENGTH) / MAP_HEIGHT;

                if (mapActivity[x, y] &&
                    playerPosition.X + PLAYER_WIDTH > mapPosition[x, y].X &&
                    playerPosition.X < mapPosition[x, y].X + MAP_WIDTH &&
                    playerPosition.Y + PLAYER_HEITHT > mapPosition[x, y].Y &&
                    playerPosition.Y < mapPosition[x, y].Y + MAP_HEIGHT)
                {
                    playerPosition.X = mapPosition[x, y].X + MAP_WIDTH;
                    playerSpeed.X = 0.0f;
                }
            }
            if (playerSpeed.X > 0.0f)
            {
                int x = (int)(playerPosition.X + PLAYER_WIDTH) / MAP_WIDTH;
                int y = (int)(playerPosition.Y + PLAYER_HEITHT - HIT_ADJUST_LENGTH) / MAP_HEIGHT;

                if (mapActivity[x, y] && 
                    playerPosition.X + PLAYER_WIDTH > mapPosition[x, y].X &&
                    playerPosition.X < mapPosition[x, y].X + MAP_WIDTH &&
                    playerPosition.Y + PLAYER_HEITHT > mapPosition[x, y].Y &&
                    playerPosition.Y < mapPosition[x, y].Y + MAP_HEIGHT)
                {
                    playerPosition.X = mapPosition[x, y].X - PLAYER_WIDTH;
                    playerSpeed.X = 0.0f;
                }
            }
            if (playerSpeed.Y < 0.0f)
            {
                int x = (int)(playerPosition.X) / MAP_WIDTH;
                int y = (int)(playerPosition.Y) / MAP_HEIGHT;

                if (mapActivity[x, y] && 
                    playerPosition.X + PLAYER_WIDTH > mapPosition[x, y].X &&
                    playerPosition.X < mapPosition[x, y].X + MAP_WIDTH &&
                    playerPosition.Y + PLAYER_HEITHT > mapPosition[x, y].Y &&
                    playerPosition.Y < mapPosition[x, y].Y + MAP_HEIGHT)
                {
                    playerPosition.Y = mapPosition[x, y].Y + MAP_HEIGHT;
                    playerSpeed.Y = 0.0f;
                }
            }
            if (playerSpeed.Y > 0.0f)
            {
                int x = (int)(playerPosition.X) / MAP_WIDTH;
                int y = (int)(playerPosition.Y + PLAYER_HEITHT) / MAP_HEIGHT;

                if (mapActivity[x, y] && 
                    playerPosition.X + PLAYER_WIDTH > mapPosition[x, y].X &&
                    playerPosition.X < mapPosition[x, y].X + MAP_WIDTH &&
                    playerPosition.Y + PLAYER_HEITHT > mapPosition[x, y].Y &&
                    playerPosition.Y < mapPosition[x, y].Y + MAP_HEIGHT)
                {
                    playerPosition.Y = mapPosition[x, y].Y - PLAYER_HEITHT;
                    playerSpeed.Y = 0.0f;
                }
            }
        }
        /// 
        /// This is called when the game should draw itself.
        /// 
        /// Provides a snapshot of timing values.        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            for (int x = 0; x < MAP_DIVISION_MAX_X; ++x)
            {
                for (int y = 0; y < MAP_DIVISION_MAX_Y; ++y)
                {
                    if (mapActivity[x, y])
                    {
                        spriteBatch.Draw(
                            map,
                            new Rectangle((int)mapPosition[x, y].X, (int)mapPosition[x, y].Y, MAP_WIDTH, MAP_HEIGHT),
                            new Rectangle(0, 0, MAP_WIDTH, MAP_HEIGHT),
                            Color.White);
                    }
                }
            }
            spriteBatch.Draw(
                player,
                new Rectangle((int)playerPosition.X, (int)playerPosition.Y, PLAYER_WIDTH, PLAYER_HEITHT),
                new Rectangle(0, 0, PLAYER_WIDTH, PLAYER_HEITHT),
                Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
ジャンプができる四角と複数の四角の当たり判定。
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 Hit_Test_03
{
    /// 
    /// This is the main type for your game
    /// 
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D player;
        Texture2D map;

        const int MAP_DIVISION_MAX_X = 8;
        const int MAP_DIVISION_MAX_Y = 5;

        Vector2 playerPosition;
        Vector2 playerSpeed;

        const float GRAVITY = 1.0f;

        Vector2[,] mapPosition = new Vector2[MAP_DIVISION_MAX_X, MAP_DIVISION_MAX_Y];
        Boolean[,] mapActivity = new Boolean[MAP_DIVISION_MAX_X, MAP_DIVISION_MAX_Y];

        const int PLAYER_WIDTH = 60;
        const int PLAYER_HEITHT = 90;

        const int MAP_WIDTH = 100;
        const int MAP_HEIGHT = 100;

        const int MAP_OFFSET_X = 0;
        const int MAP_OFFSET_Y = 0;

        const float HIT_ADJUST_LENGTH = 1.01f;
        KeyboardState keyboardState;

        enum DIRECTION
        {
            LEFT,
            TOP,
            BOTTOM,
            RIGHT
        }
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// 
        /// 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.
        /// 
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            playerPosition = new Vector2(MAP_WIDTH * 3, MAP_HEIGHT * 1);
            playerSpeed = new Vector2(0.0f, 0.0f);

            for (int x = 0; x < MAP_DIVISION_MAX_X; ++x)
            {
                for (int y = 0; y < MAP_DIVISION_MAX_Y; ++y)
                {
                    mapPosition[x, y] = new Vector2(x * MAP_WIDTH + MAP_OFFSET_X, y * MAP_HEIGHT + MAP_OFFSET_Y);
                    mapActivity[x, y] = true;
                }
                //                mapActivity[x, 0] = false;
            }
            mapActivity[2, 1] = false;
            mapActivity[2, 2] = false;
            mapActivity[2, 3] = false;
            mapActivity[3, 1] = false;
           // mapActivity[3, 2] = false;
            mapActivity[3, 3] = false;
            mapActivity[4, 1] = false;
            mapActivity[4, 2] = false;
            mapActivity[4, 3] = false;
            mapActivity[5, 1] = false;
          //  mapActivity[5, 2] = false;
            mapActivity[5, 3] = false;
            mapActivity[6, 1] = false;
            mapActivity[6, 2] = false;
            mapActivity[6, 3] = false;
            base.Initialize();
        }

        /// 
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// 
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            player = Content.Load("players16");
            map = Content.Load("map");
            // TODO: use this.Content to load your game content here
        }

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

        /// 
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// 
        /// Provides a snapshot of timing values.        protected override void Update(GameTime gameTime)
        {
            keyboardState = Keyboard.GetState();

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (keyboardState.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            if (keyboardState.IsKeyDown(Keys.Left))
            {
                playerSpeed.X = -1.0f;
            }
            if (keyboardState.IsKeyDown(Keys.Right))
            {
                playerSpeed.X = 1.0f;
            }
            if (keyboardState.IsKeyDown(Keys.Up))
            {
                playerSpeed.Y = -10.0f;
            }
            if (keyboardState.IsKeyDown(Keys.Down))
            {
                playerSpeed.Y = 1.0f;
            }

            playerPosition += playerSpeed;
            Hit();
            playerSpeed.X = 0.0f;
            playerSpeed.Y += GRAVITY;

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        private void Hit()
        {
            if (playerSpeed.X < 0.0f)
            {
                int x = (int)(playerPosition.X) / MAP_WIDTH;
                int y = (int)(playerPosition.Y + PLAYER_HEITHT - HIT_ADJUST_LENGTH - playerSpeed.Y) / MAP_HEIGHT;

                if (mapActivity[x, y] &&
                    playerPosition.X + PLAYER_WIDTH > mapPosition[x, y].X &&
                    playerPosition.X < mapPosition[x, y].X + MAP_WIDTH &&
                    playerPosition.Y + PLAYER_HEITHT > mapPosition[x, y].Y &&
                    playerPosition.Y < mapPosition[x, y].Y + MAP_HEIGHT)
                {
                    playerPosition.X = mapPosition[x, y].X + MAP_WIDTH;
                    playerSpeed.X = 0.0f;
                }
            }
            if (playerSpeed.X > 0.0f)
            {
                int x = (int)(playerPosition.X + PLAYER_WIDTH) / MAP_WIDTH;
                int y = (int)(playerPosition.Y + PLAYER_HEITHT - HIT_ADJUST_LENGTH - playerSpeed.Y) / MAP_HEIGHT;

                if (mapActivity[x, y] &&
                    playerPosition.X + PLAYER_WIDTH > mapPosition[x, y].X &&
                    playerPosition.X < mapPosition[x, y].X + MAP_WIDTH &&
                    playerPosition.Y + PLAYER_HEITHT > mapPosition[x, y].Y &&
                    playerPosition.Y < mapPosition[x, y].Y + MAP_HEIGHT)
                {
                    playerPosition.X = mapPosition[x, y].X - PLAYER_WIDTH;
                    playerSpeed.X = 0.0f;
                }
            }
            if (playerSpeed.Y < 0.0f)
            {
                int x = (int)(playerPosition.X - playerSpeed.X) / MAP_WIDTH;
                int y = (int)(playerPosition.Y) / MAP_HEIGHT;
                if (!mapActivity[x, y] && mapActivity[x + 1, y] && playerPosition.X <= mapPosition[x + 1, y].X)
                {
                    x += 1;
                }
                if (mapActivity[x, y] &&
                    playerPosition.X - HIT_ADJUST_LENGTH + PLAYER_WIDTH > mapPosition[x, y].X &&
                    playerPosition.X - HIT_ADJUST_LENGTH < mapPosition[x, y].X + MAP_WIDTH &&
                    playerPosition.Y + PLAYER_HEITHT > mapPosition[x, y].Y &&
                    playerPosition.Y < mapPosition[x, y].Y + MAP_HEIGHT)
                {
                    playerPosition.Y = mapPosition[x, y].Y + MAP_HEIGHT;
                    playerSpeed.Y = 0.0f;
                }
            }
            if (playerSpeed.Y > 0.0f)
            {
                int x = (int)(playerPosition.X) / MAP_WIDTH;
                int y = (int)(playerPosition.Y + PLAYER_HEITHT) / MAP_HEIGHT;
                if (!mapActivity[x, y] && mapActivity[x + 1, y] && playerPosition.X <= mapPosition[x + 1, y].X)
                {
                    x += 1;
                }
                if (mapActivity[x, y] &&
                    playerPosition.X + PLAYER_WIDTH > mapPosition[x, y].X &&
                    playerPosition.X < mapPosition[x, y].X + MAP_WIDTH &&
                    playerPosition.Y + PLAYER_HEITHT > mapPosition[x, y].Y &&
                    playerPosition.Y < mapPosition[x, y].Y + MAP_HEIGHT)
                {
                    playerPosition.Y = mapPosition[x, y].Y - PLAYER_HEITHT;
                    playerSpeed.Y = 0.0f;
                }
            }
        }
        /// 
        /// This is called when the game should draw itself.
        /// 
        /// Provides a snapshot of timing values.        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            for (int x = 0; x < MAP_DIVISION_MAX_X; ++x)
            {
                for (int y = 0; y < MAP_DIVISION_MAX_Y; ++y)
                {
                    if (mapActivity[x, y])
                    {
                        spriteBatch.Draw(
                            map,
                            new Rectangle((int)mapPosition[x, y].X, (int)mapPosition[x, y].Y, MAP_WIDTH, MAP_HEIGHT),
                            new Rectangle(0, 0, MAP_WIDTH, MAP_HEIGHT),
                            Color.White);
                    }
                }
            }
            spriteBatch.Draw(
                player,
                new Rectangle((int)playerPosition.X, (int)playerPosition.Y, PLAYER_WIDTH, PLAYER_HEITHT),
                new Rectangle(0, 0, PLAYER_WIDTH, PLAYER_HEITHT),
                Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}