Unit Face Mouse System

  • Like
Reactions: deepstrasz
Allows one to force a unit to turn to face the position of the mouse cursor. Supports customizable turn rates and executing code while a unit is being affected by the system.

Credits:
MyPad for MouseUtils

Changelog:
1.0 Initial release
1.1 Updated with new math from Antares
1.2 Code refactor
1.3 Made it into a VJASS library
Contents

Unit Face Mouse System (Map)

Reviews
Antares
Simple system. Works well. Approved
Your method of determining the turn direction fails at the 360° - 0° transition point.

You can use this function to determine the sign of the angle difference:
JASS:
function AngleBetweenAngles takes real a, real b returns real
	set b = b - a
	if b < 0 then
		set b = b + 2*bj_PI
	elseif b > 2*bj_PI then
		set b = b - 2*bj_PI
	endif
	if b > bj_PI then
		return b - 2*bj_PI //less than 0.
	else
		return 2*bj_PI - b //greater than 0.
	endif
endfunction
 
Last edited:
Your method of determining the turn direction fails at the 360° - 0° transition point.

You can use this function to determine the sign of the angle difference:
JASS:
function AngleBetweenAngles takes real a, real b returns real
    set b = b - a
    if b < 0 then
        set b = b + 2*bj_PI
    elseif b > 2*bj_PI then
        set b = b - 2*bj_PI
    endif
    if b > bj_PI then
        return b - 2*PI //less than 0.
    else
        return 2*PI - b //greater than 0.
    endif
    return b
endfunction
Thanks. I was basically brute forcing it trying to figure out the right math so this will be helpful.

edit: Actually can you explain this a little more? The purpose of this line:
JASS:
local boolean addangle = unitfacing - finalangle < 0
which I assume you want me to replace is to determine which direction to turn to face the desired angle faster than the other direction
 
Last edited:
When the unit is facing 359° and the mouse angle is 1°, then the angle difference is greater than 0, so your addangle boolean will be false. The unit will turn clockwise, although there are only 2° left to turn in counter-clockwise direction.

The AngleBetweenAngles function gets rid of that discontinuity problem in the first half. Then, it checks if the angle difference is greater than pi (faster to travel from a to b clockwise => angle is negative) or smaller than pi (faster to travel from a to b counter-clockwise => angle is positive).

Have not double-checked my signs here, just for explanation.
 
When the unit is facing 359° and the mouse angle is 1°, then the angle difference is greater than 0, so your addangle boolean will be false. The unit will turn clockwise, although there are only 2° left to turn in counter-clockwise direction.

The AngleBetweenAngles function gets rid of that discontinuity problem in the first half. Then, it checks if the angle difference is greater than pi (faster to travel from a to b clockwise => angle is negative) or smaller than pi (faster to travel from a to b counter-clockwise => angle is positive).

Have not double-checked my signs here, just for explanation.
You'll probably just have to tell me what the new calculate function should look like since I'm not sure how to use the function you've given me
 
Here, maybe this version is clearer:
JASS:
function ShouldAddAngle takes real angle1, real angle2 returns boolean
	local real angleDiff = angle2 - angle1
	if angleDiff < 0 then
		set angleDiff = angleDiff + 2*bj_PI
	elseif angleDiff > 2*bj_PI then
		set angleDiff = angleDiff - 2*bj_PI
	endif
    return angleDiff < bj_PI
endfunction
Again, sign may be inverted.
 
Here, maybe this version is clearer:
JASS:
function ShouldAddAngle takes real angle1, real angle2 returns boolean
    local real angleDiff = angle2 - angle1
    if angleDiff < 0 then
        set angleDiff = angleDiff + 2*bj_PI
    elseif angleDiff > 2*bj_PI then
        set angleDiff = angleDiff - 2*bj_PI
    endif
    return angleDiff < bj_PI
endfunction
Again, sign may be inverted.
Updated the system
 
The function still doesn't work. My function expects angles in radians, but your system calculates it in degrees. You need to replace the radians with degrees inside the ShouldAddAngle function. Also, the sign is inverted as I suspected.

You have a three-line library dependency. That's really unnecessary and you can include it in your main script to make the importing easier. It's also good practice to put a scope/library around your script and make the functions that don't need to be exposed private.
 
The function still doesn't work. My function expects angles in radians, but your system calculates it in degrees. You need to replace the radians with degrees inside the ShouldAddAngle function. Also, the sign is inverted as I suspected.

You have a three-line library dependency. That's really unnecessary and you can include it in your main script to make the importing easier. It's also good practice to put a scope/library around your script and make the functions that don't need to be exposed private.
You've basically lost me with the math at this point since math was never my subject. Can you just post the code with the correct math please?
 
Your resource is not a pure JASS resource, because you're using MouseUtils, which requires vJASS. Given that, I don't understand why you're not putting your system inside of a library. You're doing this for all of your other systems. This will prevent any name clashes as well as any no-forward-declaration errors that a user might experience. The AngleBetweenPointsReal function should be put inside of your library. No need for a three-line dependency.

If you can fix those issues, I'll approve it right away.
 
Top