Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

i'm just scouting around for different methods of implementing a typical rts box selection (not the 2D box on the screen, but what units are selected with the box) when selecting units, and would appreciate any ideas people have.

This is for a 3D real time strategy game.

Here is a quick screenshot of what a box selection looks like in Starcraft, because I find visual stimulus quite helpful:

Starcraft Screenshot

share|improve this question
4  
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
1  
"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
add comment

1 Answer

up vote 4 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);
        ...
        context_swap_buffers();
}

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)) {
        list.add(x);
    }    
}
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 gamedev.stackexchange.com/questions/7060/…. –  Daniel 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: gamedev.stackexchange.com/questions/8974/…. –  Daniel 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. –  Daniel Feb 22 '12 at 13:04
show 2 more comments

Your Answer

 
discard

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.