Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Oncollision detection and cut rope problem
#5
(18-11-2021, 12:38 PM)josemendez Wrote: These modifications work perfectly for me, can you share the full script you're using including these changes?

Code:
using System.Collections.Generic;
using UnityEngine;
using Obi;

namespace _Game.Scripts.Entities.Rope
{
    [RequireComponent(typeof(ObiSolver))]
    public class RopeCutter : MonoBehaviour
    {
        ObiSolver solver;
        public List<ObiStructuralElement> ropeElements;
       
        void Awake(){
            solver = GetComponent<Obi.ObiSolver>();
        }

        void OnEnable () {
            solver.OnCollision += Solver_OnCollision;
        }

        void OnDisable(){
            solver.OnCollision -= Solver_OnCollision;
        }
        void Solver_OnCollision(object sender, ObiSolver.ObiCollisionEventArgs e)
        {
            var world = ObiColliderWorld.GetInstance();

            // just iterate over all contacts in the current frame:
            foreach (Oni.Contact contact in e.contacts)
            {
                // if this one is an actual collision:
                if (contact.distance < 0.01);
                {
                    ObiColliderBase col = world.colliderHandles[contact.bodyB].owner;
                    if(col !=null)
                        TearRope(col, contact);
                }
               
            }
        }

        private void TearRope(ObiColliderBase col, Oni.Contact contact)
        {
            if (!col || !col.CompareTag("saw")) return;
// get the particle index:
            int particleIndex = solver.simplices[contact.bodyA];

// retrieve the actor this particle belongs to:
            ObiSolver.ParticleInActor pa = solver.particleToActor[particleIndex];
            var actor = pa.actor as ObiRope;

// not a rope, stop:
            if (actor == null) return;

// check rope elements and tear the one that references this particle:
            foreach (var elm in actor.elements)
            {
                if (elm.particle1 == particleIndex)
                {
                    actor.Tear(elm);
                    actor.RebuildConstraintsFromElements();
                    break;
                }
            }

        }
       
    }
}

What am i doing wrong?
Reply


Messages In This Thread
RE: Oncollision detection and cut rope problem - by greyhawk - 18-11-2021, 01:00 PM