• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Unit Faces Unit?

Status
Not open for further replies.

Deleted member 219079

D

Deleted member 219079

My current checking method;
JASS:
    private constant function Positive takes real r returns real
        if r < 0 then
            return -r
        endif
        return r
    endfunction
    
    private function UnitFacingUnit takes unit u, unit u2 returns boolean
        return Positive((bj_RADTODEG*Atan2(GetUnitY(u2)-GetUnitY(u),GetUnitX(u2)-GetUnitX(u)))-GetUnitFacing(u))<5.
    endfunction
Does some crazy stuff from time to time.

If nobody can spot anything errors from it, I guess the bug lingers somewhere else in my code...
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Atan2 returns from +180 to -180 if I recall and unit facing is from 0 to 360.

If the unit is facing 179 degrees (almost straight to the left but a tad up) yet the target is at -179 degrees (almost straight to the left but a tad down) the result is -358. When put through your positive function you get...

358 < 5.0 which is false.

However in reality the unit was only 2 degrees off a direct facing which should be true.

The solution is to not test the angle for deviation but the sin/cos of the angle. Unlike the returned angle from Atan2 which is discontinuous, the sin/cos of an angle is continuous and in effect converts the discontinuous angle into a continuous angle based number.

Since you are after deviation from facing the logical one to use is COS which is 1 at 0 and falls to 0 at 90/-90 and to -1 at 180/-180. Thus your entire return condition becomes...

JASS:
    private function UnitFacingUnit takes unit u, unit u2 returns boolean
         return Cos(Atan2(GetUnitY(u2)-GetUnitY(u),GetUnitX(u2)-GetUnitX(u))-bj_DEGTORAD*GetUnitFacing(u))<0.996194
     endfunction

Where 0.99619469809174553229501040247389 is Cos(5degrees).
 
  • Like
Reactions: ILH

Deleted member 219079

D

Deleted member 219079

Is that guaranteed to work? Can't get it to work..
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Is that guaranteed to work?
I do hope so, but there is always the chance of some mistake as I have not tested it.

Check the result returned by
JASS:
Cos(Atan2(GetUnitY(u2)-GetUnitY(u),GetUnitX(u2)-GetUnitX(u))-bj_DEGTORAD*GetUnitFacing(u))

It should be very close to 1 when facing a unit and -1 when facing away from a unit.
 

Deleted member 219079

D

Deleted member 219079

Then the error must be in the following function:
JASS:
private static method dummyMotion takes nothing returns nothing
            local unit u = GetEnumUnit()
            local integer i = GetUnitUserData(u)
            call SetUnitZ(u, DUMMY_HEIGHT + Pythagoras(GetUnitX(u)-GetUnitX(s.target),GetUnitY(u)-GetUnitY(s.target))*0.5)
            if not UnitFacingUnit(u, s.target) then
                    if FACES[i] then
                        set FACE_ANGLE[i] = -FACE_ANGLE[i]
                        set FACES[i] = false
                    endif
                    set FACING[i] = FACING[i] + DUMMY_TURN_SPEED*FACE_ANGLE[i]
                    call SetUnitFacing(u, FACING[i]*bj_RADTODEG)
                    call ChangeMotionAngle(u, FACING[i])
                else
                    set FACES[i] = true
                endif
        endmethod

Erratic movement; the turning stops after a while:
attachment.php


Edit: I know I can't put others to fix my code, I'll probably just dump the whole spell...
 

Attachments

  • asdf.png
    asdf.png
    1.9 MB · Views: 143

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Erratic movement
No movement is erratic. The deterministic nature means it is all predictable but might not make sense. Please post an example map demonstrating this behaviour.

A rough guess is that you may not be converting degrees to radians (or back) correctly somewhere. Using degrees instead of radians will produce pretty crazy and seemingly random angles.
 
Level 12
Joined
Feb 22, 2010
Messages
1,115
The function UnitFacingUnit Dr Super Good told you actually gives inverse result.Use a "not" at the return value.(I will test soon)
 

Deleted member 219079

D

Deleted member 219079

I scrapped the spell, couldn't get it work.

Thanks for all the help anyways...
 
Status
Not open for further replies.
Top