public void UpdateController() {
      INTERACTION_TRANSFORM transform = new INTERACTION_TRANSFORM();
      transform.position = Vector3.one.ToCVector();
      transform.rotation = Quaternion.identity.ToCQuaternion();
      transform.wallTime = 0;

      InteractionC.UpdateController(ref _scene, ref transform);
    }
        public void UpdateController()
        {
            INTERACTION_TRANSFORM transform = new INTERACTION_TRANSFORM();

            transform.position = Vector3.one.ToCVector();
            transform.rotation = Quaternion.identity.ToCQuaternion();
            transform.wallTime = 0;

            InteractionC.UpdateController(ref _scene, ref transform);
        }
        protected virtual void simulateInteraction()
        {
            var _controllerTransform = new INTERACTION_TRANSFORM();

            _controllerTransform.position = _leapProvider.transform.position.ToCVector();
            _controllerTransform.rotation = _leapProvider.transform.rotation.ToCQuaternion();
            _controllerTransform.wallTime = Time.fixedTime;

            InteractionC.UpdateController(ref _scene, ref _controllerTransform);
        }
        public void UpdateInstance()
        {
            INTERACTION_TRANSFORM transform = new INTERACTION_TRANSFORM();

            transform.position = Vector3.one.ToCVector();
            transform.rotation = Quaternion.identity.ToCQuaternion();
            transform.wallTime = 0;

            INTERACTION_UPDATE_SHAPE_INFO info = new INTERACTION_UPDATE_SHAPE_INFO();

            info.angularAcceleration = Vector3.zero.ToCVector();
            info.angularVelocity     = Vector3.zero.ToCVector();
            info.linearAcceleration  = Vector3.zero.ToCVector();
            info.linearVelocity      = Vector3.zero.ToCVector();

            InteractionC.UpdateShapeInstance(ref _scene, ref transform, ref info, ref _shapeInstanceHandle);
        }
        protected INTERACTION_TRANSFORM getRigidbodyTransform()
        {
            INTERACTION_TRANSFORM interactionTransform = new INTERACTION_TRANSFORM();

            if (IsBeingGrasped)
            {
                interactionTransform.position = _solvedPosition.ToCVector();
                interactionTransform.rotation = _solvedRotation.ToCQuaternion();
            }
            else
            {
                interactionTransform.position = _warper.RigidbodyPosition.ToCVector();
                interactionTransform.rotation = _warper.RigidbodyRotation.ToCQuaternion();
            }

            interactionTransform.wallTime = Time.fixedTime;
            return(interactionTransform);
        }
    public override void GetInteractionShapeUpdateInfo(out INTERACTION_UPDATE_SHAPE_INFO updateInfo, out INTERACTION_TRANSFORM interactionTransform) {
      updateInfo = new INTERACTION_UPDATE_SHAPE_INFO();

      updateInfo.updateFlags = UpdateInfoFlags.VelocityEnabled;
      updateInfo.linearVelocity = _rigidbody.velocity.ToCVector();
      updateInfo.angularVelocity = _rigidbody.angularVelocity.ToCVector();

      if (_isKinematic) {
        updateInfo.updateFlags |= UpdateInfoFlags.Kinematic;
      } else {
        // Generates notifications even when hands are no longer influencing
        if (_contactMode == ContactMode.SOFT) {
          updateInfo.updateFlags |= UpdateInfoFlags.SoftContact;
        }

        // All forms of acceleration.
        if (_contactMode != ContactMode.GRASPED) {
          updateInfo.updateFlags |= UpdateInfoFlags.AccelerationEnabled;
          updateInfo.linearAcceleration = _accumulatedLinearAcceleration.ToCVector();
          updateInfo.angularAcceleration = _accumulatedAngularAcceleration.ToCVector();

          if (_useGravity) {
            updateInfo.updateFlags |= UpdateInfoFlags.GravityEnabled;
          }
        }
      }

      interactionTransform = getRigidbodyTransform();
    }
    public override void GetInteractionShapeCreationInfo(out INTERACTION_CREATE_SHAPE_INFO createInfo, out INTERACTION_TRANSFORM createTransform) {
      createInfo = new INTERACTION_CREATE_SHAPE_INFO();
      createInfo.shapeFlags = ShapeInfoFlags.None;

      createTransform = getRigidbodyTransform();
    }
    public INTERACTION_SHAPE_DESCRIPTION_HANDLE GetCollision(GameObject parentObject) {
      parentObject.GetComponentsInChildren<Collider>(_tempColliderList);

      // Remove Colliders that are children of other IInteractionBehaviour.
      Transform parentTransform = parentObject.transform;
      for (int i = _tempColliderList.Count; i-- > 0; ) {
        Transform it = _tempColliderList[i].transform;
        while (it != parentTransform) {
          if (it.GetComponent<IInteractionBehaviour>() != null) {
            _tempColliderList.RemoveAt(i);
            break;
          }
          it = it.parent;
        }
      }

      if (_tempColliderList.Count == 0) {
        throw new InvalidOperationException("The GameObject " + parentObject + " did not have any colliders.");
      }

      INTERACTION_SHAPE_DESCRIPTION_HANDLE handle = new INTERACTION_SHAPE_DESCRIPTION_HANDLE();

      // Try optimized encodings for a single collider.  Everything else is a compound.
      if (_tempColliderList.Count == 1) {
        if (getCollisionSingleInternal(parentObject, ref handle)) {
          return handle;
        }
      }

      INTERACTION_COMPOUND_DESCRIPTION compoundDesc = new INTERACTION_COMPOUND_DESCRIPTION();
      compoundDesc.shape.type = ShapeType.Compound;
      compoundDesc.nShapes = (uint)_tempColliderList.Count;
      compoundDesc.pShapes = StructAllocator.AllocateArray<IntPtr>(_tempColliderList.Count);
      compoundDesc.pTransforms = new INTERACTION_TRANSFORM[_tempColliderList.Count];

      // The parent's relative pose is a components of the parents transform that
      // child transforms are considered to be relative two.  In this case scale
      // and shear are not considered a property of the parents relative pose and
      // therefore these calcualtions will allow the child to inherit the parents
      // scale.

      Matrix4x4 parentRelativePose = Matrix4x4.TRS(parentObject.transform.position, parentObject.transform.rotation, Vector3.one);
      Matrix4x4 inverseParentRelativePose = parentRelativePose.inverse;

      for (int i = 0; i < _tempColliderList.Count; i++) {
        Collider collider = _tempColliderList[i];

        // Transform to apply to collision shape.
        Matrix4x4 localToParentRelative = inverseParentRelativePose * collider.transform.localToWorldMatrix;

        // Values used to construct INTERACTION_TRANSFORM.
        Vector3 parentRelativePos = Vector3.zero;
        Quaternion parentRelativeRot = Quaternion.identity;

        IntPtr shapePtr;
        if (collider is MeshCollider) {
          // Mesh always has an identity associated transform that can be baked into the verts.
          MeshCollider meshCollider = collider as MeshCollider;
          shapePtr = allocateConvex(meshCollider, localToParentRelative);
        } else {
          //  Rotation and scale are lossy when shear is involved.
          parentRelativeRot = Quaternion.Inverse(parentObject.transform.rotation) * collider.transform.rotation;

          Vector3 scaleAlongLocalToParentAxes = new Vector3(localToParentRelative.GetColumn(0).magnitude,
                                                            localToParentRelative.GetColumn(1).magnitude,
                                                            localToParentRelative.GetColumn(2).magnitude);
          if (collider is SphereCollider) {
            SphereCollider sphereCollider = collider as SphereCollider;
            parentRelativePos = localToParentRelative.MultiplyPoint(sphereCollider.center);

            float aproximateScale = Mathf.Max(scaleAlongLocalToParentAxes.x, Mathf.Max(scaleAlongLocalToParentAxes.y, scaleAlongLocalToParentAxes.z));
            shapePtr = allocateSphere(sphereCollider.radius * aproximateScale);
          } else if (collider is BoxCollider) {
            BoxCollider boxCollider = collider as BoxCollider;
            parentRelativePos = localToParentRelative.MultiplyPoint(boxCollider.center);

            Vector3 extents = boxCollider.size * 0.5f;
            extents.Scale(scaleAlongLocalToParentAxes);
            shapePtr = allocateObb(extents);
          } else if (collider is CapsuleCollider) {
            CapsuleCollider capsuleCollider = collider as CapsuleCollider;
            if ((uint)capsuleCollider.direction >= 3u)
              throw new InvalidOperationException("Unexpected capsule direction " + capsuleCollider.direction);

            parentRelativePos = localToParentRelative.MultiplyPoint(capsuleCollider.center);

            Vector3 primaryAxis = new Vector3((capsuleCollider.direction == 0) ? 1 : 0, (capsuleCollider.direction == 1) ? 1 : 0, (capsuleCollider.direction == 2) ? 1 : 0);
            float primaryAxisScale = scaleAlongLocalToParentAxes[capsuleCollider.direction];
            float perpendicularScale = Mathf.Max(scaleAlongLocalToParentAxes[(capsuleCollider.direction + 1) % 3], scaleAlongLocalToParentAxes[(capsuleCollider.direction + 2) % 3]);

            float scaledHeight = capsuleCollider.height * primaryAxisScale;
            float scaledRadius = capsuleCollider.radius * perpendicularScale;
            float interiorExtent = (scaledHeight * 0.5f) - scaledRadius;

            Vector3 p0 = primaryAxis * interiorExtent;
            shapePtr = allocateCapsule(p0, -p0, scaledRadius);
          } else {
            throw new InvalidOperationException("Unsupported collider type " + collider.GetType());
          }
        }

        StructMarshal<IntPtr>.CopyIntoArray(compoundDesc.pShapes, ref shapePtr, i);

        INTERACTION_TRANSFORM ieTransform = new INTERACTION_TRANSFORM();
        ieTransform.position = parentRelativePos.ToCVector();
        ieTransform.rotation = parentRelativeRot.ToCQuaternion();
        compoundDesc.pTransforms[i] = ieTransform;
      }

      IntPtr compoundPtr = StructAllocator.AllocateStruct(ref compoundDesc);
      InteractionC.AddShapeDescription(ref _scene, compoundPtr, out handle);
      StructAllocator.CleanupAllocations();

      _tempColliderList.Clear();
      _allHandles[handle] = new ShapeInfo(isCached: false);

      return handle;
    }
    public override void Setup() {
      base.Setup();

      INTERACTION_SPHERE_DESCRIPTION sphere = new INTERACTION_SPHERE_DESCRIPTION();
      sphere.shape.type = ShapeType.Sphere;
      sphere.radius = 1.0f;

      IntPtr spherePtr = StructAllocator.AllocateStruct(ref sphere);

      InteractionC.AddShapeDescription(ref _scene, spherePtr, out _shapeDescriptionHandle);

      INTERACTION_CREATE_SHAPE_INFO info = new INTERACTION_CREATE_SHAPE_INFO();
      info.shapeFlags = ShapeInfoFlags.None;

      INTERACTION_TRANSFORM transform = new INTERACTION_TRANSFORM();
      transform.position = Vector3.zero.ToCVector();
      transform.rotation = Quaternion.identity.ToCQuaternion();
      transform.wallTime = 0;

      InteractionC.CreateShapeInstance(ref _scene, ref _shapeDescriptionHandle, ref transform, ref info, out _shapeInstanceHandle);
    }
 /// <summary>
 /// Called by InteractionManager to get the update info needed to update the internal state of the object.
 /// </summary>
 public abstract void GetInteractionShapeUpdateInfo(out INTERACTION_UPDATE_SHAPE_INFO updateInfo, out INTERACTION_TRANSFORM interactionTransform);
        public override void GetInteractionShapeUpdateInfo(out INTERACTION_UPDATE_SHAPE_INFO updateInfo, out INTERACTION_TRANSFORM interactionTransform)
        {
            updateInfo = new INTERACTION_UPDATE_SHAPE_INFO();

            updateInfo.updateFlags     = UpdateInfoFlags.VelocityEnabled;
            updateInfo.linearVelocity  = _rigidbody.velocity.ToCVector();
            updateInfo.angularVelocity = _rigidbody.angularVelocity.ToCVector();

            if (_isKinematic)
            {
                updateInfo.updateFlags |= UpdateInfoFlags.Kinematic;
            }
            else
            {
                // Generates notifications even when hands are no longer influencing
                if (_contactMode == ContactMode.SOFT)
                {
                    updateInfo.updateFlags |= UpdateInfoFlags.SoftContact;
                }

                // All forms of acceleration.
                if (_contactMode != ContactMode.GRASPED)
                {
                    updateInfo.updateFlags        |= UpdateInfoFlags.AccelerationEnabled;
                    updateInfo.linearAcceleration  = _accumulatedLinearAcceleration.ToCVector();
                    updateInfo.angularAcceleration = _accumulatedAngularAcceleration.ToCVector();

                    if (_useGravity)
                    {
                        updateInfo.updateFlags |= UpdateInfoFlags.GravityEnabled;
                    }
                }
            }

            interactionTransform = getRigidbodyTransform();
        }
 /// <summary>
 /// Called by InteractionManager to get the creation info used to create the shape associated with this InteractionBehaviour.
 /// </summary>
 /// <param name="createInfo"></param>
 /// <param name="createTransform"></param>
 public abstract void GetInteractionShapeCreationInfo(out INTERACTION_CREATE_SHAPE_INFO createInfo, out INTERACTION_TRANSFORM createTransform);
