Impact force on object ?

Topics: Developer Forum
Sep 11, 2010 at 10:54 AM
Edited Sep 11, 2010 at 10:55 AM

I have enemy and I need to hit it with ball. I need to calculate the impact force the ball is having on enemy, and if it's low enemy is alive or else dead.

What is the best way to calculate the impact force on object in FP3 ?

Coordinator
Sep 11, 2010 at 11:53 AM

Look at the PostSolve event.  This will allow you to get access to the impulse.

Sep 11, 2010 at 12:20 PM

Do you know how to setup the Post Solve event, I looked all over the site, and I can't find it ...

Coordinator
Sep 11, 2010 at 3:21 PM
Edited Sep 11, 2010 at 3:23 PM

Here is my "WorldContactManager" for Krashlander.  

Note that "ProcessContacts" should be called AFTER the physics engine has finished it's step.

Also note that "CollisionInfo" is my own object that I attach to the Fixture.UserData property. 

Hope this helps.

-Jeff

 

 

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

using FarseerGames.FarseerGame;
using FarseerGames.FarseerGame.SoundSystem;

using FarseerPhysics.Dynamics;
using FarseerPhysics.Collision;
using FarseerPhysics.Dynamics.Contacts;


namespace FarseerGames.KrashlanderWP7.World
{
    public class WorldContactManager
    {
        float _defaultCollisionImpulseThreshold = .16f;

        float _defaultDiverCollisionImpulseThreshold = .06f;

        float _defaultEnemyCollisionImpulseThreshold = .06f;
        
        float _groundCollisionImpulseThreshold = .15f;

        private List<ContactItem> _contactItemList = new List<ContactItem>();

        public bool Enabled { get; set; }
        
        public WorldContactManager()
        {
           Game.Instance.World.ContactManager.PostSolve += PostSolve;
        }

        public void Dispose()
        {
            Game.Instance.World.ContactManager.PostSolve -= PostSolve;
        }

        public void Reset()
        {
            _contactItemList.Clear();
        }
    
        CollisionInfo _collisionInfo;
        protected void PostSolve(Contact contact, ref ContactImpulse impulse)
        {
            if (!Enabled) return;
            if (contact.FixtureA.UserData is CollisionInfo && contact.FixtureB.UserData is CollisionInfo)
            {
                _contactItemList.Add(new ContactItem(contact, impulse));
            }
        }

        //Need to do this outside the "PostSolve" callback.
        public void ProcessContacts()
        {
            for (int i = 0; i < _contactItemList.Count; i++)
            {
                //if (_contactItemList[i].Contact.FixtureA == null || _contactItemList[i].Contact.FixtureB==null) continue;                
                float maxImpulse = 0.0f;
                Manifold manifold;
                _contactItemList[i].Contact.GetManifold(out manifold);

                for (int j = 0; j < manifold.PointCount; ++j)
                {
                    maxImpulse = Math.Max(maxImpulse, _contactItemList[i].ContactImpulse.NormalImpulses[j]);
                }
                HandleCollision((CollisionInfo)_contactItemList[i].Contact.FixtureA.UserData, (CollisionInfo)_contactItemList[i].Contact.FixtureB.UserData, maxImpulse);
            }
            _contactItemList.Clear();
        }

        private void HandleCollision(CollisionInfo collisionInfoA, CollisionInfo collisionInfoB, float maxImpulse)
        {
            //Box -- Box
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Box && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Box)
            {
                HandleBoxBoxCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse);
            }

