Check identity of two shapes in a contact listener?

Hey,

I'm new here and to Box2D. Thanks a lot to Erin, your work is great. I'm using the Java port version, so also thanks to those who did that.

The thread title is somewhat stupid, I couldn't think of one that fits into the maximum title length... 🙂

Currently, I'm looking for a way to compare shapes. I've got a contact listener and I want to find out which particular shapes are part of a collision (more concrete: there's a ball and two goals and I want to find out which goal the ball collided with). Trying to do it with shape1 == myShape doesn't work, they're two different objects (although describing the same shape), and shape1.equals(myShape) doesn't work as well. So obviously, the contact listener works with copies.

So, are there any simple means of doing shape comparisons that I've not found so far, or should I use set up different categories (and masks) and compare those?

Thanks, Sascha

Edit: I found a workaround that uses userdata objects as a means of identification. It's enough for this project, but it's surely not the perfect way to do it.
You need to set the user data on the shapes (or the body they're attached to). This way you have something defined you can compare.

Bill
Hehe, it seems you've been writing this at the same time when I was editing my post, since there was no answer when I started. Thanks anyway. 🙂

But is the user data way really the way to go, in general? If one really uses user data to store information, it might get impractical at some point.
ElkMonster wrote:
Hehe, it seems you've been writing this at the same time when I was editing my post, since there was no answer when I started. Thanks anyway. 🙂

But is the user data way really the way to go, in general? If one really uses user data to store information, it might get impractical at some point.


The way that box2d stores user data is pretty common in C++. If you need to store more than one integer what you do is store a pointer to an object (cast to the user data type), and then cast it back when you are ready to use it.

If this is not possible in Java the alternative may be using a lookup table (array or hashtable) that maps numbers to objects and then setting user data's value to an index of that array.
User data can point to whatever data you like. For a shape, I would just store the shape index (according to your data layout). You can get your actor from the associated body's user data. If you prefer less indirection, you can store a pointer to a data structure there.
Ah, finally I understand the actor concept. I had pretty much ignored it before because I thought it was just something to use for game characters... 😀

Now it all makes much more sense. Thanks everybody.

Original thread: https://box2d.org/forum/viewtopic.php?t=944