Search code examples
c#xnamonogame

XNA/MonoGame Mouse Over Cards


enter image description here

Blue overlaps Green which overlaps Red.

Each card can be selected by passing the mouse over it. But the thing my hitboxes don't have a depth notion (z-axis), it's a 2D game.

So lets say that i want to select the Green Card when i put my mouse over it the Green and the Red are selected because the cursor is in the Green HitBox but also in the Red HitBox.

So my question is how should i manage this: When i have overlapping hitboxes, how to check only the area that are not covered ?

Note : I use the Rectangle Intersect and Contains functions.


Solution

  • But the thing my hitboxes don't have a depth notion (z-axis), it's a 2D game....So my question is how should i manage this

    Just because it's a 2D game (and by that I mean the camera is projecting some world from xD to 2D) doesn't mean your scene has to be in 2D. Because your cards can overlap one another your scene has depth and so it is 3D.

    Once you realise this, hit detection of objects in your 3D scene are trivial.

    i.e.

    1. shoot a ray from the mouse, inverse projected into the scene
    2. test to see which objects it hits
    3. take the first object closest to the origin

    enter image description here