Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm scouting around for different methods of implementing a typical RTS box selection. This is for 3D game - so I'm looking for methods for finding units within selection box in 3D world. Here is a quick screenshot of what a box selection looks like in 2D Starcraft:

Starcraft Screenshot

share|improve this question
do you need help drawing the box or do you need help deciding which units go in the box? – Jimmy Feb 22 '12 at 4:07
"because I find visual stimulus quite helpful" And yet, you failed to explain exactly what you were looking for. We all know what box selection is, but which part: the box drawing or the selecting? Is this a 3D game where one might have perspective issues, or is it a 2D game? What have you tried? – Nicol Bolas Feb 22 '12 at 5:33
Sorry about being unclear, I am asking about the actual selection of units – Aralox Feb 22 '12 at 7:06
up vote 5 down vote accepted

Your question was vague, but the answer to both interpretations could be simple.

Heres some pseudo code:

vec2 original_xy = get_mouse_position();
vec2 new_xy;

while (get_mouse_click_state() == MOUSE_DOWN) {
        new_xy = get_mouse_position();
        render_box(original_xy, new_xy);

vec2 a = mouse_to_world(original_xy);
vec2 b = mouse_to_world(new_xy),

const float max_height = 100.f; // maximum search height
BoundingBox aabb(vec3(min(a.x, b.x), min(a.y, b.y), 0), vec3(max(a.x, b.x), max(a.y, b.y), max_height));
List<Object> objects_in_box;

for Object x in all_objects do {
    if (x.collides(aabb)) {
share|improve this answer
render_window_box() must render properly using the window coordinates, probably use an orthographic projection matrix instead of whatever you use for the rest of your rendering. See…. – dcousens Feb 22 '12 at 6:40
mouse_to_world() must convert the 2D coordinates of the mouse to 2D/3D coordinates relative to your world. This can be done in a few ways, here is a GD:SE topic on the matter for OpenGL:…. – dcousens Feb 22 '12 at 6:40
Why use a bounding box, you could simply translate top left and bottom right point from 2d screen coords to 3d world coords, next iterate trough all units and see if x and y coords are in the rectangle definde by those 3d coords. – Thomas Feb 22 '12 at 7:51
This seems to be the most popular (maybe only?) solution from what ive read - I reckon I will use this in conjunction with my existing cell partitioning system, to get an O(constant) time search rather than an O(n) time with the for loop – Aralox Feb 22 '12 at 8:03
@Thomas you do realise that is exactly what the bounding box above does? Albeit with a 3rd dimension attached. If you have a 2D bounding box implementation, it would be the same approach; just without the Z component. – dcousens Feb 22 '12 at 13:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.