Skip to content

Commit

Permalink
fix: fix sorting algorithm - crashed the debug versions (#26)
Browse files Browse the repository at this point in the history
- Fixed: 2 equal values could return true (sorting algorithms require that 2
  equal values return false). This caused a assertion error in debug builds and
  thus a crash.
- Fixed: testing equality of floats with zero.
- Simplified a test:
  knowing that dl > 0 and dr > 0,
  (cl * cr < 0) ? (cl > cr) : (dl * cr < dr * cl)
  is equivalent to:
  (dl * cr < dr * cl)
  • Loading branch information
DaymareOn authored May 13, 2022
1 parent a61bf59 commit 6720295
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions hdtSMP64/ActorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,22 @@ namespace hdt
auto dl = a_lhs.m_distanceFromCamera2;
return
// If one of the skeletons is at distance zero (1st person player) from the camera
(dl * dr == 0)
(btFuzzyZero(dl) || btFuzzyZero(dr))
// then it is first.
? (dl == 0)
? (dl < dr)

// If one of the skeletons is exacly on the side of the camera (product of cos = 0)
: (cl * cr == 0)
// If one of the skeletons is exacly on the side of the camera (cos = 0)
: (btFuzzyZero(cl) || btFuzzyZero(cr))
// then it is last.
? (cl != 0)
? abs(cl) > abs(cr)

// If one of the skeletons is behind the camera and the other in front of the camera (product of cos < 0)
: (cl * cr < 0)
// then the one behind the camera is last (the one with cos(angle) < 0).
? (cl > cr)

// Finally, if both are on the same side of the camera (product of cos > 0):
// If both are on the same side of the camera (product of cos > 0):
// we want first the smallest angle (so the highest cosinus), and the smallest distance,
// so we want the smallest distance / cosinus.
// cl = cosinus * distance, dl = distance² => distance / cosinus = dl/cl
// So we want dl/cl < dr/cr.
// Moreover, this test manages the case where one of the skeletons is behind the camera and the other in front of the camera too;
// the one behind the camera is last (the one with cos(angle) = cr < 0).
: (dl * cr < dr * cl);
});

Expand Down

0 comments on commit 6720295

Please sign in to comment.