Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Chipmunk Collision Handlers in Ruby
- - By RavensKrag Date 2010-07-23 19:36
I'm trying to use the Ruby bindings for Chipmunk 5 created by Beoran, but I can't seem to find any documentation. As such, I have been using the C documentation and trying to figure out how that translates to Ruby.  However, I can not figure out how the collision handlers work.  In the C version, there is a function called cpSpaceAddCollisionHandler, and the Ruby equivalent is CP::Space#add_collision_handler.  Does anyone know what the argument of the Ruby version are supposed to be?  Collision handlers are different from just defining collision functions, as you can define actions that take place at different times in the solution of the collision, as seen here http://code.google.com/p/chipmunk-physics/wiki/Documentation?tm=6

I am very eager to find a way to get Chipmunk to call a specific method when objects separate, and believe this would be the way to do that.  Any documentation at all regarding the way collisions are handled in the Ruby version of Chipmunk would be very helpful.  Well, with the exception of the Gosu tutorial on Chipmunk integration, as I have already looked at that.  Sorry to say, but it only really seems to scratch the surface of what can be done.
Parent - By RavensKrag Date 2010-07-23 19:53
Ah, so I poked around in the gemspec (at least I think that's what this stuff is called...) and found some information.  Check it out.
http://pastebin.com/6EUm3qYy

So apparently, the code is contained in a separate class called CollisionHandler... though I'm not quite exactly how that works.  I would assume it would be very similar to the way collision handlers work in the C version.

I would still be very happy to have more specific information.
Parent - - By jlnr (dev) Date 2010-07-23 23:17
Hmmm, do you happen to know if the chipmunk gem is way outdated now? Are Beoran's bindings the new way to go? Are these a gem too? :)
Parent - By erisdiscord Date 2010-07-24 00:29
I believe it's outdated—see my response about chipmunk-ffi for more info. :)
Parent - By RavensKrag Date 2010-07-24 05:10 Edited 2010-07-24 05:12
You have to make the gem yourself, to my knowledge, which is a bit annoying.  Someone should really get it up on the net somewhere though, as the ruby bindings of Chipmunk available through 'gem install chipmunk' are rather old.  But Beoran's code is a little odd, as the resultant gem has a version of 2.1.0, which is lower than the one in the repository.  The readme file confirms that the bindings are for Chipmunk 5.2 though.  I assume the version number correlates to the version of the 'gem' as it were.
Parent - - By erisdiscord Date 2010-07-24 00:28
I'd recommend looking at chipmunk-ffi for your Chipmunk needs, although I'm not a neutral party since I've contributed to it. ;) You can call Space#add_collision_handler(a, b, handler) and pass any object that responds to begin, pre, post and/or sep. You don't need to use a particular class and you don't need to implement all the methods.

If you just want to have a particular method called, you could call Space#add_collision_func(a, b, type, &block) and pass, say, &your_object.method(:handle_collision) as the last parameter instead of a block.

Your collision callback will get different arguments depending on its arity. It will either receive a single Arbiter or the two colliding Shape objects. The single argument form is preferred since the arbiter holds the two shapes plus additional collision information. Have a look at chipmunk-ffi/arbiter.rb to see what exactly it offers.

Any further questions? :)
Parent - - By banister Date 2010-07-24 00:57
isn't there a difficulty with chipmunk-ffi that you need to have pre-installed the compiled chipmunk binaries to a directory on the library load path prior to installing chipmunk-ffi? at least that was where it was at last time i looked at it. IMO This seems prohibitively hard for ruby noobs.. Nonetheless I think it's possible to have the chipmunk binary compile on gem installation even with chipmunk-ffi
Parent - By RavensKrag Date 2010-07-24 05:14
Indeed, the concept of chipmunk-ffi baffles me, and I'd rather not install it feeling that I have to install a pre-compiled chipmunk binary as well.  I'd rather have a complete package that I can simply use.
Parent - - By RavensKrag Date 2010-07-24 05:28
Ah I see... well while I was poking through the gemspec it appears as if you can use any class as well with Beoran's version.  I am just a little confused as to why these two developers went that route instead of accepting a method as an argument.  You could easily do this using lambda in ruby.  Regaurdless, how are the methods to be named? begin, presolve, postsolve, separate; or simply begin, pre, post, sep? Beoran's example simply only defined begin, so I'm not quite sure how this works out, though I would assume that Beoran's bindings and the ffi bindings work the same way.  I suppose I'll just have to try it and see to find out for sure though xD

