Validate maximum difference between two angles: Difference between revisions

From EDukeWiki
Jump to navigation Jump to search
Sangman (talk | contribs)
Added tutorial for something that was doing my head in last night
 
Sangman (talk | contribs)
mNo edit summary
 
Line 43: Line 43:
         set RETURN 1
         set RETURN 1
ends
ends
</pre>
The state can be called like this
<pre>
    // determine some angles, store in ANGLE_A and ANGLE_B
    set MAX_ANGLE_DIFF 512
    state check_max_angle_diff
    // Return 0 means we're within 512 angle units
    ife RETURN 0
    {
        // do stuff
    }
    // or if we're outside the range
    else
    {
        // do other stuff
    }
</pre>
</pre>


[[Category: Tutorials]]
[[Category: Tutorials]]

Latest revision as of 08:03, 6 April 2021

In this example code, we will validate that angle A does not exceed angle B by 512 units. If it does, RETURN is set to 1. It assumes temp, temp2 and temp3 variables are declared. The tricky part in this comparison is that angles wrap back around to 0 when reaching 2048, this must be taken into account when comparing angle difference.

var MAX_ANGLE_DIFF 0 2
var ANGLE_A 0 2
var ANGLE_B
defstate check_max_angle_diff
    set temp2 ANGLE_B
    sub temp2 MAX_ANGLE_DIFF

    set temp3 ANGLE_B
    add temp3 MAX_ANGLE_DIFF
	
    ifl temp2 0
        add temp2 2048

    ifge temp3 2048
        sub temp3 2048
		
    set temp 0
    set RETURN 0
    ifl temp3 temp2
    {
        // we wrapped around the 0/2048 line, either case is enough
        ifl ANGLE_A temp3
            set temp 1
        else ifg ANGLE_A temp2
            set temp 1
    }
    else
    {
        // we didn't wrap around so needs to be inbetween [temp2-temp3]
        ifge ANGLE_A temp2
            ifle ANGLE_A temp3
            set temp 1
    }

    ife temp 1
        set RETURN 0
    else
        set RETURN 1
ends

The state can be called like this

    // determine some angles, store in ANGLE_A and ANGLE_B
    set MAX_ANGLE_DIFF 512
    state check_max_angle_diff
    // Return 0 means we're within 512 angle units
    ife RETURN 0
    {
        // do stuff
    }
    // or if we're outside the range
    else
    {
        // do other stuff
    }