Callbacks or other notifications?

Oct 4, 2007 at 6:22 AM
Congrats on the new update -- lookin' forward to working with the more mature version.

I did my best to thoroughly sift through the demos, but couldn't seem to find a way of creating callback methods or anything of that sort? I'm trying to figure out how you would find out if two bodies are touching (player touching a powerup (or enemy, or anything)) so the game is able to react.

Thanks.
Coordinator
Oct 4, 2007 at 10:14 AM
Yes, sorry I do not have that in the samples. I actually tested it but must not have had time to build a sample around it. I dug through my testing code and found some snippets.

Geometries have a "CollisionHandler" delegate hanging off them. So to to the callback you just create a callback method and hook a geometry to it.

In the code below, i'm testing both the CollisionHandler and the "Tag" property. The Tag property can be any type of object. I just use a string here as a sort of "Name" for the geometry to identify it.

You could hook many geometries up to this CollisionHandler callback, then do something special when one of the geometries is of a specific type...

geomtry2.Tag = "Test"
geometry2.CollisionHandler += HandleCollision;

        private bool HandleCollision(Geometry g1, Geometry g2, ContactList contactList) {
            if (g2.Tag != null) {
                if (g2.Tag.ToString() == "Test") {
                    return false;
                }
                else {
                    return true;
                }
 
            }
            else {
                return true;
            }
        }
Mar 13, 2008 at 12:49 PM
Edited Mar 13, 2008 at 11:29 PM
Thank you, it is working perfect! But what is the best way to find out if there is no collision? Because the callback methods only get called if there is a collision..=)

Thanks.

Edit: Ok, I solved the problem..
Mar 24, 2008 at 6:27 PM
Can I ask how you solved your issue?
Mar 25, 2008 at 2:39 AM
Hi, yes of course!

I have made some player states for the collision, for example:

public enum PlayerState
{
Collision,
NoCollision
}

Now in the Update(..) method from your player class you can set the state back, so in the last code line you can write:
playerState = PlayerState.NoCollision

And in the HandleCollision Callback method you can change the state to Collision...
Mar 30, 2008 at 4:47 PM
Hi Farseer and thanks for the beautiful and useful library. I was trying to get this callback stuff working in my game with 1.0.0.5 on x86 platform and it seems that the CollisionHandler attribute of an individual geom has gone away. Can you please tell me the new way of doing this? If the HandleCollision method is still relevant I'm wondering also what the return value on that function means. Does returning false mean the collision should be ignored?

Thanks Again
Mar 30, 2008 at 4:59 PM
Ah, after a bit of tinkering I see that the Geom attribute CollisionHandler has been renamed to Collision. Also, my suspicion was correct that returning false from the collision handler delegate will indeed result in the collision being ignored by the engine. Useful and elegant.

For recap the new code as of 1.0.0.5 reads:

geomtry2.Tag = "Test"
geometry2.Collision += HandleCollision;

and the rest as normal.