            //Box -- Snow
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Box && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Snow)
            {
                HandleBoxSnowCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse);
            }

            //Snow -- Box
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Snow && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Box)
            {
                HandleBoxSnowCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse);
            }

            //Box -- Rock
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Box && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Rocks)
            {
                HandleBoxRockCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse);
            }

            //Rock -- Box
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Rocks && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Box)
            {
                HandleBoxRockCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse);
            }

            //Box -- Enemy
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Enemy && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Box)
            {
                HandleEnemyBoxCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoA.HandleCollision);
            }

            //Enemy -- Box
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Box && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Enemy)
            {
                HandleEnemyBoxCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoB.HandleCollision);
            }

            //Enemy -- Diver
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Enemy && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Diver)
            {
                HandleEnemyDiverCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoA.HandleCollision,collisionInfoB.HandleCollision);
            }

            //Diver -- Enemy
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Diver && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Enemy)
            {
                HandleEnemyDiverCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoB.HandleCollision,  collisionInfoA.HandleCollision);
            }

            //Enemy -- Snow
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Enemy && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Snow)
            {
                HandleEnemySnowCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoA.HandleCollision);
            }

            //Snow -- Enemy
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Snow && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Enemy)
            {
                HandleEnemySnowCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoB.HandleCollision);
            }

            //Enemy -- Rock
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Enemy && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Rocks)
            {
                HandleEnemyRockCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoA.HandleCollision);
            }

            //Rock -- Enemy
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Rocks && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Enemy)
            {
                HandleEnemyRockCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoB.HandleCollision);
            }

            //Enemy -- Enemy
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Enemy && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Enemy)
            {
                HandleEnemyEnemyCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId,maxImpulse, collisionInfoA.HandleCollision);
            }

            //Box -- Diver
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Diver && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Box)
            {
                HandleDiverBoxCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoA.HandleCollision);
            }

            //Diver -- Box
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Box && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Diver)
            {
                HandleDiverBoxCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoB.HandleCollision);
            }

            //Diver -- Snow
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Diver && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Snow)
            {
                HandleDiverSnowCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoA.HandleCollision);
            }

            //Snow -- Diver
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Snow && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Diver)
            {
                HandleDiverSnowCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoB.HandleCollision);
            }

            //Diver -- Rock
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Diver && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Rocks)
            {
                HandleDiverRockCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoA.HandleCollision);
            }

            //Rock -- Diver
            if (collisionInfoA.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Rocks && collisionInfoB.CollisionTag.CollisionTagCategory == CollisionEnums.CollisionTagCategory.Diver)
            {
                HandleDiverRockCollision(collisionInfoA.CollisionTag.CollisionTagId, collisionInfoB.CollisionTag.CollisionTagId, maxImpulse, collisionInfoB.HandleCollision);
            }

        }

        //Enemy
        private void HandleEnemyBoxCollision(CollisionEnums.CollisionTagId collisionTagIdA, CollisionEnums.CollisionTagId collisionTagIdB, float maxImpulse, Action<float> handleCollision)
        {
            if (maxImpulse > _defaultEnemyCollisionImpulseThreshold)
            {
                handleCollision(maxImpulse);
            }
            
            if (maxImpulse > _defaultCollisionImpulseThreshold)
            {
                if (collisionTagIdA == CollisionEnums.CollisionTagId.Wood || collisionTagIdB == CollisionEnums.CollisionTagId.Wood)
                {
                    SoundPlayerXNA.Instance.Queue(DiverSounds.Name.Wood);
                }
                else if (collisionTagIdA == CollisionEnums.CollisionTagId.Steel || collisionTagIdB == CollisionEnums.CollisionTagId.Steel)
                {
                    SoundPlayerXNA.Instance.Queue(DiverSounds.Name.Steel);
                }
                else if (collisionTagIdA == CollisionEnums.CollisionTagId.Stone || collisionTagIdB == CollisionEnums.CollisionTagId.Stone)
                {
                    SoundPlayerXNA.Instance.Queue(DiverSounds.Name.Stone);
                }
            }
        }

        private void HandleEnemyDiverCollision(CollisionEnums.CollisionTagId collisionTagIdA, CollisionEnums.CollisionTagId collisionTagIdB, float maxImpulse, Action<float> handleEnemyCollision, Action<float> handleDiverCollision)
        {
            if (maxImpulse > _defaultDiverCollisionImpulseThreshold)
            {                 
                handleDiverCollision(maxImpulse);
            }

            if (maxImpulse > _defaultEnemyCollisionImpulseThreshold)
            {
                handleEnemyCollision(maxImpulse);
            }

            if (maxImpulse > _defaultCollisionImpulseThreshold)
            {
                SoundPlayerXNA.Instance.Queue(DiverSounds.Name.Steel);
                SoundPlayerXNA.Instance.Queue(new int[] { DiverSounds.Name.Grunt1, DiverSounds.Name.Grunt2 });
            }            

        }

        private void HandleEnemySnowCollision(CollisionEnums.CollisionTagId collisionTagIdA, CollisionEnums.CollisionTagId collisionTagIdB, float maxImpulse, Action<float> handleCollision)
        {
            if (maxImpulse < _defaultCollisionImpulseThreshold) return;
            SoundPlayerXNA.Instance.Queue(DiverSounds.Name.Snow);
            handleCollision(maxImpulse);
        }

        private void HandleEnemyRockCollision(CollisionEnums.CollisionTagId collisionTagIdA, CollisionEnums.CollisionTagId collisionTagIdB, float maxImpulse, Action<float> handleCollision)
        {
            if (maxImpulse > _defaultEnemyCollisionImpulseThreshold)
            {
                handleCollision(maxImpulse);
            }

            if (maxImpulse > _defaultCollisionImpulseThreshold)
            {
                SoundPlayerXNA.Instance.Play(new int[] { DiverSounds.Name.Steel, DiverSounds.Name.Steel, DiverSounds.Name.Stone });
            }


        }

        private void HandleEnemyEnemyCollision(CollisionEnums.CollisionTagId collisionTagIdA, CollisionEnums.CollisionTagId collisionTagIdB, float maxImpulse, Action<float> handleCollision)
        {
            if (maxImpulse > _defaultEnemyCollisionImpulseThreshold)
            { 
                handleCollision(maxImpulse);
            }
            if (maxImpulse > _defaultCollisionImpulseThreshold)
            {
                SoundPlayerXNA.Instance.Play(DiverSounds.Name.Steel);
            }
        }

        //Box
        private void HandleBoxBoxCollision(CollisionEnums.CollisionTagId collisionTagIdA, CollisionEnums.CollisionTagId collisionTagIdB, float maxImpulse)
        {
            if (maxImpulse < _defaultCollisionImpulseThreshold) return;
            
            if (collisionTagIdA == CollisionEnums.CollisionTagId.Wood && collisionTagIdB == CollisionEnums.CollisionTagId.Wood)
            {
                SoundPlayerXNA.Instance.Queue(new int[] { DiverSounds.Name.Wood });
            }
            else if (collisionTagIdA == CollisionEnums.CollisionTagId.Steel && collisionTagIdB == CollisionEnums.CollisionTagId.Steel)
            {
                SoundPlayerXNA.Instance.Queue(new int[] { DiverSounds.Name.Steel });
            }
            else if (collisionTagIdA == CollisionEnums.CollisionTagId.Stone && collisionTagIdB == CollisionEnums.CollisionTagId.Stone)
            {
                SoundPlayerXNA.Instance.Queue(new int[] { DiverSounds.Name.Stone });
            }
            else if ((collisionTagIdA == CollisionEnums.CollisionTagId.Wood && collisionTagIdB == CollisionEnums.CollisionTagId.Steel) ||
                (collisionTagIdA == CollisionEnums.CollisionTagId.Steel && collisionTagIdB == CollisionEnums.CollisionTagId.Wood))
            {
                SoundPlayerXNA.Instance.Queue(new int[] { DiverSounds.Name.Wood, DiverSounds.Name.Steel });
            }
            else if ((collisionTagIdA == CollisionEnums.CollisionTagId.Wood && collisionTagIdB == CollisionEnums.CollisionTagId.Steel) ||
                     (collisionTagIdA == CollisionEnums.CollisionTagId.Steel && collisionTagIdB == CollisionEnums.CollisionTagId.Wood))
            {
                SoundPlayerXNA.Instance.Queue(new int[] { DiverSounds.Name.Wood, DiverSounds.Name.Steel });
            }
            else if ((collisionTagIdA == CollisionEnums.CollisionTagId.Wood && collisionTagIdB == CollisionEnums.CollisionTagId.Stone) ||
                    (collisionTagIdA == CollisionEnums.CollisionTagId.Stone && collisionTagIdB == CollisionEnums.CollisionTagId.Wood))
            {
                SoundPlayerXNA.Instance.Queue(new int[] { DiverSounds.Name.Wood, DiverSounds.Name.Stone });
            }
            else if ((collisionTagIdA == CollisionEnums.CollisionTagId.Steel && collisionTagIdB == CollisionEnums.CollisionTagId.Stone) ||
                    (collisionTagIdA == CollisionEnums.CollisionTagId.Stone && collisionTagIdB == CollisionEnums.CollisionTagId.Steel))
            {
                SoundPlayerXNA.Instance.Queue(new int[] { DiverSounds.Name.Steel, DiverSounds.Name.Stone });
            }                
        }

        private void HandleBoxSnowCollision(CollisionEnums.CollisionTagId collisionTagIdA, CollisionEnums.CollisionTagId collisionTagIdB, float maxImpulse)
        {
            if (maxImpulse < _groundCollisionImpulseThreshold) return;
            SoundPlayerXNA.Instance.Queue(DiverSounds.Name.Snow, .5f);
        }

        private void HandleBoxRockCollision(CollisionEnums.CollisionTagId collisionTagIdA, CollisionEnums.CollisionTagId collisionTagIdB, float maxImpulse)
        {
            if (maxImpulse < _groundCollisionImpulseThreshold) return;
            if (collisionTagIdA == CollisionEnums.CollisionTagId.Wood || collisionTagIdB == CollisionEnums.CollisionTagId.Wood)
            {
                SoundPlayerXNA.Instance.Play(new int[] { DiverSounds.Name.Wood });
            }
            else if (collisionTagIdA == CollisionEnums.CollisionTagId.Steel || collisionTagIdB == CollisionEnums.CollisionTagId.Steel)
            {
                SoundPlayerXNA.Instance.Play(new int[] { DiverSounds.Name.Steel });
            }
            else if (collisionTagIdA == CollisionEnums.CollisionTagId.Stone || collisionTagIdB == CollisionEnums.CollisionTagId.Stone)
            {
                SoundPlayerXNA.Instance.Play(new int[] { DiverSounds.Name.Stone });
            }
        }

        private void HandleDiverBoxCollision(CollisionEnums.CollisionTagId collisionTagIdA, CollisionEnums.CollisionTagId collisionTagIdB, float maxImpulse, Action<float> handleCollision)
        {
            if (maxImpulse > _defaultDiverCollisionImpulseThreshold)
            {
                handleCollision(maxImpulse); 
            }
            
            if (maxImpulse > _defaultCollisionImpulseThreshold)
            {

                if (collisionTagIdA == CollisionEnums.CollisionTagId.Wood || collisionTagIdB == CollisionEnums.CollisionTagId.Wood)
                {
                    SoundPlayerXNA.Instance.Queue(DiverSounds.Name.Wood);
                }
                else if (collisionTagIdA == CollisionEnums.CollisionTagId.Steel || collisionTagIdB == CollisionEnums.CollisionTagId.Steel)
                {
                    SoundPlayerXNA.Instance.Queue(DiverSounds.Name.Steel);
                }
                else if (collisionTagIdA == CollisionEnums.CollisionTagId.Stone || collisionTagIdB == CollisionEnums.CollisionTagId.Stone)
                {
                    SoundPlayerXNA.Instance.Queue(DiverSounds.Name.Stone);
                }
                SoundPlayerXNA.Instance.Queue(new int[] { DiverSounds.Name.Grunt1, DiverSounds.Name.Grunt2 });
            }
        }

        private void HandleDiverSnowCollision(CollisionEnums.CollisionTagId collisionTagIdA, CollisionEnums.CollisionTagId collisionTagIdB, float maxImpulse, Action<float> handleCollision)
        {
            if (maxImpulse < _defaultCollisionImpulseThreshold) return;
            SoundPlayerXNA.Instance.Queue(DiverSounds.Name.Snow);
            //handleCollision(maxImpulse);
        }

        private void HandleDiverRockCollision(CollisionEnums.CollisionTagId collisionTagIdA, CollisionEnums.CollisionTagId collisionTagIdB, float maxImpulse, Action<float> handleCollision)
        {
            if (maxImpulse > _defaultDiverCollisionImpulseThreshold)
            {
                handleCollision(maxImpulse);
            }

            if (maxImpulse > _defaultCollisionImpulseThreshold)
            {

                CollisionEnums.CollisionTagId collisionTagId;

                if (collisionTagIdA == CollisionEnums.CollisionTagId.Rock)
                {
                    collisionTagId = collisionTagIdB;
                }
                else
                {
                    collisionTagId = collisionTagIdA;
                }

                switch (collisionTagId)
                {
                    case CollisionEnums.CollisionTagId.Torso:
                    case CollisionEnums.CollisionTagId.UpperArm:
                    case CollisionEnums.CollisionTagId.UpperLeg:
                    case CollisionEnums.CollisionTagId.LowerLeg:
                    case CollisionEnums.CollisionTagId.Head:
                    case CollisionEnums.CollisionTagId.ForeArm:
                        SoundPlayerXNA.Instance.Play(new int[] { DiverSounds.Name.Grunt1, DiverSounds.Name.Grunt2, DiverSounds.Name.BodyImpact1, DiverSounds.Name.BodyImpact2, DiverSounds.Name.BodyImpact1, DiverSounds.Name.BodyImpact2 });
                        break;
                    case CollisionEnums.CollisionTagId.Foot:

                        SoundPlayerXNA.Instance.Queue(DiverSounds.Name.Wood);
                        break;
                    default:
                        SoundPlayerXNA.Instance.Play(new int[] { DiverSounds.Name.BodyImpact1, DiverSounds.Name.BodyImpact2 });
                        break;
                }
            }            
        }
    }

    public class ContactItem
    {
        public Contact Contact {get;set;}
        public ContactImpulse ContactImpulse {get;set;}
        public ContactItem(Contact contact, ContactImpulse contactImpulse)
        {
           Contact = contact;
           ContactImpulse = contactImpulse;
        }
    }
  
}

