Unit selection in RTS (C++ SDL)

Started by
1 comment, last by evillive2 18 years, 1 month ago
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.
Advertisement
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!
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.
Evillive2

This topic is closed to new replies.

Advertisement