24-08-2017, 04:16 PM (This post was last modified: 07-05-2018, 09:54 AM by lidiamartinez.)
I'm working on a Vive VR project that will have a loop at the top with a rope threaded through it and each side dangling down. The user will be able to grab any point along the rope with the motion controllers and pull. With standard joint ropes, this tends to cause all sorts of physics problems, especially if the user pulls too far too quickly or twists the joints around.
I saw the videos of Obi Rope, and it looks like you can easily move the ends of the rope around without a problem, but would something like this be possible - allowing the rope to be grabbed at different points along its length with virtual hands and pulled around? And if so, could you maybe point me in the right direction of how to go about it?
24-08-2017, 05:47 PM (This post was last modified: 24-08-2017, 05:49 PM by josemendez.)
(24-08-2017, 04:16 PM)BlueTel Wrote: I'm working on a Vive VR project that will have a loop at the top with a rope threaded through it and each side dangling down. The user will be able to grab any point along the rope with the motion controllers and pull. With standard joint ropes, this tends to cause all sorts of physics problems, especially if the user pulls too far too quickly or twists the joints around.
I saw the videos of Obi Rope, and it looks like you can easily move the ends of the rope around without a problem, but would something like this be possible - allowing the rope to be grabbed at different points along its length with virtual hands and pulled around? And if so, could you maybe point me in the right direction of how to go about it?
Thanks!
Hi there!
There's no difference in grabbing the ends or any other point in the rope. Ropes are basically chains of particles, you can freeze any particle you wish along the rope and move it around. This is done by setting the inverse mass of the particle to zero, and setting its position to whatever you want. See:
Keep in mind that absolutely no solver will be able to handle arbitrarily large forces applied to a body, specially not if collisions are involved. You should clamp the maximum force an user is able to exert when pulling on the rope no matter what solver you use (Obi, PhysX, or any other). This is just a limitation of realtime physics: large and sudden forces need higher computation time to be handled with enough precision, so there will always be a threshold that once surpassed, breaks the simulation. Obi is no exception to this.
02-09-2017, 07:56 PM (This post was last modified: 01-05-2018, 06:21 AM by niZmo.)
(24-08-2017, 05:47 PM)josemendez Wrote: Hi there!
There's no difference in grabbing the ends or any other point in the rope. Ropes are basically chains of particles, you can freeze any particle you wish along the rope and move it around. This is done by setting the inverse mass of the particle to zero, and setting its position to whatever you want. See:
Keep in mind that absolutely no solver will be able to handle arbitrarily large forces applied to a body, specially not if collisions are involved. You should clamp the maximum force an user is able to exert when pulling on the rope no matter what solver you use (Obi, PhysX, or any other). This is just a limitation of realtime physics: large and sudden forces need higher computation time to be handled with enough precision, so there will always be a threshold that once surpassed, breaks the simulation. Obi is no exception to this.
I have fully implemented an interaction system for ObiRope using VRTK.
using System.Collections;
using UnityEngine;
using Obi;
using VRTK;
[System.Serializable]
public class InteractingInfo
{
[Header("Grab Options")]
public bool grabbingEnabled;
[Header("Pin Options")]
public Vector3 pinOffset;
[Range(0f, 1f)]
public float stiffness;
[HideInInspector]
public bool isTouchingParticle;
[HideInInspector]
public int touchingParticle;
[HideInInspector]
public int? grabbedParticle;
[HideInInspector]
public int? pinIndex;
[HideInInspector]
public VRTK_InteractGrab interactGrab;
[HideInInspector]
public Collider collider;
[HideInInspector]
public ObiCollider obiCollider;
[HideInInspector]
public VRTK_ObiRopeInteraction currentInteraction;
public void Clear()
{
isTouchingParticle = false;
}
}
[RequireComponent(typeof(ObiSolver))]
public class VRTK_ObiRopeSolver : MonoBehaviour
{
#region Fields
[Tooltip("Check if using VRTK_InteractTouch custom collider")]
public bool customColliders;
public string colliderObjectName = "Head";
[Header("Controller Info")]
public InteractingInfo leftControllerInfo = new InteractingInfo();
public InteractingInfo rightControllerInfo = new InteractingInfo();
private IEnumerator ModelLoaded(VRTK_ControllerEvents events)
{
yield return new WaitForEndOfFrame();
bool succeeded = true;
if (ReferenceEquals(events.gameObject, leftController))
{
succeeded = SetupController(events.gameObject as GameObject, true);
}
else if (ReferenceEquals(events.gameObject, rightController))
{
succeeded = SetupController(events.gameObject as GameObject, false);
}
if (!succeeded)
StartCoroutine(ModelLoaded(events as VRTK_ControllerEvents));
}
private void HandleGrabButtonPressed(object sender, ControllerInteractionEventArgs e)
{
if (ReferenceEquals(sender, leftControllerInfo.interactGrab) && leftControllerInfo.grabbingEnabled)
{
if (leftControllerInfo.isTouchingParticle)
{
// Get actor and apply constraint
leftControllerInfo.currentInteraction.GrabRope(leftControllerInfo);
}
}
else if (ReferenceEquals(sender, rightControllerInfo.interactGrab) && rightControllerInfo.grabbingEnabled)
{
if (rightControllerInfo.isTouchingParticle)
{
// Get actor and apply constraint
rightControllerInfo.currentInteraction.GrabRope(rightControllerInfo);
}
}
}
private void HandleSolverCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
{
if (e == null || e.contacts == null || !rightControllerInfo.grabbingEnabled && !leftControllerInfo.grabbingEnabled) return;
// Reset every frame
leftControllerInfo.Clear();
rightControllerInfo.Clear();
for (int i = 0; i < e.contacts.Length; ++i)
{
var contact = e.contacts[i];
var pia = solver.particleToActor[e.contacts[i].particle];
var actor = pia.actor;
var particleIndex = pia.indexInActor;
// make sure this is an actual contact
if (contact.distance < 0.01f)
{
Collider collider = ObiColliderBase.idToCollider[contact.other] as Collider;
08-09-2017, 04:20 PM (This post was last modified: 08-09-2017, 04:22 PM by BlueTel.)
Thanks! I may try out your code for another part of the project I'm working on (actually sounds pretty similar - I'm using VRTK, too). For this section, I only needed the user to be able to grab the rope at specific points, so I ended up pinning spheres with trigger colliders along the rope with as little mass as possible and Kinematic turned off. When they grab the sphere, it becomes kinematic and a child of the controller. Seems to work really well for what I'm doing - no weird physics issues like I had with joint ropes. Only minor drawback is there's no rotation to the particles, so you can't twist the rope around, but really not a big deal.
Thanks for the share, I think this is what I was looking for on my own project.
Hey, no problem. I've been creating a new interaction system with these two frameworks, and hopefully it covers most of what people want to do with ropes and cloths in VR. It'll be posted on the Made With Obi forum once I finish.
(12-09-2017, 04:31 AM)niZmo Wrote: Hey, no problem. I've been creating a new interaction system with these two frameworks, and hopefully it covers most of what people want to do with ropes and cloths in VR. It'll be posted on the Made With Obi forum once I finish.
Not to steal this thread, is there an easy way to add mouse support for testing?
(12-09-2017, 04:31 AM)niZmo Wrote: Hey, no problem. I've been creating a new interaction system with these two frameworks, and hopefully it covers most of what people want to do with ropes and cloths in VR. It'll be posted on the Made With Obi forum once I finish.
Thank you for putting up this script. I've tried it with with Obi Rope 3.4 and VRTK 3.3.0a (Unity 5.6.3p2) but saw some compiler errors.
Error CS1061 'ObiList<Oni.Contact>' does not contain a definition for 'Length' and no extension method 'Length' accepting a first argument of type 'ObiList<Oni.Contact>' could be found (are you missing a using directive or an assembly reference?) VRTK_ObiRopeSolver.cs 250 Active
Error CS0021 Cannot apply indexing with [] to an expression of type 'IEnumerable<ObiConstraintBatch>' VRTK_ObiRopeInteraction.cs 37 Active
Error CS0021 Cannot apply indexing with [] to an expression of type 'IEnumerable<ObiConstraintBatch>' VRTK_ObiRopeInteraction.cs 54 Active
Do you know if the underlying Obi structures changed after Obi3.2?
I changed e.contacts.Length to e.contacts.Count and pinConstraints.GetBatches()[0] to pinConstraints.GetBatches().ElementAt(0) which compiled after I added using System.Linq;
I have both the rope and solver on the same object so I attached both VRTK_ObiRopeInteraction.cs and VRTK_ObiRopeInteraction.cs to it, and set the Grabbing Enabled under both left and right controller info. Nothing seems to happen when I try to grab the rope though. I tried to add the usual VRTK interaction components (from the "Setup Interactable Object" wizard) to the rope but that didn't seem to do anything either.
I've been out of the scene for like 5 months lol. Things been going on and in the middle of moving houses. I haven't had VR hooked up in a while but I'm going to get my Rift at least from the new house and hook it back up where i am now. Hopefully by tomorrow I can update both libraries VRTK and Obi and fix errors. Before I took a break from coding, I was working on making a library called ObiVR to easily make Obi Rope and Obi Cloth work with VRTK. I did get pretty far back then but never make a Git for it. Ill be getting back to it soon but will try to update this little snippet first.
01-05-2018, 06:08 AM (This post was last modified: 01-05-2018, 06:16 AM by niZmo.)
Alright I changed some things up, and you were on the right track. Instead of using Linq, Obi has a new method for the constraints called GetFirstBatch(), but the real problem was the colliders hit registration.
Here is the updated code for ObiRope 3.4 and VRTK 3.3.0 alpha. I have also attached the files which is probably easier lol.
This has minimal functionality, a lot of things can be done like attaching 2 particles on grab so the rope doesn't rotate while grabbing with one hand.
Add this to the same gameobject as ObiSolver:
Code:
using System.Collections;
using UnityEngine;
using Obi;
using VRTK;
[System.Serializable]
public class InteractingInfo
{
[Header("Grab Options")]
public bool grabbingEnabled = true;
[Header("Pin Options")]
public Vector3 pinOffset;
[Range(0f, 1f)]
public float stiffness = 1;
[HideInInspector]
public bool isTouchingParticle;
[HideInInspector]
public int touchingParticle;
[HideInInspector]
public int? grabbedParticle;
[HideInInspector]
public int? pinIndex;
[HideInInspector]
public VRTK_InteractGrab interactGrab;
[HideInInspector]
public Collider collider;
[HideInInspector]
public ObiCollider obiCollider;
[HideInInspector]
public VRTK_ObiInteractableActor currentInteraction;
public void Clear()
{
isTouchingParticle = false;
}
}
[RequireComponent(typeof(ObiSolver))]
public class VRTK_ObiInteractableSolver : MonoBehaviour
{
#region Fields
public Vector3 colliderSize;
public Vector3 colliderOffset;
public Vector3 colliderRotation;
[Header("Controller Info")]
public InteractingInfo leftControllerInfo = new InteractingInfo();
public InteractingInfo rightControllerInfo = new InteractingInfo();
private IEnumerator ModelLoaded(VRTK_ControllerEvents events)
{
yield return new WaitForEndOfFrame();
bool succeeded = true;
if (ReferenceEquals(events.gameObject, leftController))
{
succeeded = SetupController(events.gameObject as GameObject, true);
}
else if (ReferenceEquals(events.gameObject, rightController))
{
succeeded = SetupController(events.gameObject as GameObject, false);
}
if (!succeeded)
StartCoroutine(ModelLoaded(events as VRTK_ControllerEvents));
}
private void HandleGrabButtonPressed(object sender, ControllerInteractionEventArgs e)
{
if (ReferenceEquals(sender, leftControllerInfo.interactGrab) && leftControllerInfo.grabbingEnabled)
{
if (leftControllerInfo.isTouchingParticle)
{
// Get actor and apply constraint
leftControllerInfo.currentInteraction.GrabActor(leftControllerInfo);
}
}
else if (ReferenceEquals(sender, rightControllerInfo.interactGrab) && rightControllerInfo.grabbingEnabled)
{
if (rightControllerInfo.isTouchingParticle)
{
// Get actor and apply constraint
rightControllerInfo.currentInteraction.GrabActor(rightControllerInfo);
}
}
}
private void HandleSolverCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
{
if (e == null || e.contacts == null || !rightControllerInfo.grabbingEnabled && !leftControllerInfo.grabbingEnabled) return;
// Reset every frame
leftControllerInfo.Clear();
rightControllerInfo.Clear();
for (int i = 0; i < e.contacts.Count; ++i)
{
var contact = e.contacts[i];
var pia = solver.particleToActor[e.contacts[i].particle];
var actor = pia.actor;
var particleIndex = pia.indexInActor;
// make sure this is an actual contact
if (contact.distance < 0.01f)
{
Collider collider = ObiColliderBase.idToCollider[contact.other] as Collider;