A problem to do with Angles

El-Sevio

New Member
Reaction score
0
Hi gents.

Simple problem here;
I want to have a unit slowly rotate, at a constant speed, to face the way the controlling player clicks

The idea is that it is not a one off thing, rather something that will constantly update in the main looping trigger.

the looped script cannot be something as simple as this:
Trigger:
  • Unit - Make unit face point over 2.0 seconds

as it wouldnt be a constant rate of change, a full 180 degree turn would take as long as a simple 5 degree turn


furthermore it cannot be something like this:
JASS:
local real direction
local real angle = AngleBetweenPoints(unit, target)
if angle > direction  then
     set direction = direction + 10
else 
     set direction = direction - 10
endif
call UnitFaceAngle(unit, direction)

as angles are clamped between 0 and 360. If you are facing angle 10 and you click angle 350 then it would go all the way around turning left, instead of turning right which is closest.

So what is an easy, clean way of making a unit choose to either start rotating left or rotating right to eventually be facing the same direction as has been ordered?

Help is much appreciated.
Matt
 

Builder Bob

Live free or don't
Reaction score
249
You will need to convert the angle to an angle between -180 and 180 degrees.

There are multiple ways of doing it. The easiest is probably

Code:
Atan2(sin(angle), cos(angle))

In Starcraft 2 that's Arctangent from deltas with cos(angle) for deltaX and sin(angle) for deltaY.

Other ways of doing it include using modulo, or a sequence of if/then's
 
Reaction score
54
You will need to convert the angle to an angle between -180 and 180 degrees.

There are multiple ways of doing it. The easiest is probably

Code:
Atan2(sin(angle), cos(angle))

In Starcraft 2 that's Arctangent from deltas with cos(angle) for deltaX and sin(angle) for deltaY.

[offtopic]
What is your major/profession? I ask because I'm sure no common folk goes, "Hmm... I need to convert this angle to something between -180 and 180... Think I'll just calculate the Arctangent of the sine and cosine of the angle with deltas."
...unless this is common knowledge and I am broadly uneducated!
 

Builder Bob

Live free or don't
Reaction score
249
[offtopic]
What is your major/profession? I ask because I'm sure no common folk goes, "Hmm... I need to convert this angle to something between -180 and 180... Think I'll just calculate the Arctangent of the sine and cosine of the angle with deltas."
...unless this is common knowledge and I am broadly uneducated!

I'm just a hobby programmer. I used to program robots with minimalistic code for robocode. To make the code as small as possible we had to figure out nifty ways to do stuff, and this method was discovered by one of the members.
 

Strilanc

Veteran Scripter
Reaction score
42
A much more efficient formula is (curAngle - targetAngle + 180) mod 360 - 180. You should create a function AngleDifference which takes the two angles and computes the signed difference.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top