0
\$\begingroup\$

I've been implementing an AI system in my 2D game using MonoGame.

In some situations when a collision between two objects occurs, I need the NPC to move in different directions depending on the direction of the collision. With the simple Rectangle.Intersects method I can't handle this, because it only detects if two rectangles intersect and doesn't tell me how they intersect.

Basically I need to know if the top of the NPC rectangle collides with the bottom of a tile rectangle, and so on.

This question talks about something similar but I don't think there is CollisionDetection2D.BoundingRectangle in MonoGame.

\$\endgroup\$
2
  • \$\begingroup\$ That first paragraph is one sentence? I think it's unclear what you're asking. (It seems to involve intersecting rectangles, but that's where I get lost.) Could you clarify; maybe draw a picture? \$\endgroup\$
    – Anko
    Commented Apr 7, 2014 at 18:04
  • \$\begingroup\$ OMG i must 've been drunk when i wrote this lol, ok the thing is , i wanted to determine when the top of npc(computer player) rectangle intersects with the bottom of the tile map rectangle,and vice versa, i already had a reply here : monogame.codeplex.com/discussions/541384 but i appreciate your reply , my question looks messy indeed and i apologize for that... Thanks again for the effort(lol) :D Kind regards, Romulo Romero \$\endgroup\$
    – Ogre3dUser
    Commented Apr 7, 2014 at 20:50

1 Answer 1

0
\$\begingroup\$

There are several solution to your problem. The first solution i can think of is instead of using one rectangle you can use 4, or even 9+ if you need more detail. Put a top-left, top-right, bottom-left, bottom-right rectangle within your main rectangle. When you have the collision you currently have you can check on the more detailed rectangles and see where it collides.

Another option would be to check it manually. If you just want to know if the top of a rectangle intersects with the bottom of another you can use the default Rectangle properties (.Bottom, .Top, .Left, .Right) and check there positions manually.

if (intersect)
{
    if (Math.Abs(rec1.Top - rec2.Bottom) < rec2.height / 2)
    {
        //rec1 is intersecting in the bottom half of rec2.
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ First of all, sorry for the delay dude, i've been working hard , trying to figure out the best settings for the AI, and thanks for replying, that seems to work but when i use the opposite in order to determine if the npc rect intersects at the top of the tile rect it doesnt seem to work...im still trying some things out , but if you have any tip i really appreciate it! Thanks again and kind regards, \$\endgroup\$
    – Ogre3dUser
    Commented Apr 10, 2014 at 18:52

Not the answer you're looking for? Browse other questions tagged .