Obi Official Forum
Ropes Lagging - Printable Version

+- Obi Official Forum (https://obi.virtualmethodstudio.com/forum)
+-- Forum: Obi Users Category (https://obi.virtualmethodstudio.com/forum/forum-1.html)
+--- Forum: Obi Rope (https://obi.virtualmethodstudio.com/forum/forum-4.html)
+--- Thread: Ropes Lagging (/thread-3523.html)



Ropes Lagging - Gauri7 - 13-07-2022

hello, 
I am using obi ropes , and using Solver_OnParticleCollision to detect there collisions
the games lags when ropes are to close to each other , can you help ?
thanks


RE: Ropes Lagging - josemendez - 13-07-2022

(13-07-2022, 12:51 PM)Gauri7 Wrote: hello, 
I am using obi ropes , and using Solver_OnParticleCollision to detect there collisions
the games lags when ropes are to close to each other , can you help ?
thanks
Hi there!

Profile your game. Specifically, make sure that you're not doing something obviously slow in OnParticleCollision (things like looking for gameobjects in the entire scene for each contact, or similar stuff).

If you could share the code you've written for OnParticleCollision I'd be happy to help with it.

kind regards,


RE: Ropes Lagging - Gauri7 - 13-07-2022

Thanks for the quick reply.

Here's my OnParticleCollisionCode:

Code:
private void Solver_OnParticleCollision (object sender, ObiSolver.ObiCollisionEventArgs e) {
       
        allNonCollidingRopes = new List<Ropes> (ropes);
        ObiSolver.ParticleInActor pa = null;
        ObiSolver.ParticleInActor po = null;

        Ropes r1 = null;
        Ropes r2 = null;

        int particleIndex = 0;
        int particleIndex2  = 0;
     
        foreach (var contact in e.contacts) {

            if (contact.distance < 0.01f) {

                int simplexStart = _solver.simplexCounts.GetSimplexStartAndSize (contact.bodyA, out int simplexSize);

                for (int i = 0; i < simplexSize; ++i) {
                    particleIndex = _solver.simplices[simplexStart + i];

                    pa = _solver.particleToActor[particleIndex];
                    r1 = pa.actor.transform.GetComponent<Ropes> ();
                }

                int simplexStart2 = _solver.simplexCounts.GetSimplexStartAndSize (contact.bodyB, out int simplexSize2);

                for (int i = 0; i < simplexSize2; ++i) {
                    particleIndex2 = _solver.simplices[simplexStart2 + i];

                    po = _solver.particleToActor[particleIndex2];
                    r2 = po.actor.transform.GetComponent<Ropes> ();
                }

                if (r1.attacht1 == r2.attacht1 || r1.attacht1 == r2.attacht2 || r1.attacht2 == r2.attacht1 || r1.attacht2 == r2.attacht2)
                {
                    pa = null;
                    po = null;
                }

                if (pa != null && po != null) {
                    r1.colliding = true;
                    r2.colliding = true;

                    allNonCollidingRopes.Remove (r1);
                    allNonCollidingRopes.Remove (r2);

                    if (r1.partnerRope == null && !r1.hasErrormark && r2.partnerRope != r1)
                    {
                        r1.partnerRope = r2;
                        r1.hasErrormark = true;
                        r1.errorMark.SetActive(true);
                    }

                    if (r1.partnerRope == r2)
                    {
                        r1.errorMarkPos = _solver.positions[particleIndex];
                    }

                    StopCoroutine(ResetRopes());
                }
            }
        }

        if (!resetRopes) {
            resetRopes = true;
            StartCoroutine (ResetRopes ());
        }
    }



RE: Ropes Lagging - josemendez - 13-07-2022

Hi,

Every frame you're creating a new list, and for each contact you're removing Rope components from this list. I don't know how often this happens, but it has the potential to be slow (and cause a lot of GC).

Removing entries from a list has linear cost in the amount of elements in the list: the entire list has to be searched for the element being removed, then all elements past it have to be moved one entry back. So for each pair of particles in contact, you're iterating trough all ropes in the allNonCollidingRopes list multiple times. If you have more than a handful of ropes in the allNonCollidingRopes list, this will be *very* slow.

I'd use Unity's profiler to see what is the bottleneck, and if your collision logic is indeed the culprit, find a more efficient way to implement it.