victorfisac / Physac

2D physics header-only library for videogames developed in C using raylib library.
http://www.victorfisac.com/physac
MIT License
432 stars 28 forks source link

Performance with 100 contacting circles / FindAvailableManifoldIndex() #62

Open RubendeBruin opened 7 months ago

RubendeBruin commented 7 months ago

When running a small test with 100 colliding circles, the performance is extremely slow. Profiling shows that 99% of the cpu time is consumed by the FindAvailableManifoldIndex() function.

It seems that this function returns the first available manifold id. But it only used from the PhysicsStep function, and that function first deletes all manifolds.

So would the first available manifold id not just be the number of manifolds + 1 ?

like so:

// Finds a valid index for a new manifold initialization
static int FindAvailableManifoldIndex()
{
    const int id = physicsManifoldsCount + 1;

    if (id >= PHYSAC_MAX_MANIFOLDS)
        return -1;
    return id;
}

my test program (cpp)

    SetPhysicsGravity(0, 0);

    // Create obstacle circle physics body
    Vector2 position = { 0, 0 };

    for (float i = 0; i < 100; i++)
    {
        CreatePhysicsBodyCircle({i,i}, 5, 10);
    };

    PhysicsBody floor = CreatePhysicsBodyRectangle({0,-100}, 500, 5, 10);
    floor->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)

    for (int i = 0; i < 5; i++)
    {
        PhysicsStep();
        std::cout << i << std::endl;

    }