0
votes

Hy everyone, so I am making a little game in C# / XNA and I have this problem with collision.

Basiclly what I do in my code is I make a normal player Movement and instead of just checking if for example the Up key is pressed, it also checks if player CanGoTop == true;

if (keyState.IsKeyDown(Keys.Up) && CanGoTop == true) 
{
bla bla bla
}

I do this for left, right, up, down, and if a bollean is false, player will not move (There is an else statment after those 4 statments of movemnt that sets velocity,x and .y to 0)

Okay now I made a method inside my Player class, and you will get the idea what it is just by simply looking at it, I dont think that I have to explain what is going on here:

        public void RectangleInteraction(Rectangle anotherRectangle)
    {
        if (playerRectangle.Bottom <= anotherRectangle.Top &&
            playerRectangle.Top >= (anotherRectangle.Top - playerRectangle.Height) &&
            playerRectangle.Left <= anotherRectangle.Right &&
            playerRectangle.Right >= anotherRectangle.Left)
        {
            CanGoBot = false;
        }
        else CanGoBot = true;


        if (playerRectangle.Top >= anotherRectangle.Bottom &&
            playerRectangle.Bottom <= (anotherRectangle.Bottom + playerRectangle.Height) &&
            playerRectangle.Left <= anotherRectangle.Right &&
            playerRectangle.Right >= anotherRectangle.Left)
        {
            CanGoTop = false;
        }
        else CanGoTop = true;


        if (playerRectangle.Top <= anotherRectangle.Bottom &&
            playerRectangle.Bottom >= anotherRectangle.Top &&
            playerRectangle.Right <= anotherRectangle.Left &&
            playerRectangle.Left >= (anotherRectangle.Left - playerRectangle.Width))
        {
            CanGoRight = false;
        }
        else CanGoRight = true;


        if (playerRectangle.Top <= anotherRectangle.Bottom &&
            playerRectangle.Bottom >= anotherRectangle.Top &&
            playerRectangle.Left >= anotherRectangle.Right &&
            playerRectangle.Right <= (anotherRectangle.Right + playerRectangle.Width))
        {
            CanGoLeft = false;
        }
        else CanGoLeft = true;

    }

and then In my Game1.cs Update() method I use my player instance to call that method there, and now something thats bugging me a lot, It works for CanGoLeft but for other 3 bools, it doesn't.

I really don't know why, here are my 4 screenshoots with InGameMsgs to help me out, and well checking those Messages with my code tells me that CollisionLogic is good, but something else is wrong. Why does only CanGoLeft work then ? :/

Thanks in advanced for your time and help.

CanGoLeft CanGoBot

1

1 Answers

1
votes

Your logic seems broken at several points. You should use the Rectangle's method like Offset and Intersects to check if the target Rectangles would colide. I think this comes close to your intention (assuming distance is the value by which you're player moves):

public void RectangleInteraction(Rectangle anotherRectangle)
{
    Rectangle down = playerRectangle; down.Offset(0, distance);
    Rectangle up = playerRectangle; up.Offset(0, -distance);
    Rectangle left = playerRectangle; left.Offset(-distance, 0);
    Rectangle right = playerRectangle; right.Offset(distance, 0);

    ConGoBot = !down.Intersects(anotherRectanlge); 
    ConGoTop = !up.Intersects(anotherRectanlge); 
    ConGoLeft = !left.Intersects(anotherRectanlge); 
    ConGoRight = !right.Intersects(anotherRectanlge); 
}

(This is assuming you're using Microsoft.Xna.Framework.Rectangle, there are other Rectangle structs in .net framework, but your question was tagged )