Chipmunk and pointers (revisited)

About Monkey 2 Forums Monkey 2 Programming Help Chipmunk and pointers (revisited)

This topic contains 6 replies, has 2 voices, and was last updated by  peterigz 1 year, 6 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #10749

    peterigz
    Participant

    I posted earlier on this year about the best way to pass pointer of the class in a chipmunk query and ended up using a “Me” field which worked well:

    Now that doesn’t work, I get an error in C++ when it compiles:

    I didn’t expect it to always work as Mark mentioned he’d run into similar issues with mojo3d and bullet physics and come up with a way of doing things better. So the question is now, what’s the best way to pass a monkey object pointer?

    Example code can be found here if it helps: https://github.com/peterigz/Monkey2Tutorials/tree/master/Spacecraft/Part-2

    Thanks!

    #10754

    Mark Sibly
    Keymaster

    I’ve thought about this a bit and I reckon the best thing to do is to just allow (explicit) object->void ptr->object casting.

    This is basically what I did in mojo3d-physics. There’s some native code in there that casts to/from ptr via object_to_handle and handle_to_object functions.

    You have to be careful of course. It’s up to you to keep the underlying object ‘alive’, and if you fail to do so and it gets GC’d you’re toast.

    I handle this in mojo3d-physics by setting native user data to monkey2 object only when monkey2 object is added to world, and setting userdata back to 0 when it’s removed, eg:

    This way, btBody userdata is only non null if RigidBody is safely being kept alive in the World _bodies stack.

    I was in fact thinking of adding a safer GCLock()/GCUnlock() system or similar and may yet still do so, but this is clearly a popular feature request so lets go nuts!

    #10792

    peterigz
    Participant

    Thanks Mark. So is the handle_to_object functions currently in mojo3d only? How do I utilise that to make it work?

    #10793

    Mark Sibly
    Keymaster

    I will be pushing the void ptr change (and removing handle_to_object etc) some time today.

    #10794

    peterigz
    Participant

    Ahh I see, cool thanks Mark 🙂

    #10795

    Mark Sibly
    Keymaster

    Ok, erm, looks like tomorrow sorry!

    #10799

    peterigz
    Participant

    No worries!

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.