瀏覽代碼

ADDED: CheckCollisionLines()

Removed function from raymath
raysan5 4 年之前
父節點
當前提交
b78ee13791
共有 3 個文件被更改,包括 26 次插入40 次删除
  1. 2 1
      src/raylib.h
  2. 0 39
      src/raymath.h
  3. 24 0
      src/shapes.c

+ 2 - 1
src/raylib.h

@@ -1123,10 +1123,11 @@ RLAPI void DrawPolyLines(Vector2 center, int sides, float radius, float rotation
 RLAPI bool CheckCollisionRecs(Rectangle rec1, Rectangle rec2);                                           // Check collision between two rectangles
 RLAPI bool CheckCollisionRecs(Rectangle rec1, Rectangle rec2);                                           // Check collision between two rectangles
 RLAPI bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, float radius2);        // Check collision between two circles
 RLAPI bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, float radius2);        // Check collision between two circles
 RLAPI bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec);                         // Check collision between circle and rectangle
 RLAPI bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec);                         // Check collision between circle and rectangle
-RLAPI Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2);                                         // Get collision rectangle for two rectangles collision
 RLAPI bool CheckCollisionPointRec(Vector2 point, Rectangle rec);                                         // Check if point is inside rectangle
 RLAPI bool CheckCollisionPointRec(Vector2 point, Rectangle rec);                                         // Check if point is inside rectangle
 RLAPI bool CheckCollisionPointCircle(Vector2 point, Vector2 center, float radius);                       // Check if point is inside circle
 RLAPI bool CheckCollisionPointCircle(Vector2 point, Vector2 center, float radius);                       // Check if point is inside circle
 RLAPI bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Vector2 p3);               // Check if point is inside a triangle
 RLAPI bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Vector2 p3);               // Check if point is inside a triangle
+RLAPI bool CheckCollisionLines(Vector2 startPos1, Vector2 endPos1, Vector2 startPos2, Vector2 endPos2, Vector2 *collisionPoint); // Check the collision between two lines defined by two points each, returns collision point by reference
+RLAPI Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2);                                         // Get collision rectangle for two rectangles collision
 
 
 //------------------------------------------------------------------------------------
 //------------------------------------------------------------------------------------
 // Texture Loading and Drawing Functions (Module: textures)
 // Texture Loading and Drawing Functions (Module: textures)

+ 0 - 39
src/raymath.h

@@ -336,45 +336,6 @@ RMDEF Vector2 Vector2MoveTowards(Vector2 v, Vector2 target, float maxDistance)
     return result;
     return result;
 }
 }
 
 
-// Get the intersection point of two lines A and B defined by A(p1, p2) and B(p3, p4), return true if it exists, else false
-RMDEF bool Vector2LineIntersect(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, Vector2* pointIntersection)
-{
-    const float div = (p4.y - p3.y)*(p2.x - p1.x) - (p4.x -p3.x)*(p2.y - p1.y);
-
-    if (div == 0.f) return false;
-
-    const float coeff = ((p4.x - p3.x)*(p1.y - p3.y) - (p4.y - p3.y)*(p1.x - p3.x)) / div;
-
-    if (pointIntersection)
-    {
-        pointIntersection->x = p1.x + (p2.x - p1.x) * coeff;
-        pointIntersection->y = p1.y + (p2.y - p1.y) * coeff;
-    }
-    return true;
-}
-
-// Get the intersection point of two segments A and B defined by A(p1, p2) and B(P3, p4), return true if it exists, else false
-RMDEF bool Vector2SegmentIntersect(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, Vector2* pointIntersection)
-{
-    const float div = (p4.y - p3.y)*(p2.x - p1.x) - (p4.x -p3.x)*(p2.y - p1.y);
-
-    if (div == 0.f) return false;
-
-    const float xi = ((p3.x - p3.x)*(p1.x * p2.y - p1.y * p2.x) - (p1.x - p2.x)*(p3.x * p4.y - p3.y * p4.x)) / div;
-    const float yi = ((p3.y - p4.y)*(p1.x * p2.y - p1.y * p2.x) - (p1.y - p2.y)*(p3.x * p4.y - p3.y * p4.x)) / div;
-
-    if (xi < fminf(p1.x, p2.x) || xi > fmaxf(p1.x, p2.x)) return false;
-    if (xi < fminf(p3.x, p4.x) || xi > fmaxf(p3.x, p4.x)) return false;
-    if (yi < fminf(p1.y, p2.y) || yi > fmaxf(p1.y, p2.y)) return false;
-    if (yi < fminf(p3.y, p4.y) || yi > fmaxf(p3.y, p4.y)) return false;
-    if (pointIntersection)
-    {
-        pointIntersection->x = xi;
-        pointIntersection->y = yi;
-    }
-    return true;
-}
-
 //----------------------------------------------------------------------------------
 //----------------------------------------------------------------------------------
 // Module Functions Definition - Vector3 math
 // Module Functions Definition - Vector3 math
 //----------------------------------------------------------------------------------
 //----------------------------------------------------------------------------------

+ 24 - 0
src/shapes.c

@@ -1471,6 +1471,30 @@ bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec)
     return (cornerDistanceSq <= (radius*radius));
     return (cornerDistanceSq <= (radius*radius));
 }
 }
 
 
+// Check the collision between two lines defined by two points each, returns collision point by reference
+bool CheckCollisionLines(Vector2 startPos1, Vector2 endPos1, Vector2 startPos2, Vector2 endPos2, Vector2 *collisionPoint)
+{
+    const float div = (endPos2.y - startPos2.y)*(endPos1.x - startPos1.x) - (endPos2.x - startPos2.x)*(endPos1.y - startPos1.y);
+
+    if (div == 0.0f) return false;      // WARNING: This check could not work due to float precission rounding issues...
+
+    const float xi = ((startPos2.x - startPos2.x)*(startPos1.x*endPos1.y - startPos1.y*endPos1.x) - (startPos1.x - endPos1.x)*(startPos2.x*endPos2.y - startPos2.y*endPos2.x))/div;
+    const float yi = ((startPos2.y - endPos2.y)*(startPos1.x*endPos1.y - startPos1.y*endPos1.x) - (startPos1.y - endPos1.y)*(startPos2.x*endPos2.y - startPos2.y*endPos2.x))/div;
+
+    if (xi < fminf(startPos1.x, endPos1.x) || xi > fmaxf(startPos1.x, endPos1.x)) return false;
+    if (xi < fminf(startPos2.x, endPos2.x) || xi > fmaxf(startPos2.x, endPos2.x)) return false;
+    if (yi < fminf(startPos1.y, endPos1.y) || yi > fmaxf(startPos1.y, endPos1.y)) return false;
+    if (yi < fminf(startPos2.y, endPos2.y) || yi > fmaxf(startPos2.y, endPos2.y)) return false;
+    
+    if (collisionPoint != NULL)
+    {
+        collisionPoint->x = xi;
+        collisionPoint->y = yi;
+    }
+    
+    return true;
+}
+
 // Get collision rectangle for two rectangles collision
 // Get collision rectangle for two rectangles collision
 Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
 Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
 {
 {