Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting actors from bodyA and bodyB in Obi 6.0?
#3
Ah, I think it's because I'm disabling and enabling softbody components. I have expensive softbodies in my scene that slow the game down even when not in contact with any other softbodies, so I disable their softbody component until another one is near, enable it, then disable it again when nothing is in range again. It seems that the contacts report fine the first time, but after disabling the softbody and reenabling it, contacts are now null.

Repro:

1. Add my script to any game object in the Ball Pool Scene and inject the solver parameter.
2. Run Ball Pool Scene.
3. Drop 2 balls in the scene in contact, output window will say something like "Num contacts: Total = 78 Close: 17 Both Valid: 17". All good!
4 Disable and enable an Obi Softbody component on one of the balls in the editor by unchecking it and checking it again.
5. Output will now look like: "Num contacts: Total = 25 Close: 8 Both Valid: 0". "0" at the end with non-zero close contacts means both actors are null incorrectly.

My method of enabling/disabling softbody components worked in Obi 5.x but maybe I shouldn't be doing this and there's a better way now? Having softbodies active when not in contact and static does seem to have a big performance cost so I do need some solution for it.

As always, thoughts and suggestions most welcome and I'll happily help any way I can!

Code:
using UnityEngine;

public class TestParticleCollisions : MonoBehaviour
{
    public Obi.ObiSolver m_solver;

    void OnEnable()
    {
        m_solver.OnParticleCollision += OnSoftParticleCollision;
    }

    void OnDisable()
    {
        m_solver.OnParticleCollision -= OnSoftParticleCollision;
    }
    
    public void OnSoftParticleCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
    {
        if(e.contacts.Count>0)
        {
            int numCloseContacts = 0;
            int bothActorsNotNull = 0;

            foreach(var contact in e.contacts)
            {
                if(contact.distance<0.01f)
                {
                    var otherActor = m_solver.particleToActor[contact.bodyB];
            var thisActor = m_solver.particleToActor[contact.bodyA];

                    numCloseContacts++;

                    if( otherActor != null && thisActor != null )
                    {
                        bothActorsNotNull ++;
                    }
                }
            }

            string results = "Num contacts: Total = " + e.contacts.Count;
            results += " Close: " + numCloseContacts;
            results += " Both Valid: " + bothActorsNotNull;

            Debug.Log( results );
        }
    }
}


.cs   TestParticleCollisions.cs (Size: 1.29 KB / Downloads: 5)

Reply


Messages In This Thread
RE: Getting actors from bodyA and bodyB in Obi 6.0? - by MattS - 13-02-2021, 06:43 PM