EndContact not reporting when body is destroyed.

Post about the Flash World Construction Kit and the Alchemy port of Box2D.
dougk16
Posts: 137
Joined: Thu May 27, 2010 6:52 am

EndContact not reporting when body is destroyed.

Postby dougk16 » Tue Jan 04, 2011 4:28 pm

If two bodies are touching each other, resting on top of each other, and I destroy one of them, I don't get an EndContact() call. I think it might have something to do with the call getting stuck in Alchemy-land when b2ContactManager destroys a contact (?). Or I could be doing something wrong.

Thanks for any help.

- Doug

Paril
Posts: 236
Joined: Mon Jul 27, 2009 6:46 am

Re: EndContact not reporting when body is destroyed.

Postby Paril » Tue Jan 04, 2011 7:15 pm

Are you destroying from any contact call? Because the world is locked at that point, and you shouldn't delete bodies.

-P

dougk16
Posts: 137
Joined: Thu May 27, 2010 6:52 am

Re: EndContact not reporting when body is destroyed.

Postby dougk16 » Tue Jan 04, 2011 7:59 pm

No, the destroy is done inside a key press listener. Looking deeper into both the C++ and ActionScript, it looks like the world's b2ContactManager class in C++ land has no way of being assigned a b2ContactListener from ActionScript land, so the call just doesn't get made in the first place, or gets made to a b2ContactListener only in C++ land. Could be way off though too :)

mayobutter
Posts: 915
Joined: Fri Dec 14, 2007 8:07 pm
Contact:

Re: EndContact not reporting when body is destroyed.

Postby mayobutter » Wed Jan 05, 2011 2:41 pm

How are you destroying your body? Are you just removing it from the stage if you're using WCK? is "reportEndContact" definitely true? I'm 99% certain the problem is not something off in alchemy land.

dougk16
Posts: 137
Joined: Thu May 27, 2010 6:52 am

Re: EndContact not reporting when body is destroyed.

Postby dougk16 » Wed Jan 05, 2011 3:19 pm

I use the Alchemy port directly, so in this case I'm just calling b2World::DestroyBody(), which eventually calls lib.b2World_DestroyBody(m_world._ptr, _ptr). In the C++ version of Box2D, this invokes the world's b2ContactManager to make an EndContact() call to its m_contactListener class.

I think everything works fine in Alchemy land, but the call just isn't getting out to my b2ContactListener implementation in ActionScript.

All the report flags are set to true, so normal "EndContacts" get fired, i.e. if I pull the first body off the other body instead of destroying it.

mayobutter
Posts: 915
Joined: Fri Dec 14, 2007 8:07 pm
Contact:

Re: EndContact not reporting when body is destroyed.

Postby mayobutter » Mon Jan 10, 2011 5:10 pm

Holy smokes, you're right! Here's the problem code (in Alchemy):

Code: Select all

AS3_Val b2World_DestroyBody(void* data, AS3_Val args) {
   b2World* w;
   b2Body* b;
   AS3_ArrayValue(args, "PtrType, PtrType", &w, &b);
   AS3_Release((AS3_Val)b->m_userData);
   for(b2Fixture* f = b->m_fixtureList; f; f = f->m_next) {
      AS3_Release((AS3_Val)f->m_userData);
   }
   for(b2JointEdge* j = b->m_jointList; j; j = j->next) {
      AS3_Release((AS3_Val)j->joint->m_userData);
   }
   w->DestroyBody(b);
   return AS3_Null();
}



See how it's calling AS3_Release on all the fixture userData before calling DestroyBody? Well that means once the end contact event is dispatched, the userData (which points to the AS3 instance) is no longer around to dispatch an event from. Working on a fix...

mayobutter
Posts: 915
Joined: Fri Dec 14, 2007 8:07 pm
Contact:

Re: EndContact not reporting when body is destroyed.

Postby mayobutter » Mon Jan 10, 2011 5:24 pm

Ok, the latest commit to github should fix this problem. Turns out I didn't even need to release everything there, because I was already doing it in the destruction listener. Of course, alchemy happily marched along as if nothing was wrong.

Sorry I doubted you! I was thinking that if this bug were true, it would wreak havoc on all my other projects.

dougk16
Posts: 137
Joined: Thu May 27, 2010 6:52 am

Re: EndContact not reporting when body is destroyed.

Postby dougk16 » Mon Jan 10, 2011 10:44 pm

Yea it's actually odd that I haven't ran into this before either. Your fix does the trick for me though.

As usual, you're very quick to address issues. I appreciate it.

Timbolio1
Posts: 5
Joined: Fri Dec 09, 2011 3:10 pm

Re: EndContact not reporting when body is destroyed.

Postby Timbolio1 » Wed Mar 21, 2012 2:53 pm

Hi, I think I've run into the same problem and I'm just wondering if I've got the latest build. I'm downloading the .ZIP from here https://github.com/jesses/wck. Is there a separate, more up to date github page for the alchemy port without the rest of the wck?

I'm trying to resize a box at runtime which involves destroying and recreating the fixture on the body. b2ContactListener EndContact doesn't seem to be being called when I destroy the fixture. I'll carry on looking just to make certain the problem isn't in my code.

Timbolio1
Posts: 5
Joined: Fri Dec 09, 2011 3:10 pm

Re: EndContact not reporting when body is destroyed.

Postby Timbolio1 » Thu Mar 22, 2012 12:32 pm

I solved it by saving all contacts in an associated object and manually removing them when the fixture is destroyed. Not great but it works! I tried changing the fixture without destroying it but box2d didn't seem to only kindof like that. It works but not very well.


Return to “WCK”



Who is online

Users browsing this forum: No registered users and 0 guests