Arbiters exist in Beoran's version as well, I believe.  I would assume both his version and the chipmunk-ffi version both work the same way as the C version.

What is "arity"? How do I know what the "arity" of a collision callback will be?
Parent - - By erisdiscord Date 2010-07-24 06:26
Arity is the number of arguments a method takes—sorry, I took for granted that it is pretty standard terminology. :) You'll know its arity because you declared its argument list yourself, of course.

The collision handlers are probably there just because Chipmunk itself uses a roughly equivalent data structure. If you want to use one of those, the methods should have the short names of begin, pre, post and sep. Only the ones you've defined will be called.

You can pass a block (or lambda, or method, or anything that can be converted to a Proc object) to the other method I mentioned, add_collision_func.
Parent - - By RavensKrag Date 2010-07-25 18:37
I have heard the term arity before, and I believe it is a fairly standard term, but I'm still learning about programming and computer science, so I didn't really understand what it was.  Thank you for enlightening me.

Ah, I see.  I also did not realize that all those things eventually boiled down to Proc objects.  That's pretty cool.
Parent - - By erisdiscord Date 2010-07-25 18:52
Strictly speaking, Method and Proc objects are distinct, but you can convert a method to a proc easily. :) The nice thing is that the method stays bound to the object it belongs to, so you can pass it around and call it just like a proc and self will still be the  original "owner" object.

Good luck with your Ruby learning, and computer science in general. Don't forget to dabble with other languages too!
Parent - By RavensKrag Date 2010-07-26 02:36
Thank you.  I have studied other languages, particularly Java and C.  I've taken a few classes in Java, but the course I took in C was really only enough to familiarize myself with the language.  I haven't had the need to do anything very complex in C.  However, I feel like that could get rather cumbersome rather quickly as C is a rather low-level language.
Parent - - By banister Date 2010-07-27 01:40
Beoran tells me that his chipmunk extension was designed to be API compatible with chipmunk-ffi, he even has a spec that ensures they match. Haven't run the spec though (nor do i know anything about chipmunk-ffi) so i can't confirm this.

Another point though...can chipmunk-ffi work on a windows system?
Parent - By erisdiscord Date 2010-07-27 01:46
I don't have access to Windows so I can't say for sure, but Ruby FFI officially supports Windows, so chipmunk-ffi should work there.
Parent - By RavensKrag Date 2010-07-28 18:16
Well, I suppose this is as good of a spot to add this info as any.

