Overwriting OnCollision

Jul 20, 2010 at 8:29 AM
Is it possible to overwrite the default OnCollision method to my own method? The default method is like this: private bool OnCollision(Geom geom1, Geom geom2, ContactList contactList) { return true; } I want to change to have my own parameter or arguments, maybe something like this? private bool OnCollision(Body body1, Body body2, Geom geom1, Geom geom2, ContactList contactList) { return true; } Looking for some replies here :D
Jul 20, 2010 at 11:16 AM
You could! but the Geom store a reference to Body already. Geom.Body.
Jul 20, 2010 at 3:40 PM

I did this, it worked well in 2.1.3:

 

            //allow user to cancel collision if desired
            if (GeometryA.OnCollision != null)
            {
                if (_newContactList.Count > 0)
                {
                    CollisionEventHandlerArgs args = new CollisionEventHandlerArgs();
                    GeometryA.OnCollision(GeometryA, GeometryB, _newContactList, args);
                    if (args.Cancel)
                    {
                        _newContactList.Clear();
                        CancelledCollision = true;
                    }
                }
            }

            if (GeometryB.OnCollision != null)
            {
                if (_newContactList.Count > 0)
                {
                    CollisionEventHandlerArgs args = new CollisionEventHandlerArgs();
                    GeometryB.OnCollision(GeometryB, GeometryA, _newContactList, args);
                    if (args.Cancel)
                    {
                        _newContactList.Clear();
                        CancelledCollision = true;
                    }
                }
            }

Basically the reason I did this is that by default, C# only examines the return value of the last callback that was attached to an event, so if I had two callbacks attached to OnCollision, and the first once cancelled the collision but the second one didn't, the engine would still think the collision occurred. So now that I'm passing along my custom args class, anyone can set args.Cancel to true to cancel the event, and any attached callback can see if a previous callback cancelled the event as well.

 

 

I may make the same change to my version of FP3.0, since it suffers from the same design flaw. I'm trying not to change the engine while it's still under development however, as merging my changes in with each new update will really suck :D

Jul 21, 2010 at 2:13 AM
But If you do it like this way, then actually how u "attach" your method to the OnCollision? Because from the documentation, it says that need to do something like this: geom.OnCollision += OnCollision; so what should I change?
Jul 21, 2010 at 4:21 AM

In your initialize or content load, you do exactly what you have there.  For the geoms or geom types you want to attach your on collision method to, you do geom.OnCollision += YourOnCollisionMethod;  If it is all of them, then loop through a geomlist that contains all the geoms in your game.

 

Then you write your OnCollision as an "private bool YourOnCollisionMethod(Fixture Fixture1, Fixture Fixture2, ContactManager contactManager) { insert Ron's code here }"

Jul 21, 2010 at 5:00 AM
Alright, let me try it out first. Thanks alot :D