Hi, guys,

I was stuck in python callback functions while I swig a dynamics engine to python.

This engine was written in c++, which allows users defines their own forces, e.g. I create a particle system, and now I want to add a random force to it. First I have to write a function:

bool Forces::addRandomForce(const float $dt, void* data)   // user defined random force
    velocities = engine->getVelocities(particleCount);              // engine  = new Engine
    for(i = 0; i < particleCount; i++)
        velocities += randomeVector();

Next, I will pass this function pointer to engine,

void Forces::setup()
    custom = New CustomForce                                              // CustomForce is a class

    custom->addSolverCallback(1, 80, addRandomForce, this);    // add random force to engine.


function addSolverCallback is defined as:

void CustomForce::addSolverCallback(int stage, int order, CUSTOM_FUNCTION f, void* data)


type bool (*CUSTOM_FUNCTION)(const float $dt, void * data)


My current job is to get a python version using swig, but the function pointer / callback stopped me. I expect I can use the functions like

class simulation:

      engine = Engine()
      custom = CustomForce()

      def randomForce(self, scale):
            velocities = engine.getVelocities(particleCount);
            velocities += randomVector()*scale

     def setup(self):
          custom.addSolverCallback(1, 80, randomForce,  self)    // self is correct ???


I have two questions here, 

1. how I can use callback function in python ? 

2. how I handle the "this" pointer ( bold and underline)

I am new to swig, I am not sure if it could be done, anybody has suggestions ???? Thanks.