Skip to content

Commit

Permalink
ENGINES: Fix when point aligns with a vertex
Browse files Browse the repository at this point in the history
This is a rare case but is built into the unit test. With this fix
all unit tests pass for trigger.cpp.
  • Loading branch information
rjshae authored and DrMcCoy committed Nov 3, 2018
1 parent 1eb8f13 commit c210147
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/engines/aurora/trigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,22 @@ bool Trigger::isRayIntersect(float x, float y,
float x1, float y1,
float x2, float y2) const {

const float epsilon = 0.00000001;

// Check if y is below max of y1 and y2
if ((y > y1) && (y > y2))
return false;

// Check if x is in (x1, x2)
// On a vertex alignment, add an offset
float x0 = x;
if ((x0 == x1) || (x0 == x2)) {
x0 = x0 + epsilon;
}

// Check if x0 is in [x1, x2]
if (x1 < x2) {
if ((x < x1) || (x > x2))
if ((x0 < x1) || (x0 > x2))
return false;
} else {
if ((x < x2) || (x > x1))
if ((x0 < x2) || (x0 > x1))
return false;
}

Expand All @@ -152,10 +156,10 @@ bool Trigger::isRayIntersect(float x, float y,
if ((dx > epsilon) || (dx < -epsilon)) {
// Get the y intersection coordinate
float m = dy / dx;
float yint = m * (x - x1) + y1;
float yint = m * (x0 - x1) + y1;

// Is the point below the line?
if (y < yint)
// Is the point above the line?
if (!(y > yint))
return true;
} else {
// Aligned with vertical line segment
Expand Down
1 change: 1 addition & 0 deletions src/engines/aurora/trigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Trigger : public Graphics::Renderable {

void prepare();
private:
const float epsilon = 1.0e-5;
bool _prepared;
Common::BoundingBox _boundingbox;

Expand Down

0 comments on commit c210147

Please sign in to comment.