I have been using Farseer for quite a while now, working on a physics teaching platform project. There are a couple of questions about the engine in my brain that are left unanswered for quite some time, many of which are about collision handling of the engine.
Many people seem to have problems understanding the working of the engine, partly because Farseer lacks a proper API. It might be a good idea for those in the know to confirm the following concepts and KILL any concepts that are wrong.
Concept 0: The Two Stages
Collision in Farseer is divided into two stages:
- Broad phase collision
- Precise collision (please correct my use of term, if there's another more official name for it...)
Concept 1: Broadphase
Farseer, using broad phase collision, will find potential collisions in a scene. Said broad phase collision test is done by...
The use a quad tree, which can be found in package "Farseer.Collision".
The class "Farseer.Dynamics.World" has a reference to "Farseer.Collision.QuadTreeBroadphase".
"Farseer.Collision.QuadTreeBroadphase" has a reference to "Farseer.Collision.QuadTree".
Further bounding box test is also done.
If we ever find a need to know which pair(s) of fixtures has the potential to collide, we may do this...
/* Game.Initialize */ public override void Initialize() { OtherIrrelevantInitCode(); _world.OnBroadphaseCollision += BroadphaseHandler; base.Initialize(); } public void BroadphaseHandler(ref FixtureProxy fp1, ref FixtureProxy fp2) { // code to read about fp1 and fp2 }
But then...
Small question 1
What are some of the common situations that it is useful for us to know which pairs of fixtures are potentially colliding?
Concept 2: Meaning of Events
Definition of two keywords:
- Touching: two objects are in contact. The array Manifold.Points is NOT empty.
- Colliding: two objects touch each other for the first time.
BeforeCollision means two objects are NOT colliding and NOT touching.
OnCollision means two objects are colliding, but touching will NOT fire this event.
AfterCollision means two objects were colliding, but now they are touching only.
OnSeparation means two objects were touching, but now they are not.
Concept 3: Using the Events
Broadphase collision test and precise collision tests are always carried out.
Precise collision may be disabled by returning false in event OnCollision or by using the IgnoreCollision method.
I have heard for more than one time that people say "just leave OnCollision empty and return true". What on earth does that mean? Using the events means that we would like to apply additional effects (e.g. kill an enemy, score, play a sound etc.) when the above events happen.