Exemple #13
0
        public INTERACTION_SHAPE_DESCRIPTION_HANDLE GetCollision(GameObject parentObject)
        {
            parentObject.GetComponentsInChildren <Collider>(_tempColliderList);

            // Remove Colliders that are children of other IInteractionBehaviour.
            Transform parentTransform = parentObject.transform;

            for (int i = _tempColliderList.Count; i-- > 0;)
            {
                Transform it = _tempColliderList[i].transform;
                while (it != parentTransform)
                {
                    if (it.GetComponent <IInteractionBehaviour>() != null)
                    {
                        _tempColliderList.RemoveAt(i);
                        break;
                    }
                    it = it.parent;
                }
            }

            if (_tempColliderList.Count == 0)
            {
                throw new InvalidOperationException("The GameObject " + parentObject + " did not have any colliders.");
            }

            INTERACTION_SHAPE_DESCRIPTION_HANDLE handle = new INTERACTION_SHAPE_DESCRIPTION_HANDLE();

            // Try optimized encodings for a single collider.  Everything else is a compound.
            if (_tempColliderList.Count == 1)
            {
                if (getCollisionSingleInternal(parentObject, ref handle))
                {
                    return(handle);
                }
            }

            INTERACTION_COMPOUND_DESCRIPTION compoundDesc = new INTERACTION_COMPOUND_DESCRIPTION();

            compoundDesc.shape.type  = ShapeType.Compound;
            compoundDesc.nShapes     = (uint)_tempColliderList.Count;
            compoundDesc.pShapes     = StructAllocator.AllocateArray <IntPtr>(_tempColliderList.Count);
            compoundDesc.pTransforms = new INTERACTION_TRANSFORM[_tempColliderList.Count];

            // The parent's relative pose is a components of the parents transform that
            // child transforms are considered to be relative two.  In this case scale
            // and shear are not considered a property of the parents relative pose and
            // therefore these calcualtions will allow the child to inherit the parents
            // scale.

            Matrix4x4 parentRelativePose        = Matrix4x4.TRS(parentObject.transform.position, parentObject.transform.rotation, Vector3.one);
            Matrix4x4 inverseParentRelativePose = parentRelativePose.inverse;

            for (int i = 0; i < _tempColliderList.Count; i++)
            {
                Collider collider = _tempColliderList[i];

                // Transform to apply to collision shape.
                Matrix4x4 localToParentRelative = inverseParentRelativePose * collider.transform.localToWorldMatrix;

                // Values used to construct INTERACTION_TRANSFORM.
                Vector3    parentRelativePos = Vector3.zero;
                Quaternion parentRelativeRot = Quaternion.identity;

                IntPtr shapePtr;
                if (collider is MeshCollider)
                {
                    // Mesh always has an identity associated transform that can be baked into the verts.
                    MeshCollider meshCollider = collider as MeshCollider;
                    shapePtr = allocateConvex(meshCollider, localToParentRelative);
                }
                else
                {
                    //  Rotation and scale are lossy when shear is involved.
                    parentRelativeRot = Quaternion.Inverse(parentObject.transform.rotation) * collider.transform.rotation;

                    Vector3 scaleAlongLocalToParentAxes = new Vector3(localToParentRelative.GetColumn(0).magnitude,
                                                                      localToParentRelative.GetColumn(1).magnitude,
                                                                      localToParentRelative.GetColumn(2).magnitude);
                    if (collider is SphereCollider)
                    {
                        SphereCollider sphereCollider = collider as SphereCollider;
                        parentRelativePos = localToParentRelative.MultiplyPoint(sphereCollider.center);

                        float aproximateScale = Mathf.Max(scaleAlongLocalToParentAxes.x, Mathf.Max(scaleAlongLocalToParentAxes.y, scaleAlongLocalToParentAxes.z));
                        shapePtr = allocateSphere(sphereCollider.radius * aproximateScale);
                    }
                    else if (collider is BoxCollider)
                    {
                        BoxCollider boxCollider = collider as BoxCollider;
                        parentRelativePos = localToParentRelative.MultiplyPoint(boxCollider.center);

                        Vector3 extents = boxCollider.size * 0.5f;
                        extents.Scale(scaleAlongLocalToParentAxes);
                        shapePtr = allocateObb(extents);
                    }
                    else if (collider is CapsuleCollider)
                    {
                        CapsuleCollider capsuleCollider = collider as CapsuleCollider;
                        if ((uint)capsuleCollider.direction >= 3u)
                        {
                            throw new InvalidOperationException("Unexpected capsule direction " + capsuleCollider.direction);
                        }

                        parentRelativePos = localToParentRelative.MultiplyPoint(capsuleCollider.center);

                        Vector3 primaryAxis        = new Vector3((capsuleCollider.direction == 0) ? 1 : 0, (capsuleCollider.direction == 1) ? 1 : 0, (capsuleCollider.direction == 2) ? 1 : 0);
                        float   primaryAxisScale   = scaleAlongLocalToParentAxes[capsuleCollider.direction];
                        float   perpendicularScale = Mathf.Max(scaleAlongLocalToParentAxes[(capsuleCollider.direction + 1) % 3], scaleAlongLocalToParentAxes[(capsuleCollider.direction + 2) % 3]);

                        float scaledHeight   = capsuleCollider.height * primaryAxisScale;
                        float scaledRadius   = capsuleCollider.radius * perpendicularScale;
                        float interiorExtent = (scaledHeight * 0.5f) - scaledRadius;

                        Vector3 p0 = primaryAxis * interiorExtent;
                        shapePtr = allocateCapsule(p0, -p0, scaledRadius);
                    }
                    else
                    {
                        throw new InvalidOperationException("Unsupported collider type " + collider.GetType());
                    }
                }

                StructMarshal <IntPtr> .CopyIntoArray(compoundDesc.pShapes, ref shapePtr, i);

                INTERACTION_TRANSFORM ieTransform = new INTERACTION_TRANSFORM();
                ieTransform.position        = parentRelativePos.ToCVector();
                ieTransform.rotation        = parentRelativeRot.ToCQuaternion();
                compoundDesc.pTransforms[i] = ieTransform;
            }

            IntPtr compoundPtr = StructAllocator.AllocateStruct(ref compoundDesc);

            InteractionC.AddShapeDescription(ref _scene, compoundPtr, out handle);
            StructAllocator.CleanupAllocations();

            _tempColliderList.Clear();
            _allHandles[handle] = new ShapeInfo(isCached: false);

            return(handle);
        }
    protected virtual void simulateInteraction() {
      var _controllerTransform = new INTERACTION_TRANSFORM();
      _controllerTransform.position = _leapProvider.transform.position.ToCVector();
      _controllerTransform.rotation = _leapProvider.transform.rotation.ToCQuaternion();
      _controllerTransform.wallTime = Time.fixedTime;

      InteractionC.UpdateController(ref _scene, ref _controllerTransform);
    }
    protected INTERACTION_TRANSFORM getRigidbodyTransform() {
      INTERACTION_TRANSFORM interactionTransform = new INTERACTION_TRANSFORM();

      if (IsBeingGrasped) {
        interactionTransform.position = _solvedPosition.ToCVector();
        interactionTransform.rotation = _solvedRotation.ToCQuaternion();
      } else {
        interactionTransform.position = _warper.RigidbodyPosition.ToCVector();
        interactionTransform.rotation = _warper.RigidbodyRotation.ToCQuaternion();
      }

      interactionTransform.wallTime = Time.fixedTime;
      return interactionTransform;
    }
        public override void GetInteractionShapeCreationInfo(out INTERACTION_CREATE_SHAPE_INFO createInfo, out INTERACTION_TRANSFORM createTransform)
        {
            createInfo            = new INTERACTION_CREATE_SHAPE_INFO();
            createInfo.shapeFlags = ShapeInfoFlags.None;

            createTransform = getRigidbodyTransform();
        }
 /// <summary>
 /// Called by InteractionManager to get the update info needed to update the internal state of the object.
 /// </summary>
 public abstract void GetInteractionShapeUpdateInfo(out INTERACTION_UPDATE_SHAPE_INFO updateInfo, out INTERACTION_TRANSFORM interactionTransform);
    public void UpdateInstance() {
      INTERACTION_TRANSFORM transform = new INTERACTION_TRANSFORM();
      transform.position = Vector3.one.ToCVector();
      transform.rotation = Quaternion.identity.ToCQuaternion();
      transform.wallTime = 0;

      INTERACTION_UPDATE_SHAPE_INFO info = new INTERACTION_UPDATE_SHAPE_INFO();
      info.angularAcceleration = Vector3.zero.ToCVector();
      info.angularVelocity = Vector3.zero.ToCVector();
      info.linearAcceleration = Vector3.zero.ToCVector();
      info.linearVelocity = Vector3.zero.ToCVector();

      InteractionC.UpdateShapeInstance(ref _scene, ref transform, ref info, ref _shapeInstanceHandle);
    }
 /// <summary>
 /// Called by InteractionManager to get the creation info used to create the shape associated with this InteractionBehaviour.
 /// </summary>
 /// <param name="createInfo"></param>
 /// <param name="createTransform"></param>
 public abstract void GetInteractionShapeCreationInfo(out INTERACTION_CREATE_SHAPE_INFO createInfo, out INTERACTION_TRANSFORM createTransform);