1

So, my collision detection system is set up to check each pixel of the character, flagging collisionDetected, putting the pixel of the offending character model into collisionDetectedX and collisionDetectedY. It uses this data to determine where the valid movable areas are, and nudge the unit back into it. Is the only way around this to have a Collision class for X and Y running at the same time, or can I modify this class to do both jobs at the same time properly? Thank you for your time, I've been going nuts trying to figure out what I did wrong. I do apologize for sloppy code. I cleaned it up a bit (it was really a mess), but I realize it's not final product worthy.

class Collision
{        
    public Color[,] DataMiner(Texture2D background)
    {
        Color[,] collision2D;
        Color[] collision1D;
        collision1D = new Color[background.Width * background.Height];
        background.GetData(collision1D);
        collision2D = new Color[background.Width, background.Height];
        for (int x = 0; x < background.Width; x++)
            for (int y = 0; y < background.Height; y++)
                collision2D[x, y] = collision1D[x + y * background.Width];
        return collision2D;
    }
    public void CollisionChecker(Color[,] background, ref Vector2 position)
    {
        Rectangle character;
        character.Width = 32;
        character.Height = 32;
        character.X = Convert.ToInt16(position.X);
        character.Y = Convert.ToInt16(position.Y);
        int n = 0;
        int n2 = 0;
        bool collisionDetected = false;
        int collisionDetectedX = 0;
        int collisionDetectedY = 0;
        while (n2 < character.Width)
        {
            n = 0;
            while (n < character.Height)
            {
                if (background[character.X + n2, character.Y + n] == Color.Black)
                {
                    collisionDetected = true;
                    collisionDetectedX = n2;
                    collisionDetectedY = n;
                }
                n++;
            }
            n2++;
        }
        if (collisionDetected == true)
        {
            collisionDetectedX += Convert.ToInt16(position.X);
            collisionDetectedY += Convert.ToInt16(position.Y);
            if (background[collisionDetectedX, collisionDetectedY + 2] == Color.Red)
                position.Y += 1;
            else if (background[collisionDetectedX, collisionDetectedY - 2] == Color.Red)
                position.Y -= 1;
            else if (background[collisionDetectedX + 2, collisionDetectedY] == Color.Red)
                position.X += 1;
            else if (background[collisionDetectedX - 2, collisionDetectedY] == Color.Red)
                position.X -= 1;
            else if (background[collisionDetectedX - 2, collisionDetectedY - 2] == Color.Red)
            {
                position.X -= 1;
                position.Y -= 1;
            }
            else if (background[collisionDetectedX - 2, collisionDetectedY + 2] == Color.Red)
            {
                position.X -= 1;
                position.Y += 1;
            }
            else if (background[collisionDetectedX + 2, collisionDetectedY - 2] == Color.Red)
            {
                position.X += 1;
                position.Y -= 1;
            }
            else if (background[collisionDetectedX + 2, collisionDetectedY + 2] == Color.Red)
            {
                position.X += 1;
                position.Y += 1;
            }
            collisionDetected = false;
        }

    }
    public void Update(Texture2D background, ref Vector2 position)
    {
        CollisionChecker(DataMiner(background), ref position);
    }


}

Hmm, I don't have 10 rep yet so I can't answer my own question, but I figured it out:

I changed the conditions of the if statement a bit. I had to remove the else, because it would just drop the rest of the nested if's when it found one it needed to change. Since it was doing all of them at once, after fixing this issue, it would "slip" right or left when going against a flat wall if it was near a corner. I added the condition to check if the collisionDetected was still true, and made each change revert it to false.

Have a great day, hope someone can learn from my mistake!

        if (collisionDetected == true)
        {
            collisionDetectedX += Convert.ToInt16(position.X);
            collisionDetectedY += Convert.ToInt16(position.Y);
            if (background[collisionDetectedX, collisionDetectedY + 2] == Color.Red && collisionDetected)
            {
                position.Y += 1;
                collisionDetected = false;
            }
            if (background[collisionDetectedX, collisionDetectedY - 2] == Color.Red && collisionDetected)
            {
                position.Y -= 1;
                collisionDetected = false;
            }
            if (background[collisionDetectedX + 2, collisionDetectedY] == Color.Red && collisionDetected)
            {
                position.X += 1;
                collisionDetected = false;
            }
            if (background[collisionDetectedX - 2, collisionDetectedY] == Color.Red && collisionDetected)
            {
                position.X -= 1;
                collisionDetected = false;
            }

}

2

1 Answer 1

0

Answered my question in original text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.