Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Grab Rope - VR
#23
Tested your script, and there's one more thing missing that I didn't pick up earlier: you're not actually activating the constraints you add to the batch. After the batch.AddConstraint line, add:

Code:
batch.activeConstraintCount = 1;

Otherwise the batch will have zero active constraints and the pin constraint will be ignored. You can just copy-paste this from the sample in the manual, or the GrapplingHook.cs sample script.

Here's the fixed & tested script, should work correctly. let me know otherwise Sonrisa:


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

[RequireComponent(typeof(ObiCollider))]
public class RopeGrabber : MonoBehaviour
{
    public bool canGrab = true;
    ObiSolver solver;
    ObiCollider obiCollider;
    public ObiRope rope;
    ObiSolver.ObiCollisionEventArgs collisionEvent;
    ObiPinConstraintsBatch newBatch;
    ObiConstraints<ObiPinConstraintsBatch> pinConstraints;

    void Awake()
    {
        solver = FindObjectOfType<ObiSolver>();
        obiCollider = GetComponent<ObiCollider>();
    }
    void Start()
    {
        // get a hold of the constraint type we want, in this case, pin constraints:
        pinConstraints = rope.GetConstraintsByType(Oni.ConstraintType.Pin) as ObiConstraints<ObiPinConstraintsBatch>;
    }

    private void OnEnable()
    {
        if (solver != null)
            solver.OnCollision += Solver_OnCollision;
    }

    private void OnDisable()
    {
        if (solver != null)
            solver.OnCollision -= Solver_OnCollision;
    }

    private void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
    {
        collisionEvent = e;
    }

    public void Grab()
    {
        var world = ObiColliderWorld.GetInstance();
        Debug.Log(pinConstraints);

        if (solver != null && collisionEvent != null)
        {
            Debug.Log("Collision");
            foreach (Oni.Contact contact in collisionEvent.contacts)
            {
                if (contact.distance < 0.01f)
                {
                    var contactCollider = world.colliderHandles[contact.other].owner;
                    ObiSolver.ParticleInActor pa = solver.particleToActor[contact.particle];

                    Debug.Log(pa + " hit " + contactCollider);
                    if (canGrab)
                    {
                        if (contactCollider == obiCollider)
                        {
                            Debug.Log("Hand Collision");
                            var batch = new ObiPinConstraintsBatch();
                            int solverIndex = rope.solverIndices[contact.particle];
                            Vector3 positionWS = solver.transform.TransformPoint(solver.positions[solverIndex]); // particle position from solver to world space
                            Vector3 positionCS = obiCollider.transform.InverseTransformPoint(positionWS); // particle position from world to collider space
                            batch.AddConstraint(rope.solverIndices[contact.particle], obiCollider, positionCS, Quaternion.identity, 0, 0, float.PositiveInfinity);
                            batch.activeConstraintCount = 1;
                            newBatch = batch;
                            pinConstraints.AddBatch(newBatch);

                            canGrab = false;

                            // this will cause the solver to rebuild pin constraints at the beginning of the next frame:
                            rope.SetConstraintsDirty(Oni.ConstraintType.Pin);

                        }
                    }
                }
            }
        }
    }

    public void Release()
    {
        if (!canGrab)
        {
            Debug.Log("Release");
            pinConstraints.RemoveBatch(newBatch);
            rope.SetConstraintsDirty(Oni.ConstraintType.Pin);
            canGrab = true;
        }
    }

}
Reply


