Bind velocity_func to member method
Posted: Wed Feb 08, 2012 12:14 pm
I've recently started using chipmunks and I really like it.
Except for one problem with OOP: Because chipmunk is written in plain C, I'm not able to bind the velocity_func to a non-static member method (C++). I've tried several workarounds so far but hadn't any luck with this.
I understand that pointer to member methods are a different size, then pointer to functions. Had anyone the same problem and found a Solution? Some code:
That works only, as if the playerUpdateVelocity method is static (4th line, add "static" to the method declaration). What I want to accomplish: bind it to an member function, so that I can creat multiple "Players". Is this even possbile? Or did I made a fatal code design misstake?
Thanks for any help!
Except for one problem with OOP: Because chipmunk is written in plain C, I'm not able to bind the velocity_func to a non-static member method (C++). I've tried several workarounds so far but hadn't any luck with this.
I understand that pointer to member methods are a different size, then pointer to functions. Had anyone the same problem and found a Solution? Some code:
Code: Select all
class Player{
public:
Player();
~Player();
void playerUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt);
protected:
private:
cpBody *playerBody;
};
Player::Player(){
playerBody = cpBodyNew(1.0f, INFINITY);
playerBody->velocity_func = playerUpdateVelocity;
}
void Player::playerUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt){
// do something
}
Thanks for any help!