Handmade Hero»Forums»Code
311 posts / 1 project
None
GetBestMatchAssestFrom: handling periodic ranges
Currently, the handling of periodic ranges in GetBestMatchAssestFrom is implemented as

1
2
3
real32 D0 = AbsoluteValue(A - B);
real32 D1 = AbsoluteValue((A - Assets->TagRange[Tag->ID]*SignOf(A)) - B);
real32 Difference = Minimum(D0, D1);


An alternative implementation might be
1
2
3
real32 Difference = AbsoluteValue(A - B);
if (Difference > TagRange[Tag->ID])
   Difference = 2 * TagRange[Tag->ID] - Difference;


I don't think the alternative implementation is necessarily any better. Although, at least for me, it is easier to visualize what is going on with the math.

Thanks!
Mox
32 posts
GetBestMatchAssestFrom: handling periodic ranges
I agree that the alternative might be easier to visualize.

The original does have one additional advantage in it's current form. The TagRange could be set to 0 to indicate a non-periodic tag. Currently Casey set the period to 100,000 or something high. But the code would still work and might even be better with a TagRange of 0.

Of course this non-periodicity could be added in you alternative implementation by adding a test for 0 too, though it is another test.
Casey Muratori
801 posts / 1 project
Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.
GetBestMatchAssestFrom: handling periodic ranges
That point about 0 is a pretty good point... we probably should do it that way...

- Casey