So im posting this here and not under game development because this is more of a problem solving question, not directly game related.

Im making a simple RPG using xna. Im currently working on collision detection, however running into difficulty. I can successfully check for a collision between the player and a 'bad' tile, however I am stumped as to what the engine should do to prevent the player from passing throguth it!

Currently, I am getting the players based on an enum 'Direction', which is set whenever one of the 4 arrow keys are pressed. I am then creating a tmp Rectangle called checkBounds, and checking whether the players position vector, plus its velocity vector will result in a collision, is true, adding 0 to its position vector, if false, adding the velocity vector on as normal. Here are the methods:

public Rectangle playerBounds = new Rectangle(360, 250, PLAYER_WIDTH, PLAYER_HEIGHT); // Sets the players initial bounds

// Which way is the player facing?
public enum Direction
{
    Up,
    Down,
    Left,
    Right,
    None
};

Direction dirUp = Direction.Up;
Direction dirDown = Direction.Down;
Direction dirLeft = Direction.Left;
Direction dirRight = Direction.Right;



        public void Update(Player player, Core core)
        {
            // Gets an array list of possible key inputs.
            Keys[] keys = Keyboard.GetState().GetPressedKeys();

            if(keys != null && keys.Length > 0){

                if (keys[0] == Keys.Up)
                {
                    // Up arrow pressed, move player up.
                    CheckCollision(player, core, dirUp);
                }
                else if (keys[0] == Keys.Down)
                {
                    // Down arrow pressed, move player down.
                    CheckCollision(player, core, dirDown);
                }
                else if (keys[0] == Keys.Left)
                {
                    // Left arrow pressed, move player left.
                    CheckCollision(player, core, dirLeft);
                }
                else if (keys[0] == Keys.Right)
                {
                    // Right arrow pressed, move player right.
                    CheckCollision(player, core, dirRight);
                }
            }

        }



        private void CheckCollision(Player player, Core core, Direction dir)
        {

            // Creates a temp storage of players new location. Used to check if the players next move will be with a bad tile.
            Rectangle checkBounds = new Rectangle(player.playerBounds.X, player.playerBounds.Y, PLAYER_WIDTH, PLAYER_HEIGHT);

            int width = core.collisionMap.GetLength(1);
            int height = core.collisionMap.GetLength(0);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (dir == dirUp)
                        checkBounds.Y += (int)player.playerVel.Z; // Player is moveing up.
                    if (dir == dirDown)
                        checkBounds.Y -= (int)player.playerVel.W; // Player is moveing down.
                    if (dir == dirLeft)
                        checkBounds.X += (int)player.playerVel.X; // Player is moveing left.
                    if (dir == dirRight)
                        checkBounds.X += (int)player.playerVel.Y; // Player is moveing right.

                    if (checkBounds.Intersects(core.collisionMap[y, x]))
                    {

                        // A collision was detected.
                    if (dir == dirUp)
                        player.playerBounds.Y += (int)0; // Add zero to players up direction
                    if (dir == dirDown)
                        player.playerBounds.Y -= (int)0; // Add zero to players down direction
                    if (dir == dirLeft)
                        player.playerBounds.X += (int)0; // Add zero to players left direction
                    if (dir == dirRight)
                        player.playerBounds.X += (int)0; // Add zero to players right direction

                    }
                    else
                    {
                        // A collision was not detected.

                    if (dir == dirUp)
                        player.playerBounds.Y += (int)player.playerVel.Z; // Add velocity to players up direction
                    if (dir == dirDown)
                        player.playerBounds.Y -= (int)player.playerVel.W; // Add velocity to players down direction
                    if (dir == dirLeft)
                        player.playerBounds.X += (int)player.playerVel.X; // Add velocity to players left direction
                    if (dir == dirRight)
                        player.playerBounds.X += (int)player.playerVel.Y; // Add velocity to players right direction
                    }
                }
            }
        }

Problem is, when I run the game, the player just dissapears off the screen?! Why is that happening, and how can I get this working?

Any and all suggestions are welcome!

Edited 3 Years Ago by CJMW

This question has already been answered. Start a new discussion instead.