# Clickable areas inside a rectangle

## Recommended Posts

popcorn    151
Suppose I have the above situation, I want to work out all the clickable areas for the two rectangles. The bigger rectangle covers the second rectangle and so when you click on the overlapping section you should be clicking on the bigger rectangle rather than the smaller rectangle. What's the best way of working this out? Solutions in C++ or C# preferred.

##### Share on other sites
theOcelot    498
Check the collision against the larger one first, and if it succeeds, ignore any further possible collisions.

##### Share on other sites
ChaosEngine    5185
No-one is going to do this for you. Show us how far you've got, where you're having trouble and we'll help you along. We're not going to implement the solution for you.

I'll give you a head start though. If I have a rectangle defined as 2 points (top left and bottom right) and a coordinate how do you figure out if a given point is in that rectangle?

How do you know the bigger rectangle is on top? Do you have a collection of rectangles sorted in z-order?

##### Share on other sites
popcorn    151
@theOcelot

Do you reckon that this game works out the clickable areas in the way you describe?

@ChaosEngine
Ok, I'm not looking for the full solution, just a general outline of how to work this out.

As for your question about the point inside a rectangle I've something similar before in a win32 console program.

The actual program is here: Memory Game

The problem here is working out the clickable areas when the cards are placed on top of each other. As I said just a general outline will do.

Also I have no idea what rectangles sorted in z order means?

##### Share on other sites
ChaosEngine    5185
Quote:
 Original post by popcornAlso I have no idea what rectangles sorted in z order means?

z order is a fairly basic concept in computer graphics. That was the first link on google.

playing cards are a good example actually. Lets assume they're layed out on top of each other like so:
|---------||        A|--||         | K|--||         |  | Q|--||         |  |  | J||         |  |  |  ||         |  |  |  ||---------|  |  |  |   |---------|  |  |      |---------|  |         |---------|

in this case the ace is on top and has the highest z-order. sort your cards by z-order and test each card to see if the point is inside it.

##### Share on other sites
popcorn    151
So what does this mean in practical terms, how do I set up Z ordering?

I'm thinking it is just a vector of integers the higher the integer, the more on top the card will be?

##### Share on other sites
Decrius    100
Quote:
 Original post by popcornSo what does this mean in practical terms, how do I set up Z ordering?I'm thinking it is just a vector of integers the higher the integer, the more on top the card will be?

Exactly. You at the moment have some sort of storage to store which cards are on one pile. You seem to have working that the top card is drawn last, so you must have some sort of z-ordering already (aka. you have defined an order depending on the z-axis, either asc- or descending). Just iterate your current draw list in reverse order (top first)...and maybe first check if the click is within the pile before checking for all cards on the pile...save some time ;-)

##### Share on other sites
Zahlman    1682
Quote:
 Original post by popcornSo what does this mean in practical terms, how do I set up Z ordering?I'm thinking it is just a vector of integers the higher the integer, the more on top the card will be?

What's with all this indirection? If you want to know what card is 32nd from the top in an actual deck of cards, do you start out by writing the numbers 0 through 51 on little bits of paper? No, you just turn over cards.

Similarly, do the same with your programmatic cards. Just put them in a container. Their order in the container is their order for drawing, and thus their order for collision priority.

You might also want to meditate upon this.

##### Share on other sites
popcorn    151
Thanks for all the help everyone, I managed to get a little test program working. It's not really a difficult thing to achieve at all, I was confused about the Z ordering when it was brought up but, I guess you are naturally doing this with cards anyway. I imagine if it was a different type of game like for example a top down 2d scroller you would probably have to order the drawing in a much more specific way.