Ruby Extension questions thread

Official forum for the Chipmunk2D Physics Library.
Post Reply
mr_luc
Posts: 4
Joined: Fri Oct 19, 2007 12:47 pm
Contact:

Ruby Extension questions thread

Post by mr_luc »

So! The ShatteredRuby dns issues were apparently temporary, and now that I have the ruby chipmunk extension, I have a whole host of questions!

You can reply here, or at the ShatteredRuby list (linked below) -- if you want to answer here, we could keep clutter off the list, but fortunately or unfortunately this little forum is more active than the ShatteredRuby group.

My first question was,
Given an instance of Space, how can I get a list of all of the shapes/
bodies I've added to it? ... in the demo code, the drawing loop accesses the space->activeShapes hash.
...
But with chipmunk.so, I can't find analogues for any of those
http://groups.google.com/group/shattere ... 91e4ec4845

Of course, since I added them myself, I could always keep track of them at the time when I add them to my Space ... and risk my model getting out of sync with Chipmunk's, but that's doable.

My second question, though, has me kind of gnashing my teeth here.
With chipmunk.so, how do I loop through the vertices of a poly shape?
...
In C, shape->verts appears to be an array of vectors, which makes
sense.

In Ruby, poly.verts is a method that apparently expects the same
arguments as the Poly.new, which doesn't make sense. I initially
assumed that from the plural name and similarity to the C property
that it would let me get at the vertices. Instead, it says that I need
to give it 3 args: a Body, an array, and a vector.

In the C demos, looping through poly->verts is the method used to draw
the poly shapes.
In Ruby, I don't know how to do this.
http://groups.google.com/group/shattere ... 91e4ec4845

THIS one is confusing.

All I can draw right now are the bounding boxes. It's almost as if all of the properties in the extension objects that are lists/arrays/hashes in C, like space.activeShapes, space.bodies, or poly->verts, are missing or inaccessible. Bounding boxes work, and if I had to guess I'd say that's because they have discrete L, R, T, B properties instead of a list of vectors.

And I *do* have to guess, because I am mightily confused!

If anyone can shed some light or throw me a life preserver, I'd appreciate it!

edit: and just editing to say, I am NOT experienced with Ruby extensions, so it is absolutely possible that I'm missing something glaringly obvious to people in the know. ;)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Ruby Extension questions thread

Post by slembcke »

I guess this comes down to a lack of documentation on what is and isn't supposed to be opaque in Chipmunk. (I know, bad bad bad) I guess a good rule of thumb is what I've put in the public documentation. If it's not there, then I've considered it unimportant to the end user or an implementation detail (that may change in the future and break things).

To the first question:
I guess I've never really considered a cpSpace to be a container. All my Ruby code is fairly MVC-esque so I end up storing my own list externally anyway. I actually consider what I did with the demo to be a bit of a hack, the simplest possible way to get a Chipmunk space on the screen.

To the second question:
I don't believe I've ever written a CP::Shape::Poly#verts method. Did the ShatteredRuby/Chipmunk folks add one? I actually don't have accessors for attributes on any shape type. Again, in my Ruby code I always have separate model and view objects anyway. Shapes as well weren't really meant to be transparent, simply an internal representation for Chipmunk to use.

The thing is that doing what I do in the demo app just isn't scalable beyond drawing simple outlines of all the objects in the space. I wouldn't really recommend it for any sizable project.

I threw together a simple set of subclasses that should do what you want. Adding that to the actual extension would be a lot more tedious. Is this an OK compromise? I guess I could add it to the extension if people really, really want that.
http://files.slembcke.net/temp/chipmunk_proto.rb
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
mr_luc
Posts: 4
Joined: Fri Oct 19, 2007 12:47 pm
Contact:

Re: Ruby Extension questions thread

Post by mr_luc »

Aha! All is clear now. :D

Except for poly.verts, of course. There is definitely a verts method defined for any CP::Shape::Poly; last on the list of poly.methods.sort.

I'm not thinking in terms of large projects, heh; I'm just feeling my way around. But it might be useful to have those methods even in large projects, for debugging. In most game engines, you can toggle the rendering of things like the collision bounding boxes/shapes/convex hulls, for debugging purposes.

Thanks a lot for those extensions -- I'd already added a couple of similar properties to the Space object (my own active_shapes, etc) that basically just hangs onto the data we pass to chipmunk, but after seeing the poly.verts method, I thought I might have been way off. As with a lot of this stuff, when you're learning a new thing, you don't know how much you're missing because you're going about it the wrong way, and how much you're missing because you're just looking in the wrong place.

Thanks a ton for this (and, of course, Chipmunk) I will try this out when I get home from work.
mr_luc
Posts: 4
Joined: Fri Oct 19, 2007 12:47 pm
Contact:

Re: Ruby Extension questions thread

Post by mr_luc »

Got time for another couple of hours with Chipmunk today. Heh, I haven't had much time for hacking recently; I work on the kill floor at a large pork processing plant that was recently raided by ICE, so there's lots of overtime. For others like me who feel their way around chipmunk in ruby every so often:

- in the extension, body.v.y += num doesn't work the same way that body->v.y += number works in C, but body.v += a vector works just fine. In case you were confused when doing this.

Some things I have questions about are the collision pair functions. The collision_type for my shapes is an integer, which by default seems to be 4 for both active and static shapes. I add a shape and set its type to 5. Then, I try to define a collision function with this code: (heh, I'm just feeling my way along here, be kind!)

chipgl.space.add_collision_func(type1, type2) { p "hello" }

where type1 and type2 are set from the collision_type properties of the 2 shapes added to the space, one active and one static. I can clearly see the shapes interacting; I've sort of ported the demo code to ruby (well, actually, I just realized that I haven't even written line segment drawing, just polys and circles, but it's enough to know it's working and see what's happening in the spaces I've mucked up so far).

I could be way off, because I am pretty new to Ruby extensions.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Ruby Extension questions thread

Post by slembcke »

Well the first issue is due to the body.v method returning a new vector. It really should be returning a vector object that points to the original vector in the body struct. For the record, I'm actually missing quite a bunch of stuff in the extension. Marshalling methods, hash, equals, etc.

For the second issue with collision pair functions, are you using the latest Chipmunk? In the Ruby extension, I use the object ID as the collision type internally. That way you can use any object (class objects are usually pretty handy here). Can't really remember when I changed it, but getting the collision type used to return the object id, and not the object. The default collision type was nil, which has an object id of 4. That might be were some of your confusion comes from.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests