Sign in to follow this  
kbranch

Know of a good way to select multiple units?

Recommended Posts

I'm trying to figure out a good way to select multiple units in a 2D RTS by dragging a box around them. Right now I'm checking to see if any of the four corners of the texture are inside the box, but that takes 4 if statements that are 11 lines each and doesn't really work too well. Does anybody know of a better way?

Share this post


Link to post
Share on other sites
Don't test each point, if you're just testing each point, I can draw a box around a unit that doesnt select it by excluding each of the 4 points, but covering the rest of the unit.

Instead, check if the right side of the selection is to the left of the left side of the unit. If it is, the unit is clearly not in the selection. Repeat for other 3 sides.

Share this post


Link to post
Share on other sites
Why is it taking you 11 lines of code to test whether a point is inside a box?

And if you have to do the same basic thing four times and it takes 11 lines of code each time, why aren't you writing a function (or loop) to do it?

Share this post


Link to post
Share on other sites
Why not just test the center point of each sprite? This makes more sense to me, because the user will be less likely to select a unit accidentally just because the unit walks slightly into their box. Also, I am sure that not all of your sprites are square (ie they have transparent areas), what happens when a user overlaps the selection square with the texture, but the texture is transparent?


Just a thought.

Share this post


Link to post
Share on other sites
I prefer having the unit be selected when any part of it is inside the box, it can be rather annoying trying to select the ones you want otherwise. I'm not too concerned with non square images since the transparent areas aren't very big.

I ended up checking the sides against the box as suggested. That didn't count units that were only partially covered by the box, so I also check if any of the box sides go through the image.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Why is it taking you 11 lines of code to test whether a point is inside a box?

And if you have to do the same basic thing four times and it takes 11 lines of code each time, why aren't you writing a function (or loop) to do it?


It wasn't actually 11 lines, but an if statement that took 11 lines when word wrap was on, so I couldn't really make a function out of it. It was that long because it checked all 4 points.

There were 4 separate ifs because I needed to check it depending on where you started drawing the box. I ended up removing 3 of the ifs by just changing around the start and end points after the box has been made.

Share this post


Link to post
Share on other sites
If you have enough units that this becomes computationally intensive to check against every unit, you might consider using a quadtree to divide up the units on the landscape and then use it for selection. I'm betting though that it would take a lot of units to make it an issue (1 mil seperate soldiers anyone ^_^), but it could be helpful.

Share this post


Link to post
Share on other sites
Quote:
Original post by intrest86
If you have enough units that this becomes computationally intensive to check against every unit, you might consider using a quadtree to divide up the units on the landscape and then use it for selection. I'm betting though that it would take a lot of units to make it an issue (1 mil seperate soldiers anyone ^_^), but it could be helpful.


I've got it down to one 3 line if statement now and I'm dealing with less than one hundred units. It also skips any units you don't own, so you're really only dealing with less than 50. I don't forsee this game ever coming anywhere near needing something like that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this