Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Grab Rope - VR
#50
(24-02-2021, 10:39 AM)josemendez Wrote: It shouldn't do that, it's just constraining the position of a particle. Can you share a video of this?

So this is the video :  https://vimeo.com/516147132

The code that I used is :

Code:
using UnityEngine;
using System.Collections.Generic;
using Obi;
using HurricaneVR.Framework.Shared;
using HurricaneVR.Framework.ControllerInput;

/**
* Sample component that makes a collider "grab" any particle it touches (regardless of which Actor it belongs to).
*/
[RequireComponent(typeof(ObiCollider))]
public class ObiGrabber : MonoBehaviour
{
    /*public HVRController RightController => HVRInputManager.Instance.RightController;
    public HVRController LeftController => HVRInputManager.Instance.LeftController;*/


    public HVRController handController;
    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()
    {
        InitializeController();


        // 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 InitializeController()
    {
        if (tag == "LeftHandGrabber")
        {
            handController = HVRInputManager.Instance.LeftController;
        }
        else if (tag == "RightHandGrabber")
        {
            handController = HVRInputManager.Instance.RightController;
        }
        else
        {
            Debug.LogError("Failed Initializing Controller on Rope Grabber");
        }
    }


    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.bodyB].owner;
                    ObiSolver.ParticleInActor pa = solver.particleToActor[contact.bodyA];

                    Debug.Log(pa + " hit " + contactCollider);
                    if (canGrab)
                    {
                        if (contactCollider == obiCollider)
                        {
                            Debug.Log("Hand Collision");
                            var batch = new ObiPinConstraintsBatch();
                            int solverIndex = rope.solverIndices[contact.bodyA];
                            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.bodyA], 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;
        }
    }

    private void Update()
    {

        if (handController.TriggerButtonState.Active || handController.GripButtonState.Active)
        {
            Debug.Log("PRESSED");
            Grab();
        }

        else
        {
            Release();
        }

    }

}
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