Example #1
0
    public void startRotating(ControllerFunctions interactingWand, InteractableItem collidedItem)
    {
        //Create GameObject for center point, use Matrix math to find offset of object from centerpoint
        centerPoint.transform.position = newCenterPosition;
        Matrix4x4 newCenterTransformMatrix = centerPoint.transform.localToWorldMatrix;
        Matrix4x4 newObjectMatrix          = newCenterTransformMatrix * objectOffsetMatrix;

        collidedItem.transform.position = extractPosition(newObjectMatrix);

        //Calculate Rotation
        newRotationVector = this.transform.position - collidedItem.GetInteractingController().transform.position;
        Vector3    axisRotationVector = Vector3.Cross(oldRotationVector, newRotationVector);
        float      angle = Vector3.Angle(oldRotationVector, newRotationVector);
        Quaternion differentialRotation = Quaternion.AngleAxis(angle, axisRotationVector);

        collidedItem.transform.rotation = differentialRotation * collidedItem.transform.rotation;
        oldRotationVector = newRotationVector;
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        if (controller == null)
        {
            Debug.Log("Controller not initializad");
            return;
        }

        //if controller.triggerPulled, then attach
        if (this.triggerPulled)
        {
            //if(objectsHoveringOver.Count > 0)
            //Debug.Log("#Interactable items = " + objectsHoveringOver.Count);
            float minDistance = float.MaxValue;
            float distance;
            //Determine which object to pickup
            foreach (InteractableItem item in objectsHoveringOver)
            {
                distance = (item.transform.position - transform.position).sqrMagnitude;
                if (distance < minDistance)
                {
                    minDistance = distance;
                    closestItem = item;
                }
                interactingItem = closestItem;
            }

            if (interactingItem)
            {
                if (!interactingItem.IsInteracting())
                {
                    interactingItem.BeginInteraction(this);
                    isAttached = true;

                    if (isOffhand)
                    {
                        handOff();
                    }
                }
            }

            //Prepare for single handed grab
            if (interactingItem && this == interactingItem.GetInteractingController() && !offsetLocked)
            {
                calculateOffset();
                offsetLocked = true;
                interactingItem.gameObject.GetComponent <Rigidbody>().isKinematic = true;
                Debug.Log("preparation finished");
            }
            //Grab object
            if (interactingItem && this == interactingItem.GetInteractingController() && offsetLocked && !interactingItem.activeBimanualInteraction)
            {
                if (collidedItem.fromBiManualScaling)
                {
                    calculateOffset();
                    collidedItem.fromBiManualScaling = false;
                }
                Matrix4x4 controllerMatrix = transform.localToWorldMatrix;
                Matrix4x4 newObjectMatrix  = controllerMatrix * objectOffsetMatrix;

                collidedItem.transform.position = extractPosition(newObjectMatrix);
                collidedItem.transform.rotation = extractRotation(newObjectMatrix);
            }
        }

        //Force exit when TriggerOnExit isn't called
        if (delayedExit)
        {
            if (isOffhand && !isScaling && !isRotating)
            {
                collisionTriggered = false;
                objectsHoveringOver.Remove(collidedItem);
                interactingItem = null;
                delayedExit     = false;
            }

            if (isAttached)
            {
                delayedExit        = false;
                collisionTriggered = false;
            }

            if (!isAttached && !isOffhand)
            {
                collisionTriggered = false;
                objectsHoveringOver.Remove(collidedItem);
                interactingItem = null;
                delayedExit     = false;
            }
        }

        //All requirements met for scaling & rotating to begin, set initial scaling parameters
        if (triggerPulled && collisionTriggered && collidedItem != null && !originalParamatersSet &&
            collidedItem.GetInteractingController() && (collidedItem.GetInteractingController() != this))
        {
            setOriginalParameters();
            isScaling             = true;
            isRotating            = true;
            originalParamatersSet = true;
            isOffhand             = true;
            collidedItem.activeBimanualInteraction = true;
        }
        //Begin scaling & rotation
        if (triggerPulled && isScaling && isRotating && collisionTriggered && (collidedItem != null) &&
            collidedItem.GetInteractingController())
        {
            updatePosition();
            startScaling(collidedItem.GetInteractingController(), collidedItem);
            startRotating(collidedItem.GetInteractingController(), collidedItem);
        }
        //cleanup gameobjects used for rotating
        if (centerPoint && !isRotating)
        {
            Destroy(centerPoint);
        }
    }
Example #3
0
    public void triggerReleased(object sender)
    {
        //Dropping from main-hand
        if (offsetLocked)
        {
            offsetLocked = false;
            interactingItem.gameObject.GetComponent <Rigidbody>().isKinematic = interactingItem.origIsKinematicSetting;
        }

        if (isAttached)
        {
            dropObject();
        }

        if (isRotating && (collidedItem.GetInteractingController() != this))
        {
            collidedItem.activeBimanualInteraction = false;
            collidedItem.fromBiManualScaling       = true;
            calculateOffset();
        }

        if (isOffhand)
        {
            isScaling       = false;
            isRotating      = false;
            isOffhand       = false;
            interactingItem = null;
        }

        triggerPulled = false;

        if (originalParamatersSet)
        {
            originalParamatersSet = false;
        }
    }