Verification concept: Farseer collision situations

I have been using Farseer for a long time, working on a physics teaching platform project. There are several questions about the engine in my brain that have remained unanswered for some time, many of which relate to handling collisions with the engine.

Many people have problems understanding the engine, partly because Farseer does not have a corresponding API. It may be a good idea for those who know to confirm the following concepts and KILL any wrong concepts.

Concept 0: Two Steps

The clash in Farseer is divided into two stages:

  • Large-scale collision
  • Exact collision (please correct my use of the term if there is another official name for it ...)

Concept 1: Broad Phase

Farseer, using a wide phase collision, will find potential collisions in the scene. The mentioned broadband collision test is performed using ...

  • Using a square tree, which can be found in the package "Farseer.Collision".
  • In the class "Farseer.Dynamics.World" there is a link to "Farseer.Collision.QuadTreeBroadphase".
  • "Farseer.Collision.QuadTreeBroadphase" has a link to "Farseer.Collision.QuadTree".
  • Next, a bounding box check is also performed.
  • If we ever need to know which pair of appliances could collide, we can 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...

Little question 1

What are some of the common situations that are useful for us to know which pairs of appliances potentially collide?

Concept 2: Significance of Events

Definition of two keywords:

  • Touch: two objects are in contact. The Manifold.Points array is NOT empty .
  • Colliding: two objects touch each other for the first time .

BeforeCollision means that two objects DO NOT collide and DO NOT touch .

OnCollision means that two objects collide , but touching DOES NOT fire this event.

AfterCollision means that two objects collide , but now they only touch .

OnSeparation means that two objects were touching , but now it is not.

Concept 3: Using Events

The extended phase collision test and accurate collision tests are always performed.

An exact collision can be disabled by returning false in the OnCollision event or using the IgnoreCollision method.

I have heard more than once when people say: "Just leave OnCollision empty and return true." What does it mean? Using events means that we would like to apply additional effects (for example, kill the enemy, score, play a sound, etc.) when these events occur.

+4
source share
1 answer

This answer applies to Farseer Physics 3.3.1 (current stable version at time of writing)


Widescreen is just a performance optimization. He quickly determines what can collide, so that things that cannot collide do not have a more expensive narrow-phase collision with detection.


In my experience, events at Farseer are not designed as well as they could have been. I found that I had to change them for my own game, but, fortunately, this code is actually not so difficult to read and modify. In addition, the documentation is inaccurate in several places.

Here, events in the physics of Farser are actually made:

BeforeCollision occurs when wide-phase registers a new potential collision before it creates a Contact object for two devices. You can block Contact creation by returning false to the event handler.

The existence of a Contact does not necessarily mean that two luminaires are in contact - simply that the wide phase has succeeded. A Contact can touch / not touch, and they can be turned on / off (they start with enabled and not touching).

OnCollision occurs when a Contact changes from not touching to touching. You can disable the contact by returning false. Oddly enough, you can re-enable the contact, which at this stage turned off another event handler if you return true (this is one of the things that I changed for my own game).

Only activated, touching contact has any effect on physical modeling.

OnSeparation is the opposite of OnCollision : it fires when a contact changes from touch to not touch. There is no way to disable / enable a contact.

AfterCollision is called for every Contact that participates in physical modeling on this frame (like this: those that are on are touched, not on the back island). It is called after the physics simulation stage, but before cleaning - especially before Body.Force and Body.Torque is cleared (convenient for sound effects, etc.).


Usually you simply use these events to trigger your effects - particles, sounds, game mechanics, such as destructive objects, etc. In this case, you subscribe to what you want, do what you like, and then return true (without disconnecting / not blocking the contact).

This gets a lot more complicated if you want to mess with the physics engine or disconnect contacts (especially when you can accidentally turn them on with another event handler, as I mentioned earlier). If Farseer inline events do not work for you, then it’s wise to go into his code and add your own. At this advanced level, it’s practically necessary to build from the source so that you can accurately examine what is happening.

+3
source

Source: https://habr.com/ru/post/1446164/


All Articles