RTS Selecting units

Hey:)

So, I’m working on an RTS, and I’m currently thinking about how to go about boxing units. I think the idea is that I get the point on the screen where the mouse has clicked, then create a ray into the 3D world from that, and intersect it with the terrain.

I do this 4 times with each corner of the box, to get an area on the terrain that I’m selecting.

The problem is, where do I go from here? Do I check which units fall within this area? That would be quite messy, especially if a corner of the box lands over on the other side of a hill and another corner hits the hill…

Doing my own research, I’ve come across 2 methods I could use - gluUnproject and ‘picking’
using gluUnproject I can create a ray from my mouse position.
‘picking’ converts everything on the screen into a 2d image, with different colours used for different things. You then check which colours are inside the box, and select those units.

I’m thinking that picking would be the best way to go about this, but I’ve heard it’s quite slow. Does anyone have any experience with the two? Implementing one of these is going to be more than a few hours of work, as this is completely new territory for me, so I’d rather not have to redo it:P

I hope I’ve explained this well enough, thanks:)

I don’t know how your game works, but the usual approach (at least my approach) is to make a 2D (height doesn’t matter in (usual) rts games) rectangle from your 2 mousepositions, and check which (x/y-coördinates from the) units intersect.

You can also check if the x/(y+z) coördinates from the units intersect with the 2D rectangle, so that you really have to click an airplane (instead of the ground).

But how do I get that rectangle if the world is 3D but the input is 2D?

Also, what if I could rotate the view? I’d quite like to include this.

You can translate the screen/local point (the point your mouse is at the screen) to the world point with some math?

This is what I just found on google: (haven’t read it myself)

<3

Thank you so much, this is exactly what I needed!

When unprojecting your dragged reactangle into the scene, you’ll run into this problem:

What to do to solve this:
Project your units to 2D coordinates instead and check whether they are in your dragged rectangle.

^Yes, whilst reading through I became aware of that problem. Instead of using glUnProject, then checking which units were selected in that rectangle, I am now just using glProject, as you suggested. Thanks:)