Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Make damaged rope
#8
(22-11-2021, 04:05 PM)josemendez Wrote: This can only happen if the constraints are still in a dirty state (ie, the amount of elements in the rope and constraints the solver don't match.)
Are you rebuilding the constraints after cutting the rope? Can you share your code for tearing the rope?

Here my code:

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

namespace _Game.Scripts.Entities.Rope
{
    [RequireComponent(typeof(ObiSolver))]
    public class RopeCutter : MonoBehaviour
    {
        private ObiSolver solver;
        private ObiRope currentRope;
        public GameObject damagezone;

        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:

                ObiColliderBase col = world.colliderHandles[contact.bodyB].owner;
                if (col != null)
                {
                    if (col.CompareTag("saw"))
                    {
                        TearRope(col, contact);
                    }
                    else if (col.CompareTag("nearMiss"))
                    {
                        DamageRope(col, contact);
                    }

                }
            }
        }

        private IEnumerator ReservationRope(ObiRope actor)
        {
            currentRope = actor;
            yield return new WaitForSeconds(1f);
            currentRope = null;
        }
       
        private void DamageRope(ObiColliderBase col, Oni.Contact contact)
        {
// 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;
            float i = -1;
// check rope elements and tear the one that references this particle:
            foreach (var elm in actor.elements)
            {
                i++;
                if (elm.particle1 == particleIndex)
                {
                    if (actor!=currentRope)
                    {
                        var obj = Instantiate(damagezone);
                        obj.GetComponent<ObiRopeAttach>().m = i / actor.elements.Count;
                        obj.GetComponent<ObiRopeAttach>().generator = actor.GetComponent<ObiPathSmoother>();
                        obj.GetComponent<ObiRopeAttach>()._continue = true;
                        //actor.Tear(elm);
                        StartCoroutine(ReservationRope(actor));

                    }
                    break;
                }
            }

        }
       
        private void TearRope(ObiColliderBase col, Oni.Contact contact)
        {
// 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.GetComponent<RopeController>().OnRopeTear();
                        actor.RebuildConstraintsFromElements();

                        break;
                }
            }

        }
       
    }
}
Reply


Messages In This Thread
Make damaged rope - by greyhawk - 22-11-2021, 11:24 AM
RE: Make damaged rope - by josemendez - 22-11-2021, 11:33 AM
RE: Make damaged rope - by greyhawk - 22-11-2021, 01:05 PM
RE: Make damaged rope - by josemendez - 22-11-2021, 01:15 PM
RE: Make damaged rope - by greyhawk - 22-11-2021, 01:56 PM
RE: Make damaged rope - by greyhawk - 22-11-2021, 03:40 PM
RE: Make damaged rope - by josemendez - 22-11-2021, 04:05 PM
RE: Make damaged rope - by greyhawk - 22-11-2021, 04:22 PM
RE: Make damaged rope - by greyhawk - 23-11-2021, 11:44 AM
RE: Make damaged rope - by josemendez - 23-11-2021, 12:01 PM
RE: Make damaged rope - by josemendez - 23-11-2021, 01:05 PM
RE: Make damaged rope - by greyhawk - 26-11-2021, 08:51 AM