24-02-2021, 10:56 AM
(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();
}
}
}