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.
- 
		AuthorPosts
 - 
		
			
				
September 23, 2017 at 6:14 pm #10749
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:
Monkey123456789101112Class SpaceCraft Extends WindowField Me:SpaceCraft..Method RenderSpace()Local spacefilter:=cpShapeFilterNew( ULong(0), Players|MapTiles|Rocks, Players|MapTiles|Rocks)cpSpaceBBQuery(Space, cpBBNew(0, 0, Width, Height), spacefilter, DrawSpaceCallBack, Varptr Me)EndEndNow that doesn’t work, I get an error in C++ when it compiles:
Monkey123Monkey/MonkeyTuts/Spacecraft/Part-2/SpaceCraft.buildv1.1.08/windows_release/src/SpaceCraft_SpaceCraft.cpp:139:184: error: lvalue required as unary '&' operandcpSpaceBBQuery(this->m_Space,cpBBNew(0.0,0.0,bbDouble(this->m_Width()),bbDouble(this->m_Height())),l_spacefilter,g_spacecraft_SpaceCraft_DrawSpaceCallBack,((void*)((&this->m_Me.get()))));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!
September 23, 2017 at 10:04 pm #10754I’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:
Monkey12345678910111213141516171819202122232425262728Class World...Method Add( body:RigidBody )_bodies.Add( body )body.btBody.setUserPointer( object_to_handle( body ) )_btworld.addRigidBody( body.btBody,body.CollisionGroup,body.CollisionMask )EndMethod Remove( body:RigidBody )_btworld.removeRigidBody( body.btBody )body.btBody.setUserPointer( Null )_bodies.Remove( body )EndField _bodies:=New Stack<RigidBody>...EndThis 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!
September 24, 2017 at 6:47 pm #10792Thanks Mark. So is the handle_to_object functions currently in mojo3d only? How do I utilise that to make it work?
September 24, 2017 at 7:59 pm #10793I will be pushing the void ptr change (and removing handle_to_object etc) some time today.
September 24, 2017 at 8:51 pm #10794Ahh I see, cool thanks Mark
September 25, 2017 at 5:29 am #10795Ok, erm, looks like tomorrow sorry!
September 25, 2017 at 7:51 am #10799No worries!
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.