1
\$\begingroup\$

I'm making my first game using C# and Monogame and I have a huge problem with collision detection.

The entire code is in a link below:

My game code

So, I use Vector2 to keep position and dimensions of objects such as characters and blocks which are elements of the map.

The problem is here(I guess):

    protected virtual void CollisionDetection()
    {
        if (!CanGoRight()) pos.X -= velocity;
    }

    protected bool IsTouchingLeft(Basic2d asset)
    {
        return pos.X + dims.X / 2 > asset.pos.X &&
               pos.X - dims.X / 2 < asset.pos.X &&
               pos.Y + dims.Y / 2 < asset.pos.Y - asset.dims.Y;
               //pos.Y - dims.Y / 2 > asset.pos.Y;
    }

    protected bool CanGoRight()
    {
        foreach (Basic2d asset in Globals.GameAssets)
            if (IsTouchingLeft(asset)) return false;
        return true;
    }

The Hero object stops while walking on the floor when the Basic2d objects ends. But I have in my code that he's supposed to stop if his bottom side(bottom of the feet)(in pixels) is lower than floor's object upper side. And he seems not to see the brick(Basic2d object) that is a real obstacle on his way, he just goes through it.

I have no idea what is wrong.

\$\endgroup\$
  • \$\begingroup\$ This looks extremely similar to your previous question about debugging your collision code. Can you please explain what's different about the problem you're solving here? If it's still the same problem, and you want to clarify the question to attract more answers, please use the edit button on your original question, rather than posting duplicates for the same issue. \$\endgroup\$ – DMGregory May 28 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.