Advertisement

Sweep selection using OpenGL

Started by June 13, 2005 01:11 PM
15 comments, last by ShCiPwA 19 years, 4 months ago
Quote: Original post by PaulAtreus
Quote: Original post by mfawcett
On mouse down, store the mouse position, on mouse up, calculate the center of the selection box, and pass the width and height params to opengl. Remember, selection usually occurs on mouse up, not in real-time as you drag the mouse.


Actually, you can make a flag that gets toggled to on when the mouse button is pressed and off when it is depressed and have the value of x,y stored upon the initial click. Then, inside your mouse-move event, you have a check for if the flag is toggled on. If it is, then you make your selection box's dimensions be from the initial x,y value to your current x,y value and you can do selection as you are dragging the selection box.

Absolutely, I was just trying to help him understand how he could go about implementing it. It's been a while since I played any RTS games. Is that how the selection action normally works?
--Michael Fawcett
Quote: Original post by mfawcett

Absolutely, I was just trying to help him understand how he could go about implementing it. It's been a while since I played any RTS games. Is that how the selection action normally works?


Actually, in most RTS games, the selection isn't made until the mouse is released. However, you will notice that many RTS games will highlight (brighten) the units that fall under your selection region as you are dragging it so that you are aware of what going to be selected before you let go. This is where it becomes a good idea to use the method I described above so that you may highlight the units as you are dragging your selection region. Then upon the button-up event, you can actually run the method to select the unit.

Also, this is where I would assume you would draw your actual box to outline the region. You wouldn't want to just have an invisible dragging region while trying to select; you would probably want to draw a box outlining where you are currently selecting as the mouse position updates.

EDIT: To clarify, when I mention that most games don't do selection until the mouse is released, I'm refering to selection in terms of game mechanics, where as an actual unit in the game is selected. My suggestion to check for selection during the mouse-move event is relating to the selection of GL polys in the code. Hence, why I say that you could use this method to highlight the units while dragging the selection region. Sorry if I'm my post was confusing.
Advertisement
How can you highlight a unit while you are making your selection box ( while you are dragging the mouse )if you didn`t make the selection yet?

My project`s facebook page is “DreamLand Page”

One way to do it would be to have a variable that says whether a unit is highlighted or not. For example, when calculating the selection area, i.e. dragging out the shape you want, you could say something like:
if (unit in selection box) then  unit.highlighted = trueelse  unit.hightlighted = false;

Now when you come to render the unit you could render highlighted units using a different colour:
if (unit.highlighted) then  glColor(RED)else  glColor(WHITE);

This would cause all units that are currently highlighted (in the selection area) to be rendered with a red hue and all those that are not in the selection area to be rendered with no hue changes.

Obviously you may want to use a different method for indicating which unit(s) are selected, like drawing a halo above them or whatever, but the principle is the same.

One thing to note is that the unit.highlight field should be set to false when selection is over as you don't want units being highlighted when a selection has been made.

Hope that helps.
--
Cheers,
Darren Clark
Quote: Original post by Calin
How can you highlight a unit while you are making your selection box ( while you are dragging the mouse )if you didn`t make the selection yet?


Ok, I think you're confused on what I meant. As I mentioned in the addendum to my post, there are two meanings of selection that I'm using.

In an RTS game, everyone knows that you click on units (or drag a selection box around them) to select them; I'll refer to this as unit selection. When doing OpenGL programming, there is also the technique of selecting polygonal objects; I'll refer to that as picking.

What I was suggesting was that you contain your picking code within the mouse-move events. That way, as you are dragging your selection region, you can draw an outlined box of the region that you are currently going to be selecting from as it expands, as well as possibly highlighting which units fall under the selection region to help the user distinguish which units are going to be selected more easily.

However, you would then have the actual unit selection take place upon release of the mouse button at which time the user would now have the newly selected "sqaud" at their command.

Sorry if I'm still not making sense. If there's something about it you still don't understand, please feel free to let me know.
Finaly I managed to make a working sweep selection system.
Now I have another question: How can I make a Minimap? I was thinking to have two OpenGL viewports: a big viewport for main screen and a small one for minimap. The problem is that I am afraid that having two viewports will slow down my game.
Do you have any suggestion?









[Edited by - Calin on July 1, 2005 11:15:57 AM]

My project`s facebook page is “DreamLand Page”

Advertisement
Your minimap does not need to have the kind of detail that the rest of your scene does. As in most games, the minimap is just a part of the hud. I would just render it over your current scene as you would some text or any other 2d interface item. Most games just have a simple pre rendered image of the terrain layout, with a shroud overlay (fog of war) and a bunch of dots to represent enemies. You will need to build an interface somtime (if you havnt already), I would just keep it along the same lines.

----- ShCiPwA -----

This topic is closed to new replies.

Advertisement