Sep 16, 2010 at 3:18 PM

I'm dealing with the same problem and I'm wondering if this is the easiest way to do it.

All I need to know is the impact of collision when a specific Fixture collides with another Fixture. Adding an eventhandler to the World's ContactManager makes that handler called for every collision in the world. 
Inside that handler it has to do a check to see if the two fixtures are the ones I need. Seems like some overhead to me?
Is there no way to add an eventhandler to a Fixture which passes the impact of collision, like the OnCollision event?

Sep 17, 2010 at 12:02 AM

Read this discussion that I created recently:

http://farseerphysics.codeplex.com/Thread/View.aspx?ThreadId=227369

 

Hope it helps!

Sep 17, 2010 at 10:40 AM
Edited Sep 17, 2010 at 10:42 AM

A simple use of the postsolve event, (basically is what crashlander does, but checking one fixture, maybe like this seems a little simplier):

 

        private void PostSolve(ContactConstraint contactConstraint)
        {
            float maxImpulse = 0.0f;
            for (int i = 0; i < contactConstraint.Manifold.PointCount; ++i)
            {
                maxImpulse = Math.Max(maxImpulse, contactConstraint.Manifold.Points[i].NormalImpulse);
            }
            if (maxImpulse >= 500)
            {
                // OUCH! that's a huge impact!
            }
        }

 

Sep 20, 2010 at 2:54 PM

Thanks Pnikosis, that's exactly what I was looking for.
Ok, so as I understand it, I can add a handler to a Fixture's postsolve event like this:

[In constructor]
objFixture.PostSolve += this.PostSolve;

[Somewhere in the class]
private void PostSolve(ContactConstraint contactConstraint)
{
           
}

But it isn't called upon collision with anything. I've done the same thing with an OnCollision() method which does work.

Sep 21, 2010 at 9:59 AM

That's strange, it should work the way you are doing it. Check that the OnCollision is returning true, because if it's returning false it will not call the PostSolve event. Also check that you are not ignoring collisions with CollisionCategories.

Sep 21, 2010 at 10:22 AM

I just found out there was indeeed an onCollision method returning false. 

Sorry for that. 

 

Sep 21, 2010 at 4:47 PM

Glad you solved it :)