# [SOLVED] Best way for RTS-style multiple selection tool

Hi monkeys,

I’m trying to implement the tipical RTS system to select multiple objects on a scene, i.e. you click and drag your mouse to “build” a 2D-square that contains all the objects you want to select and when you release the mouse button all the objects are selected. Right now I have done the draw-a-square-with-your-mouse part and I’m thinking on how to move on for the actual select-the-objects part. I thought that maybe I could ray cast from the 4 vertexes of my selection-square till the farest object in the scene (the world geometry), and calculate which objects are within the area. Or maybe it’s better to exploit the physics and build an invisible box and detect collision? I’m looking for comments and advices so any word is greatly appreciated.

If you think about it, the selection area is a pyramid with a flat top actually (the selection square at the cam near frustum is the top, the square all the way at the far frustum is the bottom). Checking if a point is inside that pyramid can be as easy as checking if a ray from that point intersects with the pyramid faces (if its one time its inside, if its zero or two times its outside):

[java]public boolean isVectorInPyramid(Vector3f v){

int cnt = 0;

Ray ray = new Ray(v, rndVec());

//seiten

if ( ray.intersect(t, b1, b2) ) cnt++;

//if (cnt>1) return false;

if ( ray.intersect(t, b2, b3) ) cnt++;

if (cnt>1) return false;

if ( ray.intersect(t, b3, b4) ) cnt++;

if (cnt>1) return false;

if ( ray.intersect(t, b4, b1) ) cnt++;

if (cnt>1) return false;

//unterseite

if ( ray.intersect(b1, b2, b3) ) cnt++;

if (cnt>1) return false;

if ( ray.intersect(b1, b3, b4) ) cnt++;

if (cnt>1) return false;

if (cnt==1)

return true;

else //->(cnt==0 || cnt>1)

return false;

}[/java]

Vectors b1-b4 are the bottom points and t is the top (its not a flat pyramid technically, it uses the cam location for that point). The bottom points have to be in clockwise or counter-clockwise order.

3 Likes

Thanks normen,

I just implemented your solution and it works perfectly. As point to check for inclusion I use the center of the bounding box.

Thanks again man

1 Like