# Picking 3D objects with mouse

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

## Recommended Posts

I'm trying to find a way to select units with mouse cursor in a 3D RTS game. What's the fastest way? I figured out I'm supposed to create bounding boxes for all units (ComputeBoundingBox(...) ?) and then a vector from mouse click position paralell to the view axis would be useful. Am I supposed to walk from start of the "pick line" to the nearest object while checking if current point is inside any of bounding boxes? Or is there a math function checking intersection of a ray with a box (or set of boxes)? Thanks

##### Share on other sites
There are 2 tutorials on picking at Robert Dunlop's X-Zone
Also, if you search this forum for picking, you'll get lots of hits.

##### Share on other sites
I've been busy with this last week. I recommend not using the D3DXComputeBoundingBox function.

Why?

Well, it does what it says: box. Not a rectangle, but a box.

So imagine you want to have a bounding box around a piece of paper (long and thin). If you used the D3DX function, it will make the width (1mm?) of the paper have a bounding box of the length of the paper.. to make a box!

A rectangle would be a lot better, since it will fit exactly round the paper.

How to do this:

vector Max = 0
vector Min = 0
Loop through all vertices
if (vertices.x < Min.x) Min.x = vertices.x
if (vertices.x > Max.x) Max.x = vertices.x
//(repeat for y/z)
Next

And then you got yourself a vector min and max: min is top-left of you, vMax is far-right-top. Good luck ;)

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

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

• Total Topics
631757
• Total Posts
3002140
×