Beoran's extension is slightly different, in at least one way.  When creating a class to pass to CP::Space#add_collision_handler, the methods should be named begin, pre_solve, post_solve, and separate.  This is as opposed to the shortened names used in the ffi version.  Personally, I prefer the former way, as it makes the code more self-documenting.  That seems to be the philosophy behind ruby no? Make the code easy to read, as long as you understand English, even if you don't completely understand the syntax.
Parent - - By Trebor777 Date 2010-07-29 12:40
well Shawn42 tried to make me a chipmunk dll to work with chipmunk-ffi  but it crashes... so nope it doesn't work for the moment... :/ so thank you a lot for that gem
Parent - By shawn42 Date 2010-07-29 16:00
Hrm, this is a good nudge for me to get that chipmunk.dll built correctly then...
Parent - - By banister Date 2010-07-24 01:26
can you explain how you installed beoran's bindings and tell me whether or not they are different to the bindings in the chipmunk gem itself?
Parent - - By RavensKrag Date 2010-07-24 05:09
It's been a while since I did it, but I believe I just downloaded the source from github, ran rake on it, and then did 'sudo gem install' to install the gem.  Beoran's bindings are in the same style as the ones in the chipmunk gem installable from 'sudo gem install chipmunk', but they pertain to the most recent version of Chipmunk, allowing for things such as Chipmunk's version of raycasting, I believe it's called line query or something like that.
Parent - By banister Date 2010-07-24 05:52
well i hold rights to update the chipmunk gem so i'll take a look at it and maybe update the gem if it's not too difficult
Parent - By banister Date 2010-07-24 07:40
ok i've checked it, the current chipmunk gem supports chipmunk version 4.1. Beoran's code updates the bindings to support chipmunk version 5.1 (but, confusingly, he keeps a separate gem version number which is 2.1.0).

I'll either give Beoran privs to update the current gem to 5.1 or i'll update it myself in the next couple of days
Parent - - By banister Date 2010-07-25 04:38
updated the chipmunk gem to 5.2.0, let me know if it works
Parent - - By RunnerPack Date 2010-07-25 06:11
Works great for me under XP.

RUBYGEMS VERSION: 1.3.7
RUBY VERSION: 1.9.1 (2010-01-10 patchlevel 378) [i386-mingw32] (Latest RubyInstaller)

Thanks, banister!

Anybody have some demo code that illustrates more of the API? It seems quite different from the 2D physics OOP code I'm used to (Box2DAS3 is the only thing I have significant experience with).
Parent - - By jlnr (dev) Date 2010-07-25 10:16
This might be outdated then, but maybe the concepts are still similar:

http://www.libgosu.org/cgi-bin/mwf/topic_show.pl?tid=182

(Chipmunk demos translated to Ruby.)
Parent - - By RavensKrag Date 2010-07-25 18:46
I believe I tried that code numerous times before, and it seems as if it is too outdated to run correctly.  I didn't take the time to see if conversion to something runnable was merely trivial or not.

However, if you download Beoran's code for the Chipmunk bindings and look in the spec/ directory, you will find that the gemspec files are quite self documenting and are supported by comments as necessary.  This, combined with the API on the Chipmunk wiki on the google code site (http://code.google.com/p/chipmunk-physics/) should be enough to understand how everything works.  The Chipmunk integration demo for Gosu is also a good starting point. 

It seems Beoran was trying to port all of the demos from the C version to Ruby, but I do not know how long it has been since that code was updated.  It would be nice if a few of us could try contributing some code to that effort.  I may try to edit some stuff personally now that I have a scattered, but comprehensive, API for Chipmunk in Ruby.  I would understand if people are working on other things though, I know I am.
Parent - - By banister Date 2010-07-26 02:41
hi,

since you are the guy most familiar with beoran's chipmunk 5.2 bindings, could you confirm that the new chipmunk gem has successfully been updated to 5.2?

thanks
Parent - - By RavensKrag Date 2010-07-26 02:44
As far as I can tell, yes, the new gem uses Beoran's chipmunk 5.2 bindings.

Thank you so much for updating that gem, now I don't have to distribute it with my game.
Parent - By shawn42 Date 2010-07-27 12:59
I'm a little late to the discussion, but as the author of chipmunk-ffi, I feel I should chime in.  First, it should be 5.2 compliant. Secondly, chipmunk-ffi does require that you have the chipmunk binary installed somewhere (you can specify the path at runtime if not installed in a standard system wide location).  The idea was that scott (author of chipmunk) would eventually publish binaries for all platforms that could just be pulled down. That hasn't really happened yet. Feel free to drop an issue on the chipmunk-ffi github repo or email me at shawn42 _at gmail.
Up Topic Gosu / Gosu Exchange / Chipmunk Collision Handlers in Ruby

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill