About Monkey 2 › Forums › Monkey 2 Programming Help › Chipmunk queries
Tagged: chipmunk
This topic contains 20 replies, has 4 voices, and was last updated by 
 abakobo
 2 years, 2 months ago.
- 
		AuthorPosts
 - 
		
			
				
January 16, 2017 at 11:39 pm #6571
Having trouble figuring out how to use cpSpaceBBQuery (and I assume I’ll have trouble with the other queries as well). I guess I’m not using pointers properly but not sure how, anyone any ideas? Getting the error:
Can’t find overload for ‘cpSpaceBBQuery:Void(chipmunk.cpSpace,chipmunk.cpBB,chipmunk.cpShapeFilter,Void(chipmunk.cpShape,Void Ptr),Void Ptr)’ with argument types (chipmunk.cpSpace,chipmunk.cpBB,chipmunk.cpShapeFilter,Void(chipmunk.cpShape,Void),Void Ptr)
Have tried with lambda, function pointer and various other things. I’d like to pass in some meaningful data as well… test code:
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586#Import "<std>"#Import "<mojo>"#Import "<chipmunk>"#Import "chipmunkdebugger"Using std..Using mojo..Using chipmunk..Class HelloChipmunk Extends WindowField space:cpSpaceField ground:cpShapeField ballBody:cpBodyField ballShape:cpShapeField ballBody2:cpBodyField ballShape2:cpShapeField polyBody:cpBodyField polyShape:cpShapeField debugger:=New ChipmunkDebuggerMethod New()ClearColor=Color.Black'Create a new space and set its gravity to 100'space=cpSpaceNew()space.Gravity=cpv( 0,1000 )EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()Const timeStep:=1.0/60.0space.StepTime( timeStep )' Local rot:=ballBody.Rotation' Local pos:=ballBody.Position' Local vel:=ballBody.Velocity' Print "ball rot="+ATan2( rot.y,rot.x )+", pos.x="+pos.x+", pos.y="+pos.y+", vel.x="+vel.x+", vel.y="+vel.yLocal spacefilter:=cpShapeFilterNew(0, 1, 1 )Local Test:voidcpSpaceBBQuery(space, cpBBNew(0,0,640,480), spacefilter, Lambda:Void(shape:cpShape, p:Void)End, Varptr Test)canvas.Translate( Width/2, Height/2 )debugger.DebugDraw( canvas,space )EndFunction CallBack:Void(shape:cpShape, p:Void)EndMethod Cleanup() 'Yeah, right!cpShapeFree( ballShape )cpBodyFree( ballBody )cpShapeFree( ground )cpSpaceFree( space )EndEndFunction Main()New AppInstanceNew HelloChipmunkApp.Run()EndThe chipmunk docs for AABB queries is here for reference: http://chipmunk-physics.net/release/ChipmunkLatest-Docs/#Queries-AABBQueries
January 16, 2017 at 11:47 pm #6572initial guess would be to try using a function instead of a lambda ?
January 16, 2017 at 11:54 pm #6573I tried that first, see the CallBack function in the class, but unfortunately same issue. The void pointers are confusing me, as far as I know they are function pointers but in the chipmunk docs it seems you can pass data. I’m don’t know much about c though!
January 16, 2017 at 11:57 pm #6574afaik you’d still need to cast the value to a void pointer tho… better yet a void pointer to a struct
– sorry the lambda was only a guess, do let us all know how you get on, examples of stuff like this would be rather useful…
January 17, 2017 at 12:24 am #6576Thanks. It was Ptr I was missing, this compiles ok now, will see how it goes with an actual sample and data going through it:
Monkey123cpSpaceBBQuery(space, cpBBNew(0,0,640,480), spacefilter, Lambda:Void(shape:cpShape, p:Void Ptr)End, Varptr Test)Works with the callback as well.
January 17, 2017 at 1:26 am #6577Next problem is the UserData in cpShape, I’m having trouble casting it to anything. It does equal something but gives a memory access error if I try and access it. This feels like a bug in Monkey now but not sure:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107#Import "<std>"#Import "<mojo>"#Import "<chipmunk>"#Import "chipmunkdebugger"Using std..Using mojo..Using chipmunk..Struct somedataField value:IntEndClass HelloChipmunk Extends WindowField space:cpSpaceField ground:cpShapeField ballBody:cpBodyField ballShape:cpShapeField ballBody2:cpBodyField ballShape2:cpShapeField polyBody:cpBodyField polyShape:cpShapeField debugger:=New ChipmunkDebuggerMethod New()ClearColor=Color.Black'Create a new space and set its gravity to 100'space=cpSpaceNew()space.Gravity=cpv( 0,1000 )Local data:=New MyClassLocal body:= cpBodyNewStatic()body.Position = cpv(100,100)space.AddBody(body)Local shape:= cpBoxShapeNew(body, 50, 50, 0.0)shape.Friction = 1shape.UserData = Varptr(data)space.AddShape(shape)EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()Const timeStep:=1.0/60.0space.StepTime( timeStep )' Local rot:=ballBody.Rotation' Local pos:=ballBody.Position' Local vel:=ballBody.Velocity' Print "ball rot="+ATan2( rot.y,rot.x )+", pos.x="+pos.x+", pos.y="+pos.y+", vel.x="+vel.x+", vel.y="+vel.yLocal spacefilter:=cpShapeFilterNew(0, 1, 1 )Local Test:=SelfcpSpaceBBQuery(space, cpBBNew(0,0,640,480), spacefilter, CallBack, Varptr Test)canvas.Translate( Width/2, Height/2 )debugger.DebugDraw( canvas,space )EndFunction CallBack:Void(shape:cpShape, p:Void Ptr)Local data:=Cast<MyClass Ptr>(shape.UserData)[0]Local app:=Cast<HelloChipmunk Ptr>(p)[0]If dataPrint data.TestEndEndMethod Cleanup() 'Yeah, right!cpShapeFree( ballShape )cpBodyFree( ballBody )cpShapeFree( ground )cpSpaceFree( space )EndEndClass MyClassField Test:String = "Something"EndFunction Main()New AppInstanceNew HelloChipmunkApp.Run()EndThe App object that I’m passing in seems to cast fine though so that’s something.
January 17, 2017 at 8:38 am #6578[/crayon]Monkey123[crayon-5cb9cf78deda9101174735 inline="true" ]/home/chris/development/monkey2-projects/chipmonk-test/test1.monkey2 [66] : Error : Can't find overload for 'cpShapeFilterNew:chipmunk.cpShapeFilter(Ulong,Uint,Uint)' with argument types (Int,Int,Int)for
[/crayon]Monkey123[crayon-5cb9cf78dedaf870250441 inline="true" ]Local spacefilter:=cpShapeFilterNew(0, 1, 1 )(Linux difference??)
so you might beat me to it, but you’re trying to print data.Test when it should probably be a ptr ? (initial guess!) edit: missed the [0] doh so not that…
January 17, 2017 at 8:56 am #6579if you do this
[/crayon]Monkey1234[crayon-5cb9cf78e28eb724780249 inline="true" ]Local data:=New MyClassPrint data.Testthen the whole thing works
I had to do this tho!
[/crayon]Monkey123[crayon-5cb9cf78e28f1087427067 inline="true" ]Local spacefilter:=cpShapeFilterNew(ULong(0), UInt(1), UInt(1) )which seems wrong!
I think it might possibly be to do with string assignment and class creating but even if I assign Test in a MyClass’s new Method (and print it) the issue still exists the only thing that seems to work is if I print the field from outside the class
I’d say this is defiantly one to report on github
btw assume you know you can set body userdata ?
Probably teaching grannie to suck eggs here but I’d recommend wrapping all the body specific stuff into its own class and keeping it in its own seperate file, that way you can just create/draw and object and keep all the chipmonk stuff out of your main code. you could probably set a class global (static) field with things like world etc that you need for object creation…
January 17, 2017 at 11:11 am #6580I haven’t read all the topic… but I made some little things with chipmunk: https://github.com/abakobo/chipmunk_monkey2
It includes a modified hellochipmunk.monkey2 with MouseJoint/DistanceQuery and a debugdraw with rounded end fat segment and rounded corner polys that were missing on Mark’s debugdraw.
The debugdraw also includes a “complete”/”fast draw” and a camera using affineMat3 matrix that you can test in the stress_test example. I plan to work on it a bit more but have no time for now..For the mouse joint I use filter this way:
[/crayon]Monkey1234[crayon-5cb9cf78e64b6075545116 inline="true" ]' This Filter is for collision and queries filtering, it's the filter received by default by shapes and shall collide with any other.' The two last parameter are shown as bits because they are bitmasksLocal FullGrabFilter:=cpShapeFilterNew(ULong(0),UInt(StringToULong("11111111111111111111111111111111",2)),UInt(StringToULong("11111111111111111111111111111111",2)))Used stringToULong because there’s no stringToUInt…
No data however..
As c2mx2 is not copying the api docs I use the original official C api doc: https://chipmunk-physics.net/release/ChipmunkLatest-API-Reference/
January 17, 2017 at 1:21 pm #6581Thanks for looking at this. Still the same issue here, I’m on Windows 10 so looks like there’s differences in behaviour as I don’t get the error on the filter (maybe I should?). Will post on GitHub as I think there’s something odd going on. This is the current test code with a separate function for a sanity check:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117#Import "<std>"#Import "<mojo>"#Import "<chipmunk>"#Import "chipmunkdebugger"Using std..Using mojo..Using chipmunk..Struct somedataField value:IntEndClass HelloChipmunk Extends WindowField space:cpSpaceField ground:cpShapeField ballBody:cpBodyField ballShape:cpShapeField ballBody2:cpBodyField ballShape2:cpShapeField polyBody:cpBodyField polyShape:cpShapeField something:String = "App String"Field debugger:=New ChipmunkDebuggerMethod New()ClearColor=Color.Black'Create a new space and set its gravity to 100'space=cpSpaceNew()space.Gravity=cpv( 0,1000 )Local data:=New MyClassLocal body:= cpBodyNewStatic()body.Position = cpv(100,100)space.AddBody(body)Local shape:= cpBoxShapeNew(body, 50, 50, 0.0)shape.Friction = 1shape.UserData = Varptr(data)space.AddShape(shape)TestCallBack(Varptr(data))EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()Const timeStep:=1.0/60.0space.StepTime( timeStep )' Local rot:=ballBody.Rotation' Local pos:=ballBody.Position' Local vel:=ballBody.Velocity' Print "ball rot="+ATan2( rot.y,rot.x )+", pos.x="+pos.x+", pos.y="+pos.y+", vel.x="+vel.x+", vel.y="+vel.yLocal spacefilter:=cpShapeFilterNew(ULong(0), UInt(1), UInt(1) )Local Test:=SelfcpSpaceBBQuery(space, cpBBNew(0,0,640,480), spacefilter, CallBack, Varptr Test)canvas.Translate( Width/2, Height/2 )debugger.DebugDraw( canvas,space )EndFunction TestCallBack(d:Void Ptr)DebugStop()Local data:=Cast<MyClass Ptr>(d)[0]Print data.TestEndFunction CallBack:Void(shape:cpShape, p:Void Ptr)Local whatsthis:=cpShapeGetUserData(shape)Local data:=Cast<MyClass Ptr>(whatsthis)[0]Local app:=Cast<HelloChipmunk Ptr>(p)[0]DebugStop()If dataPrint app.somethingPrint data.TestEndEndMethod Cleanup() 'Yeah, right!cpShapeFree( ballShape )cpBodyFree( ballBody )cpShapeFree( ground )cpSpaceFree( space )EndEndClass MyClassField Test:String = "Something in myclass"EndFunction Main()New AppInstanceNew HelloChipmunkApp.Run()EndJanuary 17, 2017 at 5:22 pm #6586did you try the print immediately after
[/crayon]Monkey123[crayon-5cb9cf78ef298696974261 inline="true" ]Local data:=New MyClassI’d be curious to know if that makes it work like it does here…
January 17, 2017 at 5:30 pm #6588Yeah I tried that but it didn’t make any difference here on Windows 10 – I should try on my Mac as well really. I think the memory pointer doesn’t point to the right place for whatever reason but I guess you need to run it though a proper c debugger to get a better idea of what’s going on, hopefully Mark can do that.
January 17, 2017 at 5:38 pm #6589The crash is happening when the string is being retained, and indeed works fine if you use an int instead – so looks like you might have stumbled on something fundamental…
January 17, 2017 at 9:10 pm #6591This wont work…Monkey123Local data:=New MyClass...shape.UserData = Varptr(data)…because ‘data’ is a local variable and will go out of scope when the ctor returns, meaning there will be nothing keeping the MyClass instance alive.
The main problem here is binding an mx2 object to a ‘native’ cp object. I think a ‘generic’ way to do this would be very useful and will look into it soon.
January 17, 2017 at 9:21 pm #6592I just posted on github and closed that as it makes sense now, here’s updated code, a field that references itself seems a bit funny, but why not? If you have a gameobject, that has a cpShape as field that needs to reference the gameobject when passed to a callback, then it seems like an ok solution to me:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120#Import "<std>"#Import "<mojo>"#Import "<chipmunk>"#Import "chipmunkdebugger"Using std..Using mojo..Using chipmunk..Struct somedataField value:IntEndClass HelloChipmunk Extends WindowField space:cpSpaceField ground:cpShapeField ballBody:cpBodyField ballShape:cpShapeField ballBody2:cpBodyField ballShape2:cpShapeField polyBody:cpBodyField polyShape:cpShapeField gameobject:AGameObjectField something:String = "App String"Field debugger:=New ChipmunkDebuggerMethod New()ClearColor=Color.Black'Create a new space and set its gravity to 100'space=cpSpaceNew()space.Gravity=cpv( 0,1000 )gameobject=New AGameObjectgameobject.CreateShape(space)EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()Const timeStep:=1.0/60.0space.StepTime( timeStep )' Local rot:=ballBody.Rotation' Local pos:=ballBody.Position' Local vel:=ballBody.Velocity' Print "ball rot="+ATan2( rot.y,rot.x )+", pos.x="+pos.x+", pos.y="+pos.y+", vel.x="+vel.x+", vel.y="+vel.yLocal spacefilter:=cpShapeFilterNew(ULong(0), UInt(1), UInt(1) )Local Test:=SelfcpSpaceBBQuery(space, cpBBNew(0,0,640,480), spacefilter, CallBack, Varptr Test)canvas.Translate( Width/2, Height/2 )debugger.DebugDraw( canvas,space )EndFunction CallBack:Void(shape:cpShape, p:Void Ptr)Local data:=Cast<AGameObject Ptr>(shape.UserData)[0]Local app:=Cast<HelloChipmunk Ptr>(p)[0]If dataPrint app.somethingPrint data.TestEndEndMethod Cleanup() 'Yeah, right!cpShapeFree( ballShape )cpBodyFree( ballBody )cpShapeFree( ground )cpSpaceFree( space )EndEndClass AGameObjectField Test:Int = 999Field body:cpBodyField shape:cpShapeField Me:AGameObjectMethod New()Me = SelfEndMethod CreateShape(space:cpSpace)body= cpBodyNewStatic()body.Position = cpv(100,100)space.AddBody(body)shape = cpBoxShapeNew(body, 50, 50, 0.0)shape.Friction = 1shape.UserData = Varptr(Me)space.AddShape(shape)EndEndFunction Main()New AppInstanceNew HelloChipmunkApp.Run()End - 
		AuthorPosts
 
You must be logged in to reply to this topic.