[Python] Persistent parallel threads in Panda3d, with args at runtime

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm writing a physics game, and I'm trying to speed up my motion calculations. Every tick of the update cycle, I call an rk4 routine which calls an ODE function 4 times, passing updated values for dt/2 and dt etc each of the four calls, to adjust the weighting on integration- so this must run in sequence. However, the ODE itself calculates forces on every particle in the simulation, so I wanted to run those calculations in parallel.

    I hacked together a solution where I have one of panda3d's TaskChains constructed at initialisation, and add in a bunch of tasks every time the ODE is called. That way I can add tasks with

    for i in range(sphereNum):
    taskMgr.add(forceCalculation, "forceCalculation", extraArgs=[r,v,i,dt], taskChain="physTaskChain", sort=0, appendTask=True)


    Obviously, this is extremely inefficient. Py-spy reports that taskMgr.add() is the most expensive function call I'm making, and I did think that the numpy arrays should crunch faster than this! My issue is that if I added my tasks at initialisation, I can't pass arguments to them with r, v, and dt in order to refine weights in rk4 before updating the global position/velocity values. Is there a more elegant way to do this than simply having another layer of arrays for r, v, and a temporary dt variable for my tasks to read/write at runtime?

    I'm also concerned about potential timing issues there too- I'm not sure if the threads will always execute in the right order, and I'm struggling to find a solution that fits. Panda3d's Intervals, Sequencing, and Parallel classes looked relevant, but they don't allow passing data back and forth, and offer much less control over the threading than the taskMgr.

    Continue reading...

Compartilhe esta Página