Contact Listeners in new versions & old code translation

Hi,

I've come across a problem related to Contact Listeners. I'm porting Box2D code from C++ to Flash (for a cross-platform development tool); the problem is that the cold is several years old, based on a rather old Box2D version - It didn't even have the "Fixture" classes. It took me a while to figure out how to translate some of that code, and up until now I've always succeeded - however I have no idea what to do with Contact Listeners.

These are the C++ functions that I need to port (part of b2ContactListener):
ContactStart, ContactStop, ContactPointAdd, ContactPointPersist, ContactPointRemove, ContactPointResponse

And in Flash, the most similar functions that I was able to find are:
BeginContact, EndContact, PreSolve, PostSolve

I don't think it matters that the code is C++ because of the almost identical design in the Flash version - so sorry if this post doesn't belong in the Flash subforum.
I would be happy if anybody had an idea of what I need to do - I figured that since this change should affect anyone who's worked with older versions before, there should be some kind of conversion guide, but I was unable to find anything like that.

Thanks for any answer.
I'm assuming the Box2D C++ version you're porting from is 1.4.3 or earlier, since the ContactStart, ContactStop, etc. functions are not in 2.0.x (only Start, Stop, etc). But for the life of me, I can't find any code or documentation for 1.4.3, so I can't find out how Box2D handled contact listeners back then.

Since I don't know how the code even looks like in the old Box2D versions pre 2.0, I don't know what the problem could be. What are you stuck with exactly? Is the b2ContactListener a struct or what?

Just by guessing, I would just follow whatever the Flash port did (2.0.x or 2.1a) to port the b2ContactListener and how it's handled.
Your assumption is correct. I didn't know how to check the version but the files were last modified in 2008 - that should give an idea. 😉

The problem I have is, Box2D was used in a form of a plug-in. A kind of binding to the C++ library for said development tool. It supports a major part of the features, which includes contact listeners.
The plug-in for this development tool was designed to resemble the core Box2D library, so there are functions like "OnContactStart", just like ContactStart in C++.
Now I need to implement these in Flash. I somehow need to figure out how I can use the 4 available members of b2ContactListener in Flash to simulate the old behaviour.
I'm unsure what you mean by "follow whatever the Flash port did" - the Flash port of the actual library didn't need any of this translation since it didn't need to be backwards compatible.
Thanks for taking your time 🙂

EDIT: Sorry if it was unclear, the development tool has several runtimes such as Windows and Flash. So to make games that use Box2D work in Flash, I'm porting the plug-in.
Ah, I see. I'm still not sure what the problem actually is. x]

So, is the problem the lack of the contact listener functions (or contact listener in general), and you're trying to add that functionality in your Flash port?
Thanks again. The problem is:

The old Box2D version had different functions in b2ContactListener. The C++ code that uses that old version needs to be ported to Flash and work identically.
However, in Flash, I have a new Box2D version with a different set of functions in b2ContactListener. I basically need the old functions' behaviour in the new library.

Basically this is how my plug-in would work (pseudo code):

class b2ContactListener { ...

function ContactStart(Parameters)
{
GameEngine.trigger('ContactStart',Parameters); //This calls the game engine's custom scripting language
}

}

But in Flash's Box2D, there's no ContactStart as such 🙂 There's BeginContact, for instance. Judging from the name, I would say it has a similar behaviour to that of ContactStart - the problem is, I need to figure out how to be able to call ContactStart in the game engine once BeginContact is triggered.
I know this sounds confusing 🙁 It's just that I'm porting a Box2D binding that relied on an ancient Box2D version... I can't help it.
Ah, I completely see now haha. x] I had no idea that you were trying to get some of the old C++ Box2D functions in the new Flash Box2D. x]

I still can't find the version in C++ that has the ContactStart/ContactStop/etc function names. If you added those in, then call me silly. x]

In either case, what you can do is to have a sort of conversion function or class so that it converts a b2ContactPoint to whatever class type or parameters you need to call the ContactStart function and such.

I'm sorry if I didn't fully understand your problem still.
No, I think you're on the right track 🙂 I didn't add those functions in - it makes sense that you can't find it because it's super old I'm just having a really hard time figuring out how to convert them - I posted this here in the first place because I hoped somebody would have been in a similar situation before (updating his engine to the new Box2D without changing his own library structure).

It's crazy, I know. I have no idea where this old Box2D version with ContactStart has gone. A google search of "Box2D ContactStart" yields like 4 results, including this thread. But there ARE things like these that make me believe Box2D used to have this function - but then it got removed and the Internet completely forgot about it. 😕

EDIT: I actually found this thread now where somebody suggests to update the contact listener system - it ends with Erin Catto updating it so I'll definitely read this thread once I have time. Thanks for your interest 🙂
That thread you found looks promising. Sorry I couldn't be of more help. I hope you find your answers!
OK, I should have read the documentation more carefully 😳

I'll provide a short solution in case anyone ever stumbles upon this thread again which is unlikely:

ContactStart becomes BeginContact, instead of returning the bool, pass it to contact.SetSensor()
ContactStop becomes EndContact

ContactPointAdd, ContactPointRemove, ContactPointPersist etc. become PreSolve().
In the function, compare the two Vectors contact.GetManifold().m_points and oldManifold.m_points to see if a contact point was added, etc.
EDIT: I had a look at the old Box2D source code, and you can find new points by comparing pointA.m_id.key == pointB.m_id.key (pointA,B being b2ManifoldPoints)

ContactPointResponse becomes PostSolve, haven't looked into that much yet.

Again, thanks for your help though, jayther. 🙂

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