Obi Official Forum
Tearing rope on Collision - 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: Tearing rope on Collision (/thread-2532.html)



Tearing rope on Collision - Spectra_7 - 13-10-2020

I am trying to tear the rope using OnCollision callback. The rope does get cut, but it also throws exception error when tearing.

Code:
void Solver_OnCollision(object sender, ObiSolver.ObiCollisionEventArgs e)
    {
        for (int i = 0; i < e.contacts.Count; ++i)
        {
            if (e.contacts.Data[i].distance < 0.01f)
            {
                //ObiColliderBase collider = ObiColliderWorld.GetInstance().colliderHandles[e.contacts[i].other].owner;
                Component collider;
                if (ObiCollider.idToCollider.TryGetValue(e.contacts.Data[i].other, out collider))
                {                   
                    if (collider == scissorCollider)
                    {
                        ObiSolver.ParticleInActor pa = solver.particleToActor[e.contacts.Data[i].particle];

                        if (pa != null)
                        {
                            ropeObject = pa.actor.GetComponent<ObiRope>();
                            ropeElements = ropeObject.elements;
                        }

                        //print(e.contacts.Data[i].particle);

                        for (int j = 0; j < ropeElements.Count; j++)
                        {
                            if( ropeElements[j].particle1 == e.contacts.Data[i].particle )
                            {
                                //print("Contact Particle: "+e.contacts.Data[i].particle);
                                //print("Element Particle: "+ropeObject.elements[j].particle1);

                                int particleToTear = ropeObject.elements[j].particle1;

                                ropeObject.Tear(ropeElements[particleToTear]);
                                ropeObject.RebuildConstraintsFromElements();                               
                            }
                        }                       
                    }
                }
            }
        }
    }

It throws index OutOfRange exception.
Code:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <599589bf4ce248909b8a14cbe4a2034e>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <599589bf4ce248909b8a14cbe4a2034e>:0)
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <599589bf4ce248909b8a14cbe4a2034e>:0)
ScissorCut.Solver_OnCollision (System.Object sender, Obi.ObiSolver+ObiCollisionEventArgs e) (at Assets/Scripts/Cut Logic/ScissorCut.cs:67)
Obi.ObiSolver.EndStep () (at Assets/_IMPORTED/Obi/Scripts/Common/Solver/ObiSolver.cs:1302)
Obi.ObiUpdater.EndStep () (at Assets/_IMPORTED/Obi/Scripts/Common/Updaters/ObiUpdater.cs:104)
Obi.ObiFixedUpdater.FixedUpdate () (at Assets/_IMPORTED/Obi/Scripts/Common/Updaters/ObiFixedUpdater.cs:69)

I can't figure out, please help.


RE: Tearing rope on Collision - josemendez - 13-10-2020

Using a particle index to look into an elements array makes no sense whatsoever. There's usually more particles than elements in a rope, hence the out of range exception. Instead of:

Code:
ropeObject.Tear(ropeElements[particleToTear]);

do

Code:
ropeObject.Tear(ropeElements[j]);



RE: Tearing rope on Collision - Spectra_7 - 13-10-2020

(13-10-2020, 11:43 AM)josemendez Wrote: Using a particle index to look into an elements array makes no sense whatsoever. There's usually more particles than elements in a rope, hence the out of range exception. Instead of:

Code:
ropeObject.Tear(ropeElements[particleToTear]);

do

Code:
ropeObject.Tear(ropeElements[j]);

Silly me! Thank you very much. That worked.


RE: Tearing rope on Collision - josemendez - 13-10-2020

No worries! Sonrisa