RandyGaul / cute_headers

Collection of cross-platform one-file C/C++ libraries with no dependencies, primarily used for games
4.33k stars 269 forks source link

Change of behaviour in c2TOI v1.10? #392

Closed kyavi closed 3 months ago

kyavi commented 3 months ago

Previously c2TOI would return a TOI of 0 for shapes already intersecting. Now outside of the case of an impact happening with the given parameters (no intersection and the velocities causing an intersection) the new c2TOIResult always seems to resolve with hit=0 and toi=1.

Is this intended? The only safe way to test seems to be calling c2TOI and then if hit is 0 also call c2XXX_toXXX?

RandyGaul commented 3 months ago

The c2TOI function was replaced with Erin's from Box2D, but, it should still be returning you a TOI of 0 if the shapes are already intersecting. Is it possible to copy + paste in a minimal example here to make it quick to hop on and take a look in the debugger for you?

kyavi commented 3 months ago
image

Where the green square slightly offset to the top right is a.

        c2AABB c2_a = aabb_to_c2AABB(a);
        c2AABB c2_b = aabb_to_c2AABB(b);

        printf("a min: %f, %f\n", c2_a.min.x, c2_a.min.y);
        printf("a max: %f, %f\n", c2_a.max.x, c2_a.max.y);
        printf("b min: %f, %f\n", c2_b.min.x, c2_b.min.y);
        printf("b max: %f, %f\n", c2_b.max.x, c2_b.max.y);

        c2TOIResult result = c2TOI(
                (const void*)&c2_a, C2_TYPE_AABB, nullptr, { 0, 0 },
                (const void*)&c2_b, C2_TYPE_AABB, nullptr, { 0, 0 },
                true
        );

        printf("result p at { %f, %f }\n", result.p.x, result.p.y);
        printf("result m at { %f, %f }\n", result.n.x, result.n.y);
        printf("result hit %d\n", result.hit);
        printf("result toi %f\n", result.toi);

Console output:

a min: 149.000000, 230.000000
a max: 181.000000, 262.000000
b min: 144.000000, 224.000000
b max: 176.000000, 256.000000
result p at { 0.000000, 0.000000 }
result m at { 0.000000, 0.000000 }
result hit 0
result toi 1.000000
RandyGaul commented 3 months ago

https://github.com/RandyGaul/cute_headers/commit/8c73bfe274feb4cc5a4c1f8d48d82059b8df5c24

Let me know if you still have issues!

kyavi commented 3 months ago

Another faulty case.

image
        c2AABB c2_a = aabb_to_c2AABB(a);
        c2Circle c2_b = circle_to_c2Circle(b);

        printf("a min: %f, %f\n", c2_a.min.x, c2_a.min.y);
        printf("a max: %f, %f\n", c2_a.max.x, c2_a.max.y);
        printf("b pos: %f, %f\n", c2_b.p.x, c2_b.p.y);
        printf("b r: %f\n", c2_b.r);

        c2TOIResult result = c2TOI(
                (const void*)&c2_a, C2_TYPE_AABB, nullptr, { 0, 0 },
                (const void*)&c2_b, C2_TYPE_CIRCLE, nullptr, { 0, 0 },
                true
        );

        printf("result p at { %f, %f }\n", result.p.x, result.p.y);
        printf("result n at { %f, %f }\n", result.n.x, result.n.y);
        printf("result hit %d\n", result.hit);
        printf("result toi %f\n", result.toi);
        printf("result iterations %d\n", result.iterations);
a min: 319.000000, 223.000000
a max: 351.000000, 255.000000
b pos: 322.928192, 229.071793
b r: 16.000000
result p at { 0.000000, 0.000000 }
result n at { 0.000000, 0.000000 }
result hit 0
result toi 1.000000
result iterations 0

Occurs when the bottom left corner of the AABB is the only one within the circle and the solver goes through zero iterations.

RandyGaul commented 3 months ago

Awesome, here's another fix https://github.com/RandyGaul/cute_headers/commit/a0368a12064a6711a3271f219a7347d9b4c5b520

kyavi commented 3 months ago

Thank you for the quick fixes 😊 Everything in my tests looks good!

kyavi commented 3 months ago

Actually still a regression here - in the previous case, with the bottom left corner of an AABB intersecting a circle, whilst hit=1 it also returns toi=1 instead of toi=0.

RandyGaul commented 3 months ago

Right yeah added toi = 0 to the if-statement just before the iteration loop. Should be good to go. Thanks for the help!

RandyGaul commented 3 months ago

Please report any other issues you find