From 925e369c8960f8fd644f192bffb819c215f49368 Mon Sep 17 00:00:00 2001 From: kai-z99 Date: Wed, 29 May 2024 18:24:27 -0700 Subject: [PATCH 1/5] inital function --- src/raylib.h | 1 + src/rshapes.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/raylib.h b/src/raylib.h index b870cc6ca..1758e7b80 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1295,6 +1295,7 @@ RLAPI bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Ve RLAPI bool CheckCollisionPointPoly(Vector2 point, Vector2 *points, int pointCount); // Check if point is within a polygon described by array of vertices 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 bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshold); // Check if point belongs to line created between two points [p1] and [p2] with defined margin in pixels [threshold] +RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Vector2 p2); RLAPI Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2); // Get collision rectangle for two rectangles collision //------------------------------------------------------------------------------------ diff --git a/src/rshapes.c b/src/rshapes.c index 80df64e2d..a786e744f 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -2315,6 +2315,38 @@ bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshol return collision; } +RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Vector2 p2) +{ + //test if end points are in circle + bool inside1 = CheckCollisionPointCircle(p1, center, radius); + bool inside2 = CheckCollisionPointCircle(p2, center, radius); + if (inside1 || inside2) return true; + + // get length of line + float dx = p1.x - p2.x; + float dy = p1.y - p2.y; + float length = sqrtf((dx * dx) + (dy * dy)); + + // dot product + float dotProduct = (((center.x - p1.x) * (p2.x - p1.x)) + ((center.y - p1.y) * (p2.y - p1.y))) / (length * length); + + // closest point on line + float closeX = p1.x + (dotProduct * (p2.x - p1.x)); + float closeY = p1.y + (dotProduct * (p2.y - p1.y)); + + bool onSegment = CheckCollisionPointLine((Vector2) { closeX, closeY }, p1, p2, 0); + if (!onSegment) return false; + + //distnce from circle to closest point + + dx = closeX - center.x; + dy = closeY - center.y; + float distance = sqrtf((dx * dx) + (dy * dy)); + + if (distance <= radius) return true; + return false; +} + // Get collision rectangle for two rectangles collision Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) { From 747f9d9f1b573c9c34b468986ecf595084be98d2 Mon Sep 17 00:00:00 2001 From: kai-z99 Date: Wed, 29 May 2024 18:35:48 -0700 Subject: [PATCH 2/5] working 1 --- examples/shapes/shapes_collision_area.c | 83 +++++++------------------ src/rshapes.c | 5 +- 2 files changed, 26 insertions(+), 62 deletions(-) diff --git a/examples/shapes/shapes_collision_area.c b/examples/shapes/shapes_collision_area.c index c2e2c1e1d..99c9df7e4 100644 --- a/examples/shapes/shapes_collision_area.c +++ b/examples/shapes/shapes_collision_area.c @@ -27,81 +27,42 @@ int main(void) InitWindow(screenWidth, screenHeight, "raylib [shapes] example - collision area"); - // Box A: Moving box - Rectangle boxA = { 10, GetScreenHeight()/2.0f - 50, 200, 100 }; - int boxASpeedX = 4; - - // Box B: Mouse moved box - Rectangle boxB = { GetScreenWidth()/2.0f - 30, GetScreenHeight()/2.0f - 30, 60, 60 }; - - Rectangle boxCollision = { 0 }; // Collision rectangle - - int screenUpperLimit = 40; // Top menu limits - - bool pause = false; // Movement pause - bool collision = false; // Collision detection SetTargetFPS(60); // Set our game to run at 60 frames-per-second //---------------------------------------------------------- + + Vector2 p0 = { 0,0 }; + Vector2 p1 = { 300,300 }; + + + float r = 20.0f; + + Color col = RED; + + + // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { - // Update - //----------------------------------------------------- - // Move box if not paused - if (!pause) boxA.x += boxASpeedX; - - // Bounce box on x screen limits - if (((boxA.x + boxA.width) >= GetScreenWidth()) || (boxA.x <= 0)) boxASpeedX *= -1; - - // Update player-controlled-box (box02) - boxB.x = GetMouseX() - boxB.width/2; - boxB.y = GetMouseY() - boxB.height/2; - - // Make sure Box B does not go out of move area limits - if ((boxB.x + boxB.width) >= GetScreenWidth()) boxB.x = GetScreenWidth() - boxB.width; - else if (boxB.x <= 0) boxB.x = 0; - - if ((boxB.y + boxB.height) >= GetScreenHeight()) boxB.y = GetScreenHeight() - boxB.height; - else if (boxB.y <= screenUpperLimit) boxB.y = (float)screenUpperLimit; - - // Check boxes collision - collision = CheckCollisionRecs(boxA, boxB); - - // Get collision rectangle (only on collision) - if (collision) boxCollision = GetCollisionRec(boxA, boxB); - - // Pause Box A movement - if (IsKeyPressed(KEY_SPACE)) pause = !pause; + Vector2 c = { GetMouseX(),GetMouseY() }; //----------------------------------------------------- + if (CheckCollisionCircleLine(c, r, p0, p1)) + { + col = BLUE; + } + else + { + col = RED; + } // Draw //----------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); - - DrawRectangle(0, 0, screenWidth, screenUpperLimit, collision? RED : BLACK); - - DrawRectangleRec(boxA, GOLD); - DrawRectangleRec(boxB, BLUE); - - if (collision) - { - // Draw collision area - DrawRectangleRec(boxCollision, LIME); - - // Draw collision message - DrawText("COLLISION!", GetScreenWidth()/2 - MeasureText("COLLISION!", 20)/2, screenUpperLimit/2 - 10, 20, BLACK); - - // Draw collision area - DrawText(TextFormat("Collision Area: %i", (int)boxCollision.width*(int)boxCollision.height), GetScreenWidth()/2 - 100, screenUpperLimit + 10, 20, BLACK); - } - - // Draw help instructions - DrawText("Press SPACE to PAUSE/RESUME", 20, screenHeight - 35, 20, LIGHTGRAY); - + DrawLine(p0.x, p0.y, p1.x, p1.y, BLACK); + DrawCircle(c.x, c.y, r, col); DrawFPS(10, 10); EndDrawing(); diff --git a/src/rshapes.c b/src/rshapes.c index a786e744f..3d97c7d09 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -2334,7 +2334,10 @@ RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Ve float closeX = p1.x + (dotProduct * (p2.x - p1.x)); float closeY = p1.y + (dotProduct * (p2.y - p1.y)); - bool onSegment = CheckCollisionPointLine((Vector2) { closeX, closeY }, p1, p2, 0); + + Vector2 close = { closeX, closeY }; + + bool onSegment = CheckCollisionPointLine(close, p1, p2, 1); if (!onSegment) return false; //distnce from circle to closest point From ba934b16c17901c87b19f15cc70c2cb1a0c9e4a7 Mon Sep 17 00:00:00 2001 From: kai-z99 Date: Wed, 29 May 2024 19:00:37 -0700 Subject: [PATCH 3/5] optimize --- examples/shapes/shapes_collision_area.c | 2 +- src/rshapes.c | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/shapes/shapes_collision_area.c b/examples/shapes/shapes_collision_area.c index 99c9df7e4..6a0f9e94e 100644 --- a/examples/shapes/shapes_collision_area.c +++ b/examples/shapes/shapes_collision_area.c @@ -32,7 +32,7 @@ int main(void) //---------------------------------------------------------- - Vector2 p0 = { 0,0 }; + Vector2 p0 = { 100,100 }; Vector2 p1 = { 300,300 }; diff --git a/src/rshapes.c b/src/rshapes.c index 3d97c7d09..c34547dd1 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -2325,18 +2325,18 @@ RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Ve // get length of line float dx = p1.x - p2.x; float dy = p1.y - p2.y; - float length = sqrtf((dx * dx) + (dy * dy)); + float length_sqr = ((dx * dx) + (dy * dy)); // dot product - float dotProduct = (((center.x - p1.x) * (p2.x - p1.x)) + ((center.y - p1.y) * (p2.y - p1.y))) / (length * length); + float dotProduct = (((center.x - p1.x) * (p2.x - p1.x)) + ((center.y - p1.y) * (p2.y - p1.y))) / (length_sqr); // closest point on line float closeX = p1.x + (dotProduct * (p2.x - p1.x)); float closeY = p1.y + (dotProduct * (p2.y - p1.y)); - Vector2 close = { closeX, closeY }; + //is the point on the segment? bool onSegment = CheckCollisionPointLine(close, p1, p2, 1); if (!onSegment) return false; @@ -2344,9 +2344,9 @@ RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Ve dx = closeX - center.x; dy = closeY - center.y; - float distance = sqrtf((dx * dx) + (dy * dy)); + float distance_sqr = ((dx * dx) + (dy * dy)); - if (distance <= radius) return true; + if (distance_sqr <= radius * radius) return true; return false; } From 6ad402a39df907befc4cd8aa3c6a9b653bab409c Mon Sep 17 00:00:00 2001 From: kai-z99 Date: Wed, 29 May 2024 19:30:55 -0700 Subject: [PATCH 4/5] optimized dot product --- src/rshapes.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/rshapes.c b/src/rshapes.c index c34547dd1..6f25c3f09 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -2317,11 +2317,6 @@ bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshol RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Vector2 p2) { - //test if end points are in circle - bool inside1 = CheckCollisionPointCircle(p1, center, radius); - bool inside2 = CheckCollisionPointCircle(p2, center, radius); - if (inside1 || inside2) return true; - // get length of line float dx = p1.x - p2.x; float dy = p1.y - p2.y; @@ -2330,16 +2325,20 @@ RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Ve // dot product float dotProduct = (((center.x - p1.x) * (p2.x - p1.x)) + ((center.y - p1.y) * (p2.y - p1.y))) / (length_sqr); + if (dotProduct > 1.0f) + { + dotProduct = 1.0f; + } + else if (dotProduct < 0.0f) + { + dotProduct = 0.0f; + } + // closest point on line float closeX = p1.x + (dotProduct * (p2.x - p1.x)); float closeY = p1.y + (dotProduct * (p2.y - p1.y)); - Vector2 close = { closeX, closeY }; - //is the point on the segment? - bool onSegment = CheckCollisionPointLine(close, p1, p2, 1); - if (!onSegment) return false; - //distnce from circle to closest point dx = closeX - center.x; From 6b122259cf32445a5b6d242acfc29cf065085bd4 Mon Sep 17 00:00:00 2001 From: kai-z99 Date: Wed, 29 May 2024 19:41:01 -0700 Subject: [PATCH 5/5] simplify --- src/rshapes.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rshapes.c b/src/rshapes.c index 6f25c3f09..774275496 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -2335,8 +2335,9 @@ RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Ve } // closest point on line - float closeX = p1.x + (dotProduct * (p2.x - p1.x)); - float closeY = p1.y + (dotProduct * (p2.y - p1.y)); + float closeX = p1.x - (dotProduct * (dx)); + float closeY = p1.y - (dotProduct * (dy)); + Vector2 close = { closeX, closeY }; //distnce from circle to closest point