/// <summary>
    /// Creates a straight rope anchored to a transform at the top.
    /// Transform may or may not move around and may or may not have a rigidbody.
    /// When you call this the rope will appear in the scene and immediately interact with gravity and objects with ObiColliders.
    /// Called from anywhere (main thread only)
    /// </summary>
    public IEnumerator MakeRope(Transform anchoredTo, Vector3 attachmentOffset, float ropeLength)
    {
        // create a new GameObject with the required components: a solver, a rope, and a curve.
        // we also throw a cursor in to be able to change its length.
        GameObject ropeObject = new GameObject("rope", typeof(ObiSolver),
                                               typeof(ObiRope),
                                               typeof(ObiCatmullRomCurve),
                                               typeof(ObiRopeCursor));

        // get references to all components:
        rope   = ropeObject.GetComponent <ObiRope>();
        cursor = ropeObject.GetComponent <ObiRopeCursor>();
        solver = ropeObject.GetComponent <ObiSolver>();
        ObiCatmullRomCurve path = ropeObject.GetComponent <ObiCatmullRomCurve>();

        // set up component references (see ObiRopeHelper.cs)
        rope.Solver   = solver;
        rope.ropePath = path;
        rope.section  = Resources.Load <ObiRopeSection>("DefaultRopeSection");

        // set path control points (duplicate end points, to set curvature as required by CatmullRom splines):
        path.controlPoints.Clear();
        path.controlPoints.Add(Vector3.zero);
        path.controlPoints.Add(Vector3.zero);
        path.controlPoints.Add(Vector3.down * ropeLength);
        path.controlPoints.Add(Vector3.down * ropeLength);

        rope.pooledParticles = 2000;

        // parent the rope to the anchor transform:
        rope.transform.SetParent(anchoredTo, false);
        rope.transform.localPosition = attachmentOffset;

        // generate particles/constraints and add them to the solver (see ObiRopeHelper.cs)
        yield return(rope.StartCoroutine(rope.GeneratePhysicRepresentationForMesh()));

        rope.AddToSolver(null);

        // get the last particle in the rope at its rest state.
        pinnedParticle = rope.UsedParticles - 1;

        // add a tethers batch:
        ObiTetherConstraintBatch tetherBatch = new ObiTetherConstraintBatch(true, false, 0, 1);

        rope.TetherConstraints.AddBatch(tetherBatch);
        //UpdateTethers();

        // fix first particle in place (see http://obi.virtualmethodstudio.com/tutorials/scriptingparticles.html)
        rope.invMasses[0] = 0;
        Oni.SetParticleInverseMasses(solver.OniSolver, new float[] { 0 }, 1, rope.particleIndices[0]);
    }
Exemplo n.º 2
0
    public IEnumerator MakeRope()
    {
        print("Load Extraction Rope");
        ropeObject = new GameObject("rope",
                                    typeof(ObiSolver),
                                    typeof(ObiRope),
                                    typeof(ObiCatmullRomCurve),
                                    typeof(ObiRopeCursor));

        // get references to all components:
        ObiSolver solver = ropeObject.GetComponent <ObiSolver>();

        rope = ropeObject.GetComponent <ObiRope>();
        ObiCatmullRomCurve path = ropeObject.GetComponent <ObiCatmullRomCurve>();

        //ObiRopeCursor cursor = ropeObject.GetComponent<ObiRopeCursor>();

        // set up component references
        rope.Solver   = solver;
        rope.ropePath = path;
        rope.section  = (ObiRopeSection)Resources.Load("DefaultRopeSection");
        rope.GetComponent <MeshRenderer>().material = material;
        //cursor.rope = rope;

        // Calculate rope start/end and direction in local space: (plus offset)
        Vector3 localStart = transform.InverseTransformPoint(chutes[0].transform.position + new Vector3(0, 0.2f, 0));
        Vector3 localEnd   = transform.InverseTransformPoint(payloads[0].transform.position + new Vector3(0, 0.5f, 0));
        Vector3 direction  = (localEnd - localStart).normalized;

        // Generate rope path:
        path.controlPoints.Clear();
        path.controlPoints.Add(localStart - direction);
        path.controlPoints.Add(localStart);
        path.controlPoints.Add(localEnd);
        path.controlPoints.Add(localEnd + direction);

        //correct thickness, particle resolution, and parenting
        rope.thickness        = 0.025f;
        rope.resolution       = 0.05f;
        rope.transform.parent = payloads[0].transform;

        yield return(rope.StartCoroutine(rope.GeneratePhysicRepresentationForMesh()));

        rope.AddToSolver(null);

        //extractionCollider = new ObiCollider();
        //cargoCollider = new ObiCollider();

        BoxCollider extractionBox = chutes[0].AddComponent <BoxCollider>();

        extractionBox.size = Vector3.zero;
        BoxCollider cargoBox = payloads[0].AddComponent <BoxCollider>();

        cargoBox.center = new Vector3(0.0f, 0.25f, 0.0f);
        cargoBox.size   = new Vector3(0.5f, 0.5f, 0.5f);

        extractionCollider = chutes[0].AddComponent <ObiCollider>();
        cargoCollider      = payloads[0].AddComponent <ObiCollider>();

        // remove the constraints from the solver, because we cannot modify the constraints list while the solver is using it.
        rope.PinConstraints.RemoveFromSolver(null);
        ObiPinConstraintBatch constraintsBatch = rope.PinConstraints.GetFirstBatch();

        constraintsBatch.AddConstraint(0, extractionCollider, Vector3.zero, 0.0f);
        constraintsBatch.AddConstraint(rope.UsedParticles - 1, cargoCollider, new Vector3(0, 0.5f, -0.25f), 0.0f);
        rope.PinConstraints.AddToSolver(null);
        rope.PinConstraints.PushDataToSolver();
    }