Messages In This Thread
Grab Rope - VR - by zig420 - 13-08-2020, 05:30 PM
RE: Grab Rope - VR - by Aento - 23-09-2020, 02:53 PM
RE: Grab Rope - VR - by josemendez - 23-09-2020, 02:54 PM
RE: Grab Rope - VR - by josemendez - 23-09-2020, 03:17 PM
RE: Grab Rope - VR - by zig420 - 02-12-2020, 04:08 AM
RE: Grab Rope - VR - by josemendez - 02-12-2020, 08:37 AM
RE: Grab Rope - VR - by zig420 - 02-12-2020, 09:55 PM
RE: Grab Rope - VR - by josemendez - 03-12-2020, 12:57 AM
RE: Grab Rope - VR - by zig420 - 14-12-2020, 04:24 PM
RE: Grab Rope - VR - by josemendez - 15-12-2020, 09:20 AM
RE: Grab Rope - VR - by Xanduffy - 22-01-2021, 08:32 PM
RE: Grab Rope - VR - by Aento - 24-09-2020, 12:47 PM
RE: Grab Rope - VR - by Xanduffy - 27-01-2021, 05:05 PM
RE: Grab Rope - VR - by josemendez - 27-01-2021, 09:03 PM
RE: Grab Rope - VR - by Xanduffy - 31-01-2021, 08:39 PM
RE: Grab Rope - VR - by josemendez - 01-02-2021, 07:38 AM
RE: Grab Rope - VR - by Xanduffy - 04-02-2021, 12:03 PM
RE: Grab Rope - VR - by josemendez - 04-02-2021, 12:09 PM
RE: Grab Rope - VR - by Xanduffy - 04-02-2021, 01:14 PM
RE: Grab Rope - VR - by josemendez - 04-02-2021, 01:52 PM
RE: Grab Rope - VR - by Xanduffy - 04-02-2021, 08:17 PM
RE: Grab Rope - VR - by josemendez - 05-02-2021, 09:43 AM
RE: Grab Rope - VR - by josemendez - 05-02-2021, 09:51 AM
RE: Grab Rope - VR - by Xanduffy - 05-02-2021, 07:25 PM
RE: Grab Rope - VR - by josemendez - 05-02-2021, 08:25 PM
RE: Grab Rope - VR - by Xanduffy - 05-02-2021, 10:10 PM
RE: Grab Rope - VR - by josemendez - 08-02-2021, 11:00 AM
RE: Grab Rope - VR - by Xanduffy - 09-02-2021, 05:17 PM
RE: Grab Rope - VR - by josemendez - 09-02-2021, 09:33 PM
RE: Grab Rope - VR - by Xanduffy - 09-02-2021, 09:04 PM
RE: Grab Rope - VR - by josemendez - 09-02-2021, 09:33 PM
RE: Grab Rope - VR - by Xanduffy - 09-02-2021, 11:10 PM
RE: Grab Rope - VR - by josemendez - 10-02-2021, 12:41 PM
RE: Grab Rope - VR - by Xanduffy - 10-02-2021, 06:29 PM
RE: Grab Rope - VR - by josemendez - 11-02-2021, 09:33 AM
RE: Grab Rope - VR - by tpaslou - 16-02-2021, 01:57 PM
RE: Grab Rope - VR - by josemendez - 16-02-2021, 02:02 PM
RE: Grab Rope - VR - by tpaslou - 16-02-2021, 02:36 PM
RE: Grab Rope - VR - by Xanduffy - 17-02-2021, 05:03 PM
RE: Grab Rope - VR - by polymod - 18-02-2021, 03:40 AM
RE: Grab Rope - VR - by mo1ok - 18-02-2021, 04:16 AM
RE: Grab Rope - VR - by Xanduffy - 19-02-2021, 09:35 PM
RE: Grab Rope - VR - by josemendez - 22-02-2021, 12:28 PM
RE: Grab Rope - VR - by tpaslou - 22-02-2021, 12:11 PM
RE: Grab Rope - VR - by josemendez - 22-02-2021, 12:21 PM
RE: Grab Rope - VR - by tpaslou - 23-02-2021, 12:42 PM
RE: Grab Rope - VR - by josemendez - 23-02-2021, 12:45 PM
RE: Grab Rope - VR - by tpaslou - 24-02-2021, 10:35 AM
RE: Grab Rope - VR - by josemendez - 24-02-2021, 10:39 AM
RE: Grab Rope - VR - by tpaslou - 24-02-2021, 10:56 AM
RE: Grab Rope - VR - by josemendez - 24-02-2021, 11:06 AM
RE: Grab Rope - VR - by josemendez - 24-02-2021, 11:17 AM
RE: Grab Rope - VR - by tpaslou - 24-02-2021, 11:58 AM
RE: Grab Rope - VR - by josemendez - 24-02-2021, 12:14 PM
RE: Grab Rope - VR - by tpaslou - 24-02-2021, 02:00 PM
RE: Grab Rope - VR - by Xanduffy - 04-03-2021, 12:50 AM
RE: Grab Rope - VR - by Xanduffy - 04-03-2021, 02:05 PM