Sign in to follow this  
MikeWulf

Unit selection in RTS (C++ SDL)

Recommended Posts

MikeWulf    100
I have a 2d tile map with units on it already. I've been piecing this thing together as a side project for a long while now, but now I am stuck. Unit selection is going way over my head. The way I imagined it was that I would hold a list of all the units x,y coords and cycle through the list till the mouse clicks x,y is within the bounding box of the unit. Does this sound like a good way to do it? Sorry if it is confusing since I am not good with the terminology of all this.

Share this post


Link to post
Share on other sites
Bob Janova    769
That is certainly the simplest way to do it, and as long as you have a reasonably low number of units it should be fine. If you have several thousand units it might get slow, but for normal RTS games the simple way should be fine. Always code up a simple solution first!

Share this post


Link to post
Share on other sites
evillive2    779
If you are using a tilemap you could use your tiles to sort your units so that you only check a few units instead of a large amount. When a unit moves pop it from a list of units on the tile it is moving from and push it onto the tile it is moving to. It may be on the same tile but adding and removing things from a list (doubly linked) is pretty quick or you could check before you pop it. This has lots of uses especially with drawing order but for picking it can be useful as well.

I would also recommend using circles instead of squares for picking. As long as you leave all of the distances squared it can be quite fast and if units do become closely situated you can see which one is closer to the mouse click instead of just which rectangle was clicked into first. The reason I say this is that circles have an origin just like your units should. Since you have already checked the distance from this origin there is less work to be done than with rectangles in the event of a click being in multiple units space.

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