Sign in to follow this  

Point within rectangle in 3D

This topic is 2852 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


Link to 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


Link to post
Share on other sites
Quote:
Original post by Erik Rufelt
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).


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 matrix
graphDevice->GetTransform(D3DTS_VIEW,&view); //and view matrix
setupUnitWorldMatrix(this_unit,&world); //the same world matrix I use for drawing

D3DXMatrixMultiply(&matr,&world,&view); //start with world, multiply by view
D3DXMatrixMultiply(&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 here

for(i=0;i<4;i++)
if(plane[i].x>=box[0].x&&plane[i].x<=box[1].x&&plane[i].y>=box[0].y&&plane[i].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


Link to post
Share on other sites

This topic is 2852 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this