victorfisac / Physac

2D physics header-only library for videogames developed in C using raylib library.

Home Page:http://www.victorfisac.com/physac

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

High CPU usage

ethan-bmn opened this issue · comments

commented

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?

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();
    }
}

@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. 🙏