Skip to content

Commit

Permalink
Implement DirectionalLight2D
Browse files Browse the repository at this point in the history
Also separated Light2D in PointLight2D and DirectionalLight2D.
Used PointLight2D because its more of a point, and it does not work
the same as OmniLight (as shape depends on texture).
Added a few utility methods to Rect2D I needed.
  • Loading branch information
reduz committed Nov 4, 2020
1 parent 3ec10bb commit f123981
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

1 comment on commit f123981

@SpiderIerusalem
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why MODE_MASK was removed? What will replace it?

Please sign in to comment.