Box2D - Collisions

BlitzMax Forums/Brucey's Modules/Box2D - Collisions

Rick_72(Posted 2010) [#1]
Hi guys!

I am very enthusiastic about Box2D since this is a really cool physics engine (IMHO). But I don't get the collision detect (i.e. the removal of collided objects) to work properly.

I've tried to expand the ContactListener in test.bmx (using the destruction code provided in the manual in chap. 8) based on the example verticalstack.bmx: I've tried to get boxes deleted that were hit by a bullet. As soon as a contact occurs I get an exception - crash.

Could anyone show me how using the example verticalstack.bmx to do this correctly, plz? I have absolutely no idea.

Thx!


Volker(Posted 2010) [#2]
You can't delete bodies bodies directly when the contact occurs.
"Box2D does not allow you to alter the physics world inside a callback because you might destroy objects that Box2D is currently processing, leading to orphaned references. "
Set a field of your objects to true when a contact occurs and
delete them later in your main loop.


Rick_72(Posted 2010) [#3]
That is exactly what I did. I was aware of waiting for the engine to finish its job before deleting any objects. But anyways it throws an exception and crashes. So if you have a working piece of code I would appreciate you sharing it with us.


Robert Cummings(Posted 2010) [#4]
In the callback, just set a variable for that object, and kill it after an update in your main code.


Volker(Posted 2010) [#5]
Hi Ralph,

my own code is to bloated to post it, so I tried to modify
the verticalstack.bmx and ran into the same problem you have.
Here is a working example; dont' harm me, I don't know what I'm doing.
Especially I have no idea why I have to NULLifiy the userdata (just a string) of the bodies AFTER destroying it. Curious.

Changes made by me are marked with '***********'

verticalstack.bmx


test.bmx



Rick_72(Posted 2010) [#6]
OK, this one works! Thanks Volker for helping on this one and posting some working code!
I had some unresolved reference bug in my code that crashed the whole stuff before collisions even took place.
I should better go to sleep now .. ;-)