Sign in to follow this  

Picking 3D objects with mouse

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 this post

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


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[i].x < Min.x) Min.x = vertices[i].x
if (vertices[i].x > Max.x) Max.x = vertices[i].x
//(repeat for y/z)

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

Share this post

Link to post
Share on other sites

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