diff --git a/examples/shapes/shapes_collision_area.c b/examples/shapes/shapes_collision_area.c index c2e2c1e1d..8da2c2c58 100644 --- a/examples/shapes/shapes_collision_area.c +++ b/examples/shapes/shapes_collision_area.c @@ -26,20 +26,14 @@ int main(void) const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [shapes] example - collision area"); + + int r1 = 40; + int c1x = 200; + int c1y = 200; - // Box A: Moving box - Rectangle boxA = { 10, GetScreenHeight()/2.0f - 50, 200, 100 }; - int boxASpeedX = 4; + int r2 = 40; + - // 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 //---------------------------------------------------------- @@ -47,33 +41,11 @@ int main(void) // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { - // Update - //----------------------------------------------------- - // Move box if not paused - if (!pause) boxA.x += boxASpeedX; + + int c2x = GetMouseX(); + int c2y = GetMouseY(); - // 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; //----------------------------------------------------- // Draw @@ -82,25 +54,13 @@ int main(void) ClearBackground(RAYWHITE); - DrawRectangle(0, 0, screenWidth, screenUpperLimit, collision? RED : BLACK); + Color col = BLUE; + if (CheckCollisionCircles((Vector2) { c1x, c1y }, r1, (Vector2) { c2x, c2y }, r2)) col = YELLOW; + else col = BLUE; + DrawCircle(c1x, c1y, r1, RED); + DrawCircle(c2x, c2y, r2, col); - 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); DrawFPS(10, 10); diff --git a/src/rshapes.c b/src/rshapes.c index 058c96863..e9a84834e 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -2233,9 +2233,10 @@ bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, floa float dx = center2.x - center1.x; // X distance between centers float dy = center2.y - center1.y; // Y distance between centers - float distance = sqrtf(dx*dx + dy*dy); // Distance between centers + float distanceSquared = dx * dx + dy * dy; // Distance between centers squared + float radiusSum = radius1 + radius2; - if (distance <= (radius1 + radius2)) collision = true; + collision = (distanceSquared <= (radiusSum * radiusSum)); return collision; }