#### Archived

This topic is now archived and is closed to further replies.

# RTS

This topic is 6724 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In an RTS, what is a good way to make units face the appropriate direction? Suppose you have a tank at location 300,300 and you click at 250,300 for the tank to move there. Does anyone know of a way to calculate which angle the unit should face to move in that direction?

##### Share on other sites
First calc the difference
[250,300] - [300,300] = [-50, 0]

P/\             P = destination/  \           O = origin/y   \         a = angle/      \       y = y difference/__x__a_/O     x = x difference

tan(a) = y/x= 0 / -50 = 0
-1
tan = a

oh well something like that

##### Share on other sites
Jrz''s got the idea. Just to try to clarify:

angleToFace = arctan( (destinationY - locationY)/(destinationX - locationX) )

Happy coding.

while( strcmp(reply,flame) )    followThread();

##### Share on other sites
Ok, I have a method worked out to determine the correct direction to face. It requires inverse tangents, but I cannot find a function in the C libraries to caclulate an inverse tangent. Does anyone know of such a function? or how to calcuated an inverse tangent?

##### Share on other sites
There''s different ways to do this. The easiest would be to just
use the standard c atan() or atan2() which you should find in math.h For a more info, check this.

##### Share on other sites
Well,
I am doing a RTS.....and is working...

so for my tank = terestrial unit i first calculate path up to target then i calculate heading each time it cahnges points from my path vector...

for heading i have only 32 options (think u need more?) and i choose them from a Look Up table based on dx/dy and some normalization

one last thing dont forget u have to move gently from one heading to the other going through all intermediate headings

Ohs.. PS for Aerial units jus calculate it at the beginning from dx/dy

Hope this helps

Bogdan

1. 1
Rutin
68
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 21
• 10
• 33
• ### Forum Statistics

• Total Topics
633438
• Total Posts
3011882
• ### Who's Online (See full list)

There are no registered users currently online

×