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

  • 4
    \$\begingroup\$ do you need help drawing the box or do you need help deciding which units go in the box? \$\endgroup\$
    – Jimmy
    Feb 22 '12 at 4:07
  • 1
    \$\begingroup\$ "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? \$\endgroup\$ Feb 22 '12 at 5:33
  • \$\begingroup\$ Sorry about being unclear, I am asking about the actual selection of units \$\endgroup\$
    – Aralox
    Feb 22 '12 at 7:06

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)) {
  • \$\begingroup\$ 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/…. \$\endgroup\$ Feb 22 '12 at 6:40
  • \$\begingroup\$ 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/…. \$\endgroup\$ Feb 22 '12 at 6:40
  • \$\begingroup\$ 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. \$\endgroup\$
    – Thomas
    Feb 22 '12 at 7:51
  • \$\begingroup\$ 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 \$\endgroup\$
    – Aralox
    Feb 22 '12 at 8:03
  • \$\begingroup\$ @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. \$\endgroup\$ Feb 22 '12 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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