Sign in to follow this  

Mesh picking by selection rectangle

This topic is 4867 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 currently developing an RTS game in 3D, similiar to C&C generals. Currently I'm stuck at selecting objects by drawing a rectangle on the screen. Drawing the rectangle on the screen is no problem, I'm using ID3DXLine for this purpose. For a single click selection I use D3DXIntersect, which works fine so far. But how do I check if an object (a mesh) is within the selection rectangle? I have a RECT struct for my selection rectangle, what I need is a formula for a function like MeshIsWithinRect(ID3DXMesh* pMesh, RECT* rtSelection); any suggestions? thanks in advance

Share this post


Link to post
Share on other sites
I assume you are culling objects not in the camera view by using the camera frustrum planes. You can use the same logic with the selection rectangle. Just pretend the rectangle is the camera's viewport. Then, instead of checking a quadtree or some other method of culling, check all of the objects that are currently within the view frustrum, and if they lie within the sub-view frustrum, or partially lie within it, they are to be selected. It's like a sub-view frustrum you create with the rectangle. Hope that helps.

Chris

Share this post


Link to post
Share on other sites
Yeah, that was my idea as well. I have a very basic ViewFrusum class up where my objects are being culled against, and guess what? I already added a function like this:
void CViewFrustum::FromSelectionRect(RECT* rtSelection)
{
// TODO
}


But my 3D-math is pretty bad and that's where I'm stuck :(

Share this post


Link to post
Share on other sites
The sub-frustum technique is probably the best way of doing things, but you could also project the centerpoints (or maybe each point of the unit's bounding box) of your units into screen space and simply see if those screenspace points/rectangles are inside your 2D rectangle. You can use D3DXVec3Project or D3DXVec3ProjectArray for this purpose.

Share this post


Link to post
Share on other sites
Assuming you are doing something similar to a click and drag selection system - you just need to get two mouse readings (transformed in to world coordinates) to decide on the minimum and maximum extensions of the selection rectangle. Read the point in the world when the user first clicks the mouse button to initiate the selection rectangle. Next, read the point in the world when the user releases the mouse button to get the second point. As an example, you could get these points in the world by picking your terrain / floor mesh.

You can then easily get the axis extensions of the rectangle as in the code below. You can then also easily test your models (just the ones in your view frustum) by their world position to see if they fall within the rectangle:


D3DXVECTOR3 RectClickStart; // Selection Rectangle [Point 1]
D3DXVECTOR3 RectClickEnd; // Selection Rectangle [Point 2]
D3DXVECTOR3 RectMinExtensions; // Minimum rectangle extensions
D3DXVECTOR3 RectMaxExtensions; // Maximum rectangle extensions
D3DXVECTOR3 Position; // World position of the object you are testing

// Get selection rectangle extensions
RectMinExtensions.x = min(RectClickStart.x, RectClickEnd.x);
RectMinExtensions.y = min(RectClickStart.y, RectClickEnd.y);
RectMinExtensions.z = min(RectClickStart.z, RectClickEnd.z);

RectMaxExtensions.x = max(RectClickStart.x, RectClickEnd.x);
RectMaxExtensions.y = max(RectClickStart.y, RectClickEnd.y);
RectMaxExtensions.z = max(RectClickStart.z, RectClickEnd.z);

// Check if the position of the object is within these
// boundries.
if( Position.x >= RectMinExtensions.x && Position.x < RectMaxExtensions.x )
{
if( Position.y >= RectMinExtensions.y && Position.y < RectMaxExtensions.y )
{
if( Position.z >= RectMinExtensions.z && Position.z < RectMaxExtensions.z )
{

// Object is in rectange - do whatever with it.
}
}
}




You could then add all those that units that fall in the rectangle to a multiple selection list or just keep the last one tested or... whatever [smile] - You could even eliminate the Y axis and make it a 2D problem.

Hope that helps.

[Edited by - __Daedalus__ on August 20, 2004 5:50:01 AM]

Share this post


Link to post
Share on other sites
I like the idea of creating a sub-frustum apart from view frustum, to check the visibility of my objects inside the selection rectangle. I also checked the D3DXVec3Project function, but I'd like to have a more accurate 3d-selection check. Would be even very few work since my view frustum is already working fine.

Now that my view frustum is working correct, what I would need to do is to create the 6 planes not from the complete screen, but from the rectangle.



I used this algorithm to create my view frustum with no problems:
http://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf

But what would I need to change in order to create the planes from my 4 rect coordinates (xMin, xMax, yMin, yMax) to easily check my objects against the rectangle frustum?
I guess I need to create a new projection matrix from those values, but how? Do I also need to change anything in my view matrix?

thanks in advance

Share this post


Link to post
Share on other sites
Well, I haven't done it, but here's how I would approach it. Use your favorite pick algorithm and extrapolate the 3D coordinates from the four rectangle coordinates. You can then use 2 of those 3D points in conjunction with your camera's positional point to create each plane needed for the frustrum.

Chris

Share this post


Link to post
Share on other sites

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