From cdf770d6950befd25779a18ea3972deeb9f143bb Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Sun, 25 Apr 2021 15:33:53 -0400 Subject: Rectangle intersection with a line complete --- frontend/2d/_collisions/rectangle_line.html | 114 ++++++++++++++++++---------- 1 file changed, 76 insertions(+), 38 deletions(-) (limited to 'frontend/2d/_collisions/rectangle_line.html') diff --git a/frontend/2d/_collisions/rectangle_line.html b/frontend/2d/_collisions/rectangle_line.html index 17d95da..e0cd67e 100644 --- a/frontend/2d/_collisions/rectangle_line.html +++ b/frontend/2d/_collisions/rectangle_line.html @@ -47,44 +47,82 @@ - - -
-

Rectangle-Line

-
-

- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. -

-
- - - -
-
-

References

-
    -
-
-
-
+ + +
+

Rectangle intersection with a Line Segment

+
+

Algorithm

+

+ For each line segment that your rectangle could be intersecting with, + do the following: +

    +
  1. + For each corner of your rectangle, check if the distance from that point to the line is less than some epsilon, where epsilon is a reasonable small number (usually a 1 or 2 units, depending on the size of your lines). +
  2. +
  3. + To check each point, use the "distance from point to line segment" formula, which can be found here (I will not derive it just yet) +
  4. +
  5. + If a collision is found, we have all of the information required to solve the collision: +
      +
    • + Collision Normal: This is the perpendicular to the line segment, which can be found by: + +
      +Vector2 getNormalToLineSegment(LineSegment* segment) {
      +    Vector2 direction = segment->end - segment->start;
      +    return *Vector2 { -direction.y, direction.x }).normalize();
      +}
      +				
      +
      +
    • +
    • + First Point of Application: Get the vector from the center of the rectangle (most like your position) to the corner which intersected. +
    • +
    • + Second Point of Application: Get vector from center of line to the corner which intersected. +
    • +
    +
  6. +
+

+
+
+

+ Live Example +

+
+ + + +
+ +
+
-- cgit v1.2.1