I am making an RTS in Windows, and will use DirectX.
There is a problem though, I''m fairly new and just recently learned about classes.
How do I compare if a unit is within a circular radius around the current unit?
But how do I mathmatically determine which unit to attack?
While pathfinding, should I make an array of coordinates to follow, then go by that after figuring the shortest distance?
How do I find the shortest distance?
-Ace

# How do I determine if a unit is within range?

Started by ace_muncher, Oct 15 2001 01:44 PM

4 replies to this topic

###
#2
Members - Reputation: **443**

Posted 15 October 2001 - 01:53 PM

quote:Original post by ace_muncher

How do I compare if a unit is within a circular radius around the current unit?

distance=squareroot((difference in x)^2 + (difference in y)^2)

quote:

But how do I mathmatically determine which unit to attack? While pathfinding, should I make an array of coordinates to follow, then go by that after figuring the shortest distance?

eh, this is more of an AI question... a simple way would be to chase the nearest enemy though... for details, ask in the AI forum...

quote:

How do I find the shortest distance?

you could get the distance between the unit and all available targets (using the distance formula above), and store the shortest one...

HTH

--- krez (krezisback@aol.com)

###
#4
Members - Reputation: **443**

Posted 15 October 2001 - 04:23 PM

quote:Original post by ace_muncher

I know that part, but how do I compare if a unit is within this area?

okay...

(X1,Y1) is the coordinates of your unit

(X2,Y2) is the coordinates of the enemy unit you are checking

R is the radius of the circle area you are checking (centered on your unit)

if R^2 <= (x2-x1)^2+(y2-y1)^2 then the enemy unit is within R of your unit

to find the nearest unit, you must store two things: a reference to the nearest-so-far unit, and a number telling how far away it is...

then, set the nearest-so-far reference to the first unit, and the distance number to how far away it is...

then, go through all the other units, getting their distance from your unit. if their distance is less than the one you stored, replace the nearest-so-far reference with a reference to THIS unit, and the distance number with THIS distance. then keep going.

when all the units have been checked, you will have stored the nearest unit, and how far away it is.

this probably isn''t the most efficient way to do this, but it definitely works, and you can work out any optimizations yourself...

good luck!

--- krez (krezisback@aol.com)