Sign in to follow this  

XNA: Creating a selection box for 3D RTS?

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

Hello!
I'm working on a simple 3D RTS game using XNA, my problem is with units selection...
The player can select a unit by clicking on it ( I used a Ray ), So I wanted to add a Select Box feature to the game...
I have the mouseRect which is the Rectangle the user created with the mouse,from here I tried 2 different things
( The camera class is just a wrapper for the View,Projection matrices )

1) Creating a BoundingBox


Vector3 nearPoint = new Vector3(mouseRect.X, mouseRect.Y, 0);
Vector3 farPoint = new Vector3(mouseRect.X + mouseRect.Width, mouseRect.Y + mouseRect.Height, 1);
nearPoint = GraphicsDevice.Viewport.Unproject(nearPoint, camera.Projection, camera.View, camera.View);
farPoint = GraphicsDevice.Viewport.Unproject(farPoint, camera.Projection, camera.View, camera.View);
mouseBox = new BoundingBox(nearPoint, farPoint);



This approach didn't work! I guess it's because the bounding box is Axis Aligned and my camera isn't!
The bounding class doesn't have a Transform method :(

2) Creating a BoundingFrustum


mouseFrustum = new BoundingFrustum(camera.View * Matrix.CreateOrthographic(mouseRect.Width, mouseRect.Height, 0, 5000));



As you can see I multiplied the View matrix with an Ortho matrix which gives me a special case of a frustum which is a box, this box is NOT Axis aligned,
I visualized the corners of the box and every thing was okay except that the frustum is always in the middle of the camera, so it won't work unless the object I'm trying to select is in near the middle of the camera
I just don't know the amount I should translate the frustum when creating it...

mouseFrustum = new BoundingFrustum(Matrix.CreateTranslation(x,y,z)*camera.View * Matrix.CreateOrthographic(mouseRect.Width, mouseRect.Height, 0, 5000));



I just need to figure out x,z
======================================
To me the second approach seems more promising!
Note : I already has a method that checks if the object is contained by a BoundingBox/BoundingFrustum
I hope I made my problem clear,Any help is appreciated!
Thanks in advance!

Share this post


Link to post
Share on other sites
I didn't look at your code carefully, but I can tell you that the idea of generating a frustum corresponding to the selection region is sound (that's the way I've done it in the past). Typically though you wouldn't create it using an orthographic projection unless the camera itself is orthographic.

The CML (linked in my signature) includes support for this, so if you need a code reference you could take a look at it. (The code isn't entirely straightforward due to its templated nature, but you might be able to find what you need if you dig around a bit.)

Share this post


Link to post
Share on other sites
I'd imagine that you would create a view matrix using the lookat method by finding the far position in the middle of your rectangle using the unproject method.

Then create a standard projection matrix using the aspect ratio given to you by your rectangle sizes and a field of view given by the difference in angle between say the top left line of the desired frustum and the top right line of it from the cameras POV.

I haven't tried it myself, getting the correct FOV is the only tricky part though but if you get the normalised directional vectors:

near-top-left -> far-top-left
near-top-right -> far-top-right

and do a dot product it'll give you an angle value between -1 and +1. You just need to convert that into degrees or radians (can't remember which the matrix constructor takes). +1 being identical vectors, -1 being exactly opposite so 0 for example would be 90 degrees, 0.5 would be 45 degrees.

Share this post


Link to post
Share on other sites

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