victorfisac / Physac

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

High CPU usage #54

Open ethan-bmn opened 2 years ago

ethan-bmn commented 2 years ago

Whenever I use Physac, the CPU goes up to 40% (and when i'm not calling InitPhysics() it goes back to normal). Any fix for this?

aganm commented 7 months ago

Whenever I use Physac, the CPU goes up to 40% (and when i'm not calling InitPhysics() it goes back to normal). Any fix for this?

Yes there is a fix.

By default, Physac uses a busy loop inside a thread to run the simulation. What this means is that it will peg a CPU core because it never sleeps, no matter how many physics bodies you have, including zero.

The fix is to run Physac manually from within your game loop:

#define PHYSAC_NO_THREADS
#include <physac.h>

int main() {
    InitPhysics();

    bool running = true;
    while (running) { // game loop
        RunPhysicsStep();
    }
}
victorfisac commented 7 months ago

@aganm Calling RunPhysicsStep from the main thread may drive into unexpected physics behaviours because of a lack of accuracy in the different motion deltas.

@ethan-bmn I guess there's some issue related to how is managed the thread loop in some way... or it should sleep at some points...

I have no time to work on this project because of my professional and personal stuff. Please, feel free to open new pull requests as it will contribute to all people starring this repository. 🙏