Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建与块和播放器的碰撞_C#_Xna_Collision Detection - Fatal编程技术网

C# 创建与块和播放器的碰撞

C# 创建与块和播放器的碰撞,c#,xna,collision-detection,C#,Xna,Collision Detection,嗨,伙计们,我正试着和球员碰撞 这些块类似于以下示例: 这是一种与地面的碰撞,当地面停止跳跃时,这种碰撞会使玩家更加完美 那是和屋顶的碰撞,他在艰难的阻挡之后不能继续跳下去 所以他继续努力工作 我现在的问题是左碰撞和右碰撞 那样 如果有人能帮我,我会很高兴的 让他在突破障碍后无法继续移动 这是我的课 我的左右两侧都不工作 UpdateCollision.cs 在键盘头上使用它们 keyboardheader.cs 是否有任何选项仅在X轴上进行检查我也在尝试解决此任务,但我的解决方案尚未完成。

嗨,伙计们,我正试着和球员碰撞

这些块类似于以下示例:

这是一种与地面的碰撞,当地面停止跳跃时,这种碰撞会使玩家更加完美

那是和屋顶的碰撞,他在艰难的阻挡之后不能继续跳下去 所以他继续努力工作

我现在的问题是左碰撞和右碰撞

那样

如果有人能帮我,我会很高兴的 让他在突破障碍后无法继续移动

这是我的课 我的左右两侧都不工作

UpdateCollision.cs

在键盘头上使用它们

keyboardheader.cs


是否有任何选项仅在X轴上进行检查

我也在尝试解决此任务,但我的解决方案尚未完成。但我建议您使用快速体素遍历算法进行光线跟踪。不需要为每个网格创建矩形来检查碰撞,因此速度要快得多。也许稍后我会用代码片段发布答案。ty我会等待你的解决方案我真的尝试解决它几小时后我把我的解决方案放到bitbucket上。他是个跛子,但总的来说还是行得通的。我也只是在学习,我希望看到更有经验的开发人员的反应。请参阅我的解决方案。这是必需的GameHelper库,您可以在WindRock项目中找到它。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;


class UpdateCollision
{
    public Player myPlayer;
    public Map myMap;

    public Block toughtBlock;

    public void Initialize(Player myPlayer, Map myMap)
    {
        this.myPlayer = myPlayer;
        this.myMap = myMap;
    }

    public bool IsPlayerToughGround()
    {

        Rectangle Player;
        Rectangle BlockDown;

        // Only create the rectangle once for the player
        Player = new Rectangle((int)myPlayer.Position.X,
        (int)myPlayer.Position.Y,
        myPlayer.Width/2,
        myPlayer.Height/2);

        // Do the collision between the player and the Block

        int i, j;

        bool isTough = false;

        // For each block in the game
        for (i = 0; i < (myMap.Height / myMap.pictureSize) && !isTough; i++)
        {
            for (j = 0; j < (myMap.Width / myMap.pictureSize) && !isTough; j++)
            {
                // If the block is Draw
                if (myMap.Blocks[i, j].isAlive)
                {


                    BlockDown = new Rectangle((int)myMap.Blocks[i, j].X,
                                              (int)myMap.Blocks[i, j].Y,
                                               myMap.pictureSize,
                                               myMap.pictureSize);


                    // Determine if the two objects collided with each
                    // other
                    if (Player.Intersects(BlockDown)) 
                    {
                        isTough = true;
                        toughtBlock = myMap.Blocks[i, j];
                    }
                }
            }
        }

        return isTough;


    }

    public bool IsPlayerToughRoof()
    {

        Rectangle Player;
        Rectangle Blockup;

        // Only create the rectangle once for the player
        Player = new Rectangle((int)myPlayer.Position.X,
        (int)myPlayer.Position.Y,
        myPlayer.Width / 2,
        myPlayer.Height / 2);

        // Do the collision between the player and the Block

        int i, j;

        bool isTough = false;

        // For each block in the game
        for (i = 0; i < (myMap.Height / myMap.pictureSize) && !isTough; i++)
        {
            for (j = 0; j < (myMap.Width / myMap.pictureSize) && !isTough; j++)
            {
                // If the block is Draw
                if (myMap.Blocks[i, j].isAlive)
                {
                    Blockup = new Rectangle((int)myMap.Blocks[i, j].X,
                                            (int)myMap.Blocks[i, j].Y + 20,
                                                 myMap.pictureSize,
                                                 myMap.pictureSize);


                    // Determine if the two objects collided with each
                    // other
                    if (Player.Intersects(Blockup))
                    {
                        isTough = true;
                        toughtBlock = myMap.Blocks[i, j];
                    }


                }
            }
        }

        return isTough;
    }

    public bool IsPlayerToughLeft()
    {

        Rectangle Player;
        Rectangle BlockLeft;

        // Only create the rectangle once for the player
        Player = new Rectangle((int)myPlayer.Position.X,
        (int)myPlayer.Position.Y,
        myPlayer.Width ,
        myPlayer.Height );

        // Do the collision between the player and the Block

        int i, j;

        bool isTough = false;

        // For each block in the game
        for (i = 0; i < myMap.Height / myMap.pictureSize; i++)
        {
            for (j = 0; j < myMap.Width / myMap.pictureSize; j++)
            {
                // If the block is Draw
                if (myMap.Blocks[i, j].isAlive)
                {

                    BlockLeft = new Rectangle((int)myMap.Blocks[i, j].X +20,
                                          (int)myMap.Blocks[i, j].Y,
                                               myMap.pictureSize,
                                               myMap.pictureSize);



                    // Determine if the two objects collided with each
                    // other
                    if (Player.Intersects(BlockLeft))
                        {
                            isTough = true;
                            toughtBlock = myMap.Blocks[i, j];
                        }


                }
            }
        }

        return isTough;
    }

