Jump to content
• Advertisement

# Point within rectangle in 3D

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

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hi, I'm implementing a band-box selection feature in a 3D strategy game. The units have 2d quads representing their band box, so for a pick ray selection I successfully took the projection, view, and world matrix of the entity's position/rotation and applied the inverse of them all to the origin/normal of the pick ray from the screen. Now for the banding box, I am taking the four corners of the box and applying the same inverse matrix to put them on the same plane as the unit's bounding box. I thus just need to test to see if any of the four corners of the unit's border lay within the now-transformed bounding box. Therefore, what is an efficient method to determine whether a point in 3D space, which we know is coplanar on a rectangle in that space, lies within the bounds of that rectangle? Keep in mind that the rectangle might be arbitrarily rotated, so it's not so simple as comparing the respective x,y,z components. There are articles about whether two points lie on the same side of a line in 2D which are the right idea, but in 3D the idea of two points being on the same side of a line seems more complex to me. Thanks.

#### Share this post

##### Share on other sites
Advertisement
thread on the very same page you posted your question...

#### Share this post

##### Share on other sites
It might be easier to just transform the 3D boxes to screen-space instead, and do the whole comparison there in 2D (should at least be more efficient).

If you want to do it in 3D, you could construct a plane from the line and one of the points offset along the normal of the plane the line lies in. That will give you a boundary plane aligned with the line, and you can take the distance of a 3D point to that plane.

#### Share this post

##### Share on other sites
Quote:
 Original post by Erik RufeltIt might be easier to just transform the 3D boxes to screen-space instead, and do the whole comparison there in 2D (should at least be more efficient).

I figured as much; this was just one of those cases where you already have done so much work to get it one way, you resist a simpler one. :/

So I'm now attempting to do that, and this is DirectX specific but here goes:
int i;D3DXVECTOR3 plane[4]; //holds the four corners of the unit's bounding box.D3DXVECTOR2 box[2]; //the defining corners of the band box. 0 is top-left and 1 bottom-right.D3DXMATRIX matr,proj,view,world;graphDevice->GetTransform(D3DTS_PROJECTION,&proj); //retrieve currently-used projection matrixgraphDevice->GetTransform(D3DTS_VIEW,&view); //and view matrixsetupUnitWorldMatrix(this_unit,&world); //the same world matrix I use for drawingD3DXMatrixMultiply(&matr,&world,&view); //start with world, multiply by viewD3DXMatrixMultiply(&matr,&matr,&proj); //then multiply that by projection last//the above is the order that it's done in the graphics pipeline, right?//set up object boundary corners:plane[0]=top_left;plane[1]=top_right;plane[2]=bottom_right;plane[3]=bottom_left;for(i=0;i<4;i++)D3DXVec3TransformCoord(&plane[j],&plane[j],&matr);//transform them all by our combined world/view/projection matrix//set up box[2]'s corners herefor(i=0;i<4;i++)	if(plane.x>=box[0].x&&plane.x<=box[1].x&&plane.y>=box[0].y&&plane.y<=box[1].y)		select(this_unit);

However, this isn't working - it's making the plane[4] corners all close to 1, for example {0.82106942,0.98830092,0.99237508}, with each point only varying from one another by like 0.0000001 or so. I would expect it to make any transformed point's x and y components into screen space from 0 to screen resolution, and the z component 0, right?

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
17
5. 5
• Advertisement

• 10
• 14
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
631756
• Total Posts
3002114
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!