Exemple #1
0
 public void ForceUntouch(Grabbable grabbable)
 {
     if (_grabCandidates.ContainsKey(grabbable))
     {
         _grabCandidates.Remove(grabbable);
     }
 }
Exemple #2
0
 protected virtual void OffhandGrabbed(Grabbable grabbable)
 {
     if (_grabbedObj == grabbable)
     {
         GrabbableRelease(Vector3.zero, Vector3.zero);
     }
 }
Exemple #3
0
        public (Grabbable, Collider) FindClosestGrabbable()
        {
            float     closestMagSq             = float.MaxValue;
            Grabbable closestGrabbable         = null;
            Collider  closestGrabbableCollider = null;

            foreach (Grabbable grabbable in _grabCandidates.Keys)
            {
                for (int j = 0; j < grabbable.GrabPoints.Length; ++j)
                {
                    Collider grabbableCollider = grabbable.GrabPoints[j];
                    if (grabbableCollider == null)
                    {
                        continue;
                    }
                    Vector3 closestPointOnBounds = grabbableCollider.ClosestPointOnBounds(_gripTransform.position);
                    float   grabbableMagSq       = (_gripTransform.position - closestPointOnBounds).sqrMagnitude;
                    if (grabbableMagSq < closestMagSq)
                    {
                        closestMagSq             = grabbableMagSq;
                        closestGrabbable         = grabbable;
                        closestGrabbableCollider = grabbableCollider;
                    }
                }
            }
            return(closestGrabbable, closestGrabbableCollider);
        }
Exemple #4
0
 public static void ClearAllGrabs(Grabbable grabbable)
 {
     foreach (var grabber in allGrabbers)
     {
         grabber.ForceUntouch(grabbable);
         grabber.ForceRelease(grabbable);
     }
 }
Exemple #5
0
        public void ForceGrab(Grabbable closestGrabbable, Collider closestGrabbableCollider)
        {
            GrabVolumeEnable(false);

            if (closestGrabbable != null)
            {
                Grab(closestGrabbable, closestGrabbableCollider);
            }
        }
Exemple #6
0
        public void ForceRelease(Grabbable grabbable)
        {
            bool canRelease = (
                (_grabbedObj != null) &&
                (_grabbedObj == grabbable || grabbable == null)
                );

            if (canRelease)
            {
                GrabEnd();
            }
        }
Exemple #7
0
        public void OnTriggerEnter(Collider otherCollider)
        {
            Grabbable grabbable = otherCollider.GetComponent <Grabbable>() ?? otherCollider.GetComponentInParent <Grabbable>();

            if (grabbable == null)
            {
                return;
            }

            int refCount = 0;

            _grabCandidates.TryGetValue(grabbable, out refCount);
            _grabCandidates[grabbable] = refCount + 1;
        }
Exemple #8
0
        private void Grab(Grabbable closestGrabbable, Collider closestGrabbableCollider)
        {
            if (closestGrabbable.IsGrabbed)
            {
                closestGrabbable.GrabbedBy.OffhandGrabbed(closestGrabbable);
            }

            _grabbedObj = closestGrabbable;
            _grabbedObj.GrabBegin(this, closestGrabbableCollider);
            OnGrabStarted?.Invoke(_grabbedObj);

            Vector3 relPos = _grabbedObj.transform.position - transform.position;

            relPos = Quaternion.Inverse(transform.rotation) * relPos;
            _grabbedObjectPosOff = relPos;

            Quaternion relOri = Quaternion.Inverse(transform.rotation) * _grabbedObj.transform.rotation;

            _grabbedObjectRotOff = relOri;

            _lastPos = transform.position;
            _lastRot = transform.rotation;
        }
Exemple #9
0
        public void OnTriggerExit(Collider otherCollider)
        {
            Grabbable grabbable = otherCollider.GetComponent <Grabbable>() ?? otherCollider.GetComponentInParent <Grabbable>();

            if (grabbable == null)
            {
                return;
            }
            bool found = _grabCandidates.TryGetValue(grabbable, out int refCount);

            if (!found)
            {
                return;
            }
            if (refCount > 1)
            {
                _grabCandidates[grabbable] = refCount - 1;
            }
            else
            {
                _grabCandidates.Remove(grabbable);
            }
        }
Exemple #10
0
 protected void GrabbableRelease(Vector3 linearVelocity, Vector3 angularVelocity)
 {
     _grabbedObj.GrabEnd(linearVelocity, angularVelocity);
     OnGrabEnded?.Invoke(_grabbedObj);
     _grabbedObj = null;
 }