    public bool IsPlayerToughRight()
    {

        Rectangle Player;
        Rectangle Blockup;
        Rectangle BlockDown;
        Rectangle BlockLeft;
        Rectangle BlockRight;

        // Only create the rectangle once for the player
        Player = new Rectangle((int)myPlayer.Position.X,
        (int)myPlayer.Position.Y,
        myPlayer.Width / 2,
        myPlayer.Height / 2);

        // Do the collision between the player and the Block

        int i, j;

        bool isTough = false;

        // For each block in the game
        for (i = 0; i < (myMap.Height / myMap.pictureSize) && !isTough; i++)
        {
            for (j = 0; j < (myMap.Width / myMap.pictureSize) && !isTough; j++)
            {
                // If the block is Draw
                if (myMap.Blocks[i, j].isAlive)
                {
                    Blockup = new Rectangle((int)myMap.Blocks[i, j].X,
                                          (int)myMap.Blocks[i, j].Y + 20,
                                               myMap.pictureSize,
                                               myMap.pictureSize);

                    BlockDown = new Rectangle((int)myMap.Blocks[i, j].X,
                                          (int)myMap.Blocks[i, j].Y,
                                               myMap.pictureSize,
                                               myMap.pictureSize);

                    BlockLeft = new Rectangle((int)myMap.Blocks[i, j].X + 20,
                                          (int)myMap.Blocks[i, j].Y,
                                               myMap.pictureSize,
                                               myMap.pictureSize);



                    // Determine if the two objects collided with each
                    // other
                    if (Player.Intersects(Blockup) ||
                        Player.Intersects(BlockDown) ||
                        Player.Intersects(BlockLeft))// ||
                    //Player.Intersects(BlockRight))
                    {
                        isTough = true;
                        toughtBlock = myMap.Blocks[i, j];
                    }


                }
            }
        }

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

    class KeyBoardHeadler
    {
        KeyboardState myKeyBoard;

        // A movement speed for the player
        float playerMoveSpeed;

        float Gravity;

        float JumpSpeed;

        // The graphic device
        GraphicsDevice gamedevice;

        // The Contect that load texture
        ContentManager LoadTexture;

        // The vector that move the gravity
        Vector2 velocity;

        UpdateCollision Coll;

        Player player;
        Map map;


        bool RightIsOk=true;
        bool LeftIsOk=true;

        public void Initialize(GraphicsDevice gv, ContentManager mainTexture, Player player, Map map)
        {


            // Get keyboard statement
            myKeyBoard = new KeyboardState();

            // Set a constant player move speed
            playerMoveSpeed = 3.0f;

            // Set Gravity vector
            velocity = Vector2.Zero;

            // Gravity amount
            Gravity = 10f;

            // Jump Speed
            JumpSpeed = 400f;

            // Get the graphic device
            gamedevice = gv;

            // Get Contect Manger to change player texture
            LoadTexture = mainTexture;

            // Initlize collision
            Coll = new UpdateCollision();
            Coll.Initialize(player, map);

            // Initalize map and player
            this.player = player;
            this.map = map;

        }

        public void Update(GameTime gameTime)
        {


            // Read the current state of the keyboard and gamepad and store it
            myKeyBoard = Keyboard.GetState();

            // Use the Keyboard / Dpad
            if (myKeyBoard.IsKeyDown(Keys.Left) && LeftIsOk)
            {
                player.PlayerAnimation.elapsedTime +=(int)gameTime.ElapsedGameTime.TotalMilliseconds;
                player.PlayerAnimation.spriteStrip = LoadTexture.Load<Texture2D>("Player\\Left");
                player.PlayerAnimation.frameCount = 2;
                player.Position.X -= playerMoveSpeed;

            }
            if (myKeyBoard.IsKeyDown(Keys.Right) && RightIsOk)
            {
                player.PlayerAnimation.elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
                player.PlayerAnimation.spriteStrip = LoadTexture.Load<Texture2D>("Player\\Right");
                player.PlayerAnimation.frameCount = 2;
                player.Position.X += playerMoveSpeed;
            }
            if (myKeyBoard.IsKeyDown(Keys.Up) && !player.isJump)
            {
                // add the gravity to the Player
                velocity.Y = -JumpSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                player.isJump = true;
            }

            if (player.isJump)
                velocity.Y += Gravity * (float)gameTime.ElapsedGameTime.TotalSeconds;
            else
                velocity.Y = 0;


            // Add the position of the jump
            player.Position.Y += velocity.Y;

            // If the Player tough anything
            if (Coll.IsPlayerToughGround())
            {
                // if the Player is not jump
                if (Coll.myPlayer.Position.Y <= Coll.toughtBlock.Y)
                {
                    player.isJump = false;
                    velocity.Y = 0;
                }
            }
            else
            {
                player.isJump = true;
            }

            if (Coll.IsPlayerToughRoof())
            {
                velocity.Y = 2;
            }

            if (Coll.IsPlayerToughLeft())
            {
                int x=6;
            }

            // Make sure that the player does not go out of bounds (get the area of the game (between 0 - max of map)
            player.Position.X = MathHelper.Clamp(player.Position.X, 0, gamedevice.Viewport.Width - player.Width + 25);
            player.Position.Y = MathHelper.Clamp(player.Position.Y, 0, gamedevice.Viewport.Height - player.Height + 50);

            if (myKeyBoard.IsKeyDown(Keys.NumPad0))
            {
                player.Position = new Vector2(100, 100);
                velocity.Y = 0;

            }
            if (myKeyBoard.IsKeyDown(Keys.NumPad1))
            {
                player.isJump = !player.isJump;

            }

        }

        public void Draw(SpriteBatch spriteBatch)
        {

        }

    }