Public Group

# Lower right value of a RECT like object

This topic is 3169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a struct that is basically the same as a RECT. I need to know what the bottom right value would be for collision detection. At first I thought it would be as simple as adding the x+w and y+h but well...that doesn't work. My second idea was to use the Pythagorean theorem to get the diagonal length but once I did that, I didn't know if there was actually anything I could do with that information. So how the heck do I find out what the bottom right value would be? It's for a pong clone I'm trying to find this out for. C++, win api, vs2008 prof ed. (I'm trying to figure out the bottom right so I can start working on the top and bottom collision tests)

##### Share on other sites
Quote:
 At first I thought it would be as simple as adding the x+w and y+h but well...that doesn't work.
The bottom-right corner (assuming +y is down) should be (x+w, y+h). If that's not working, there must be something wrong elsewhere.

Can you post the code where you compute these coordinates?

##### Share on other sites
I'm still working on it. Here's the side ways collision:
int TestForCollision(OBJECT a, OBJECT b){	//if (((a.x + a.w <= b.y + b.h) && (a.x + a.w >= b.x)) || ((a.y + a.w >= b.x) && (a.y + a.w <= b.y + b.h)))	if( (a.x + a.w >= b.x) && (a.x <= b.x + b.w )) //testing for sideways collision			return 1;	return 0;}

The reason I said x+w and y+h wouldn't work is because those would give the top right and bottom left instead of the bottom right which is what I need. I'm probably not seeing something correctly. When I first thought about it I thought x+w and y+h would give me the bottom right but instead wouldn't that give me the width + height which would end up being a bigger width?

for example theres a rectangle with width 3 and height 4 at 0,0. Getting the bottom right corner would be 3 + 4 = 7 which would be wrong. I'm not sure if I'm explaining this well but if you look at my code you can see I'm trying to work with the corner points. I know (3,4) would be the bottom right but how do I translate that into code that I can work with?

##### Share on other sites
Just to make things a little easier: in the coordinate system that you're using, does +y go up or down?

##### Share on other sites
Y increases as it goes down and X increases as it goes to the right. The top left of the window is 0,0

##### Share on other sites
I'm not entirely clear as to the nature of your question, but here's some info that might help.

First of all, adding the width and height together (e.g. 4 + 3 = 7) isn't of much use in this context; I'm not sure what you mean to accomplish with that operation, but it doesn't really do anything useful.

Now, in the coordinate system you've specified, (x, y) is the top-left corner of the rectangle and (x+w, y+h) is the bottom-right corner.

For the purpose of collision detection, the representation (x, y, width, height) is somewhat awkward. Instead, try one of the following:

1. A 'min' vector and a 'max' vector (these would be the same as the upper-left and lower-right corners).

2. A center point, the half-width, and the half-height.

Both will work, but the former is probably the most common representation for axis-aligned boxes. Once you have the box in min-max form, it becomes fairly straightforward to write the kind of intersection tests you seem to be after.

##### Share on other sites
So if my understanding is correct, your function is supposed to return true if the rectangles overlap, and false otherwise. Here is the shortest way to do that:
int TestForCollision(OBJECT a, OBJECT b){    return (a.x < b.x+b.w && b.x < a.x+a.w         && a.y < b.y+b.h && b.y < a.y+a.h);}

I'm assuming you don't have negative heights or widths of course.

##### Share on other sites

I drew a picture. Hopefully that explains it a little better.

Also with the suggestions you gave how would creating a min vector and max vector as well as having the center points help with the collision testing? All I would need to test for are the four sides of the ball with the four sides of the paddle I would think. (The ball is a RECT because I'm still learning and wanted to keeps things simple)

##### Share on other sites
Your diagram simply illustrates your confusion. Each coordinate is a pair of numbers, e.g.
 (x, y)   (x+w, y)(x, y+h) (x+w, y+h)

To check for a collision, you don't need to care specifically about the corners. You only need to perform overlap tests on the x and y dimensions separately. The code I posted does this. Try it out.

##### Share on other sites
Quote:
 Also with the suggestions you gave how would creating a min vector and max vector as well as having the center points help with the collision testing?
You don't need min/max vectors *and* a center point; you just need one or the other representation (either min and max vectors, or a center point and two half-extents). To keep things simple, I'd just stick with the min and max vectors for now.

As for helping with collision testing, all of the representations mentioned (including 'x, y, width, height') are equivalent in the information they convey, and intersection tests can be written using any of them. However, IMO, the 'x, y, width, height' representation makes for more awkward code than the other two representations, and is a bit arbitrary in its emphasis on a specific vertex of the shape.

Regarding your image, it looks like you may be confusing scalar and vector quantities. A scalar is a single real number; a vector (2-d in this case) consists of two real numbers, x and y. x + w, y + h, and (x + w) + (y + h) are all scalar values, and as such, they do not (and cannot) represent corners of the box as your image suggests. Rather, the corners of the box are as follows:
upper-left:  x, yupper-right: x + w, ylower-left:  x, y + hlower-right: x + w, y + h
Each of these is a *vector* value, not a scalar value (note that each consists of two real numbers separated by a comma).

For the purpose of collision detection it is often convenient to think of the box in terms of the minimum and maximum values in the x and y directions, which in this case are:
min x: xmin y: ymax x: x + wmax y: y + h
Combine these into 'min' and 'max' vectors and you have:
min = (x, y)max = (x + w, y + h)
It is these min and max vectors that you'll often see used in intersection tests involving axis-aligned boxes.

I hope that helps clarify things a bit.

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633697
• Total Posts
3013407
×