I'm developing a card game. My custom control works great, but I need help to solve this:

When I move a card (my custom control) over another card I need to know which card is underneath. Once I know that, I'll be able to put in logic for game rules.


I'd like to have some event in cardcontrol like: OnOtherCardControlOver() but I have some difficulty to figure it out.

  • 1
    \$\begingroup\$ may I ask how are you storing cards in your program, are you havving an array containing all cards, or does every custom control instance take a hold of some card or any thing else that could help? \$\endgroup\$ – Ali1S232 May 20 '11 at 21:04
  • \$\begingroup\$ @gajet: I can store them in any kind of way: list<cardcontrol>, or cardcontrol[] etc. etc. Control can expose value, color of any thing can help the play... \$\endgroup\$ – Christian May 20 '11 at 21:08

Since you said your game is free form (i.e. no predefined stacks) your problem basically turns into a collision detection one.

Basically your cards are going to have a position on the board and a size. From there you need to iterate over all the other cards on the board and determine what cards you're overlapping.

If you define your cards to be rectangles and don't allow rotation, the problem gets pretty easy.

Basically you would calculate the 4 corners of your card by offsetting from the card's position half the size of the card, and plugging those numbers in to this formula: https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other/2696876#2696876. You can either decide to go for a naive solution (i.e. "do they overlap at all", or go for the area calculation to figure out which card is the "most" covered and use that one.

The problem gets a little more difficult if you can have cards stacking on top of each other. You'd basically have to do the same test but in order from top to bottom, where every time you put a card down you assume it's on a higher layer.

| improve this answer | |

if i understood is what is game goning to look like, i suggest you keep some kind of stack in for each pile of cards you have in the screen, then use your custom control to move cards from one stack to another, using stacks would also have prevent you from unwanted changes where you pick a card from the ones underneath others,

with the new information you gave i think you could still use some kind of stack but now every control has a stack of other controls underneath it. you can also make an array of all your card controllers and when trying to check if some card is underneath another see if their bounding boxes collide. and if so which one was there first (you can use time for the second check)

| improve this answer | |
  • \$\begingroup\$ than for your time! I dont have pile, but 32 card distribuiited in 4 hands. The any card can play or not whit others base on rules game. I need to know which card is underneath \$\endgroup\$ – Christian May 20 '11 at 21:17
  • \$\begingroup\$ if there is no pile involved how can a card be underneath another? \$\endgroup\$ – Ali1S232 May 20 '11 at 21:23
  • \$\begingroup\$ I can move any card freely in the board. Based on rules game each card interact diffently depends on which card is underneath \$\endgroup\$ – Christian May 20 '11 at 21:25

Your Answer

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

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