From 1974dcf90f973b645f196f5830bff2aea5e6db20 Mon Sep 17 00:00:00 2001 From: Dany Pignoux Date: Wed, 28 Apr 2021 08:31:20 +0200 Subject: [PATCH 1/2] Fix collision for parallel line/line https://github.com/jeffThompson/CollisionDetection/issues/14 --- .gitignore | 9 ++++++++- CodeExamples/LineLine/LineLine.pde | 4 ++++ CodeExamples/LineLine/web-export/LineLine.pde | 4 ++++ Website/line-line.php | 10 ++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9aeacf2..7b2df2d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,11 @@ ToDo.txt markdown.pl -_OldVersions/ + +# Mac OS/X tools ####################################################################################################### + .DS_Store +_OldVersions/ + +# Jetbrains tools ###################################################################################################### + +.idea diff --git a/CodeExamples/LineLine/LineLine.pde b/CodeExamples/LineLine/LineLine.pde index e3744f1..2263c42 100644 --- a/CodeExamples/LineLine/LineLine.pde +++ b/CodeExamples/LineLine/LineLine.pde @@ -54,6 +54,10 @@ void draw() { // LINE/LINE boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { + // Check if first line points collides with the line 2 + if (!pointLine(x1, y1, x3, y3, x4, y4) && !pointLine(x2, y2, x3, y3, x4, y4)) { + return true; + } // calculate the distance to intersection point float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); diff --git a/CodeExamples/LineLine/web-export/LineLine.pde b/CodeExamples/LineLine/web-export/LineLine.pde index df3c2ab..ecaeadf 100644 --- a/CodeExamples/LineLine/web-export/LineLine.pde +++ b/CodeExamples/LineLine/web-export/LineLine.pde @@ -53,6 +53,10 @@ void draw() { // LINE/LINE boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { + // Check if first line points collides with the line 2 + if (!pointLine(x1, y1, x3, y3, x4, y4) && !pointLine(x2, y2, x3, y3, x4, y4)) { + return true; + } // calculate the direction of the lines float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); diff --git a/Website/line-line.php b/Website/line-line.php index 80b902d..039e722 100644 --- a/Website/line-line.php +++ b/Website/line-line.php @@ -19,6 +19,12 @@ return false; +

To fix parallel line problem described here:

+
if (!pointLine(x1, y1, x3, y3, x4, y4) && !pointLine(x2, y2, x3, y3, x4, y4)) {
+    return true;
+}
+
+

That's it! We can add one more feature, if desired, that will tell us the intersection point of the two lines. This might be useful if, for example, you're making a sword-fighting game and want sparks to fly where the two blades hit.

float intersectionX = x1 + (uA * (x2-x1));
@@ -67,6 +73,10 @@
 
 // LINE/LINE
 boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {
+  // Check if first line points collides with the line 2
+  if (!pointLine(x1, y1, x3, y3, x4, y4) && !pointLine(x2, y2, x3, y3, x4, y4)) {
+    return true;
+  }
 
   // calculate the distance to intersection point
   float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));

From 3e6268ec5c31ef1cf975b3e80ae214ced1edce0a Mon Sep 17 00:00:00 2001
From: Dany Pignoux 
Date: Wed, 28 Apr 2021 09:49:28 +0200
Subject: [PATCH 2/2] Fix collision for parallel line/line

https://github.com/jeffThompson/CollisionDetection/issues/14
---
 CodeExamples/LineLine/LineLine.pde            | 31 ++++++++--------
 CodeExamples/LineLine/web-export/LineLine.pde | 33 +++++++++--------
 Website/line-line.php                         | 37 ++++++++++---------
 3 files changed, 52 insertions(+), 49 deletions(-)

diff --git a/CodeExamples/LineLine/LineLine.pde b/CodeExamples/LineLine/LineLine.pde
index 2263c42..a12a928 100644
--- a/CodeExamples/LineLine/LineLine.pde
+++ b/CodeExamples/LineLine/LineLine.pde
@@ -56,25 +56,26 @@ void draw() {
 boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {
   // Check if first line points collides with the line 2
   if (!pointLine(x1, y1, x3, y3, x4, y4) && !pointLine(x2, y2, x3, y3, x4, y4)) {
-    return true;
-  }
+    // calculate the direction of the lines
+    float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
+    float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
 
-  // calculate the distance to intersection point
-  float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
-  float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
+    // if uA and uB are between 0-1, lines are colliding
+    if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
 
-  // if uA and uB are between 0-1, lines are colliding
-  if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
-    
-    // optionally, draw a circle where the lines meet
-    float intersectionX = x1 + (uA * (x2-x1));
-    float intersectionY = y1 + (uA * (y2-y1));
-    fill(255,0,0);
-    noStroke();
-    ellipse(intersectionX,intersectionY, 20,20);
-    
+      // optionally, draw a circle where the lines meet
+      float intersectionX = x1 + (uA * (x2-x1));
+      float intersectionY = y1 + (uA * (y2-y1));
+      fill(255,0,0);
+      noStroke();
+      ellipse(intersectionX,intersectionY, 20,20);
+
+      return true;
+    }
+  } else {
     return true;
   }
+
   return false;
 }
 
diff --git a/CodeExamples/LineLine/web-export/LineLine.pde b/CodeExamples/LineLine/web-export/LineLine.pde
index ecaeadf..2ec5f94 100644
--- a/CodeExamples/LineLine/web-export/LineLine.pde
+++ b/CodeExamples/LineLine/web-export/LineLine.pde
@@ -55,25 +55,26 @@ void draw() {
 boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {
   // Check if first line points collides with the line 2
   if (!pointLine(x1, y1, x3, y3, x4, y4) && !pointLine(x2, y2, x3, y3, x4, y4)) {
-    return true;
-  }
+    // calculate the direction of the lines
+    float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
+    float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
+
+    // if uA and uB are between 0-1, lines are colliding
+    if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
 
-  // calculate the direction of the lines
-  float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
-  float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
-
-  // if uA and uB are between 0-1, lines are colliding
-  if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
-    
-    // optionally, draw a circle where the lines meet
-    float intersectionX = x1 + (uA * (x2-x1));
-    float intersectionY = y1 + (uA * (y2-y1));
-    fill(255,0,0);
-    noStroke();
-    ellipse(intersectionX,intersectionY, 20,20);
-    
+      // optionally, draw a circle where the lines meet
+      float intersectionX = x1 + (uA * (x2-x1));
+      float intersectionY = y1 + (uA * (y2-y1));
+      fill(255,0,0);
+      noStroke();
+      ellipse(intersectionX,intersectionY, 20,20);
+
+      return true;
+    }
+  } else {
     return true;
   }
+
   return false;
 }
 
diff --git a/Website/line-line.php b/Website/line-line.php
index 039e722..ba6d313 100644
--- a/Website/line-line.php
+++ b/Website/line-line.php
@@ -21,7 +21,7 @@
 
 

To fix parallel line problem described here:

if (!pointLine(x1, y1, x3, y3, x4, y4) && !pointLine(x2, y2, x3, y3, x4, y4)) {
-    return true;
+    // ... Test above code
 }
 
@@ -75,26 +75,27 @@ boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { // Check if first line points collides with the line 2 if (!pointLine(x1, y1, x3, y3, x4, y4) && !pointLine(x2, y2, x3, y3, x4, y4)) { + // calculate the direction of the lines + float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); + float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); + + // if uA and uB are between 0-1, lines are colliding + if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) { + + // optionally, draw a circle where the lines meet + float intersectionX = x1 + (uA * (x2-x1)); + float intersectionY = y1 + (uA * (y2-y1)); + fill(255,0,0); + noStroke(); + ellipse(intersectionX,intersectionY, 20,20); + + return true; + } + } else { return true; } - // calculate the distance to intersection point - float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); - float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); - - // if uA and uB are between 0-1, lines are colliding - if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) { - - // optionally, draw a circle where the lines meet - float intersectionX = x1 + (uA * (x2-x1)); - float intersectionY = y1 + (uA * (y2-y1)); - fill(255,0,0); - noStroke(); - ellipse(intersectionX,intersectionY, 20,20); - - return true; - } - return false; + return true; }