Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Grab Rope - VR
#21
(04-02-2021, 01:52 PM)josemendez Wrote: There's two things in line 67 that can be null: "pinConstraints" or "newBatch". Since you're just created newBatch and AddBatch() is a nop if you pass null to it, it's safe to say that it is pinConstraints that's null.

A quite probable cause for this is that you're getting it in Awake(), which Unity calls in no specific order for all objects in the scene. The rope's own Awake() might not have been called by the time you grab the reference to the pin constraints, so you get an uninitialized reference to the pin constraints (null). The usual Unity way to work around this is to use Start(), its intended use is to get references to other object's stuff once they've been all initialized in Awake(). Moving your Awake() code to Start() should solve this.

That's a very good point and I made that change, though I had to move the code that finds the solver back to Awake() as the script stopped displaying any debug messages at all, suggesting solver variable was returning null. Unfortunately, it didn't seem to resolve the problem and with an extra debug I can confirm you're correct that the pinConstraints is null, though it remains so even when I look for it in Start(). Also it seems important that the script is only registering collisions and getting far enough to throw the error when colliding with the rope and hook simultaneously, as though it's the collision between the hand and grapple that somehow satisfies the collisionEvent. I really appreciate your input on this and I hope I'm not being a bother with this issue, it's a stubborn one! If there's anything more specific like video of what I'm attempting or even the project file itself that might help here, I'd be happy to share. Here's the more revised script anyway:

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<IObiConstraintsBatch> 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<IObiConstraintsBatch>;
    }

    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);
                            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