Skip to content

Commit

Permalink
Merge pull request #43297 from reduz/directional-light-2d
Browse files Browse the repository at this point in the history
Implement DirectionalLight2D
  • Loading branch information
akien-mga committed Nov 4, 2020
2 parents 3ec10bb + f123981 commit 89f605c
Show file tree
Hide file tree
Showing 16 changed files with 869 additions and 364 deletions.
62 changes: 62 additions & 0 deletions core/math/rect2.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,68 @@ struct Rect2 {
return Rect2(Point2(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0)), size.abs());
}

Vector2 get_support(const Vector2 &p_normal) const {
Vector2 half_extents = size * 0.5;
Vector2 ofs = position + half_extents;
return Vector2(
(p_normal.x > 0) ? -half_extents.x : half_extents.x,
(p_normal.y > 0) ? -half_extents.y : half_extents.y) +
ofs;
}

_FORCE_INLINE_ bool intersects_filled_polygon(const Vector2 *p_points, int p_point_count) const {
Vector2 center = position + size * 0.5;
int side_plus = 0;
int side_minus = 0;
Vector2 end = position + size;

int i_f = p_point_count - 1;
for (int i = 0; i < p_point_count; i++) {
const Vector2 &a = p_points[i_f];
const Vector2 &b = p_points[i];
i_f = i;

Vector2 r = (b - a);
float l = r.length();
if (l == 0.0) {
continue;
}

//check inside
Vector2 tg = r.tangent();
float s = tg.dot(center) - tg.dot(a);
if (s < 0.0) {
side_plus++;
} else {
side_minus++;
}

//check ray box
r /= l;
Vector2 ir(1.0 / r.x, 1.0 / r.y);

// lb is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
// r.org is origin of ray
Vector2 t13 = (position - a) * ir;
Vector2 t24 = (end - a) * ir;

float tmin = MAX(MIN(t13.x, t24.x), MIN(t13.y, t24.y));
float tmax = MIN(MAX(t13.x, t24.x), MAX(t13.y, t24.y));

// if tmax < 0, ray (line) is intersecting AABB, but the whole AABB is behind us
if (tmax < 0 || tmin > tmax || tmin >= l) {
continue;
}

return true;
}

if (side_plus * side_minus == 0) {
return true; //all inside
} else {
return false;
}
}
operator String() const { return String(position) + ", " + String(size); }

Rect2() {}
Expand Down
Loading

0 comments on commit 89f605c

Please sign in to comment.