From a32157049d860c4c9c6ffb0df6c3cdbf53e4ef1d Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Mon, 26 Apr 2021 20:51:09 -0400 Subject: Getting started on rectangle collisions --- .../_collisions/rectangle_rectangle.html.content | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 frontend/2d/_collisions/rectangle_rectangle.html.content (limited to 'frontend/2d/_collisions/rectangle_rectangle.html.content') diff --git a/frontend/2d/_collisions/rectangle_rectangle.html.content b/frontend/2d/_collisions/rectangle_rectangle.html.content new file mode 100644 index 0000000..30e34d8 --- /dev/null +++ b/frontend/2d/_collisions/rectangle_rectangle.html.content @@ -0,0 +1,75 @@ + + +
+

Rectangle intersection with a Rectangle

+
+

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