Skip to content

[rmodels] Removing powf function from CheckCollisionBoxSphere#5776

Merged
raysan5 merged 1 commit into
raysan5:masterfrom
maiconpintoabreu:removing-pow-collision
Apr 19, 2026
Merged

[rmodels] Removing powf function from CheckCollisionBoxSphere#5776
raysan5 merged 1 commit into
raysan5:masterfrom
maiconpintoabreu:removing-pow-collision

Conversation

@maiconpintoabreu

@maiconpintoabreu maiconpintoabreu commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

Removing powf with a similar way to calculate used in CheckCollisionSpheres

Test:

#import "raylib.h"

int main(void) 
{
    // Define a 2x2x2 box centered at the origin
    BoundingBox box = {
        .min = {-1.0f, -1.0f, -1.0f},
        .max = { 1.0f,  1.0f,  1.0f}
    };

    // Test Case 1: Sphere inside the box
    Vector3 center1 = {0.0f, 0.0f, 0.0f};
    float radius1 = 0.5f;
    
    // Test Case 2: Sphere touching the edge
    Vector3 center2 = {2.0f, 0.0f, 0.0f};
    float radius2 = 1.0f; // Distance is exactly 1.0 from box.max.x

    // Test Case 3: Sphere far away
    Vector3 center3 = {5.0f, 5.0f, 5.0f};
    float radius3 = 1.0f;

    TraceLog(LOG_INFO, "--- Collision Tests ---");
    TraceLog(LOG_INFO, "Test 1 (Inside): %s", CheckCollisionBoxSphere(box, center1, radius1) ? "COLLISION" : "NO COLLISION");
    TraceLog(LOG_INFO, "Test 2 (Touching): %s", CheckCollisionBoxSphere(box, center2, radius2) ? "COLLISION" : "NO COLLISION");
    TraceLog(LOG_INFO, "Test 3 (Far Away): %s", CheckCollisionBoxSphere(box, center3, radius3) ? "COLLISION" : "NO COLLISION");

    return 0;
}

The benchmark on my machine the change did not improve a lot but it should improve on low end devices.

Changed the benchmark tool to use bench.h instead of poop (it was too noise)

1 Call
[==========] Running 2 benchmarks.
[ RUN      ] CollisionSuite.Old_Powf
[       OK ] CollisionSuite.Old_Powf (mean 0.051us, confidence interval +- 0.237557%)
[ RUN      ] CollisionSuite.New_Clamp
[       OK ] CollisionSuite.New_Clamp (mean 0.039us, confidence interval +- 0.582900%)
[==========] 2 benchmarks ran.
[  PASSED  ] 2 benchmarks.

10000 Calls
[==========] Running 2 benchmarks.
[ RUN      ] BatchSuite.Old_Powf
[       OK ] BatchSuite.Old_Powf (mean 148.454us, confidence interval +- 1.816232%)
[ RUN      ] BatchSuite.New_Clamp
[       OK ] BatchSuite.New_Clamp (mean 7.330us, confidence interval +- 2.055348%)
[==========] 2 benchmarks ran.
[  PASSED  ] 2 benchmarks.

@maiconpintoabreu maiconpintoabreu changed the title Making it similar to CheckCollisionSpheres to remove pow function [rmodels] Removing powf function from CheckCollisionBoxSphere Apr 18, 2026
@raysan5 raysan5 merged commit 90e9145 into raysan5:master Apr 19, 2026
16 checks passed
@raysan5

raysan5 commented Apr 19, 2026

Copy link
Copy Markdown
Owner

@maiconpintoabreu thanks, I'm merging but it's not a change I specially like, the improvement is minimum and it adds dependency on other functions, that is something I always try to minimize. Main reason to merge is consistency with CheckCollisionSpheres()...

@maiconpintoabreu

Copy link
Copy Markdown
Contributor Author

@raysan5 thanks, the main reason of the PR was to make it silimar to the other methods and more readable, the benchmark was to make sure that was no performance regression, the numbers are too small to see the improvement indeed.

@maiconpintoabreu maiconpintoabreu deleted the removing-pow-collision branch April 20, 2026 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants