[SOLVED!] Real-time Strategy Unit Highlighting

I’m trying to achieve something like the standard rectangle that appears when you highlight things on your desktop:

Via simple graphics rendering calls (g.drawRect/g.drawLine) etc.

This is something I’ve always thought about implementing :slight_smile: I’m now at that point in a project of mine (RTS) where I’d like to implement this in it.

I’ve never used Vector math but I’m guessing that’s what I’ll be needing, I’m posting this in hopes of getting some help on how I’d achieve this.

If anybody could help me / push me in the right direction that would be great :slight_smile:

EDIT: Solved! For those who were wondering:


		if (mouse1Down) {
			int mouseX = client.getController().mouseX;
			int mouseY = client.getController().mouseY;
			g.drawLine(clickX, clickY, mouseX, mouseY - (mouseY - clickY));
			g.drawLine(mouseX, mouseY - (mouseY - clickY), mouseX, mouseY);
			g.drawLine(clickX, clickY, clickX, mouseY);
			g.drawLine(clickX, mouseY, mouseX, mouseY);
		}

I’m getting ready to go to sleep at the moment but for those who are still reading this post after it being marked as solved:

How would I go about detecting if a unit is inside of the rectangle?

The only idea I have so far is to have 4 rectangles than pick 1 depending on the mouse position relative to its starting position. (Doesn’t sound too efficient)

Do point-inside-rectangle collision detection.

While you could do it with the code you have now, and you would do that by comparing if the object.x is between mousestartx and mouseendx and the same for y.

but another (probably cleaner) way to do this would be to just make a rectangle instead of doing the 4 lines yourself, and then do rectangle.contains(x,y) to find if it is in there. Or like heroesgravedev said, rectangle collision.