/// <summary> Connect this script to a SG_Material, and link any other possible components </summary>
 /// <param name="collider"></param>
 /// <param name="material"></param>
 protected void AttachScript(Collider collider, SG_Material material)
 {
     TouchedCollider       = collider;
     TouchedObject         = material.gameObject;
     TouchedMaterialScript = material;
     TouchedDeformScript   = TouchedObject.GetComponent <SG_MeshDeform>();
     ForceLevel            = 0; //still 0 since OP == EO
 }
Esempio n. 2
0
 /// <summary> Convert a SenseGlove_Material into a MaterialProps, which can be passed between scripts or stored later on. </summary>
 /// <param name="material"></param>
 public MaterialProps(SG_Material material)
 {
     this.maxForce     = material.maxForce;
     this.maxForceDist = material.maxForceDist;
     this.yieldDist    = material.yieldDistance;
     this.hapticForce  = material.hapticMagnitude;
     this.hapticDur    = material.hapticDuration;
 }
 /// <summary> Utility function to check if a collider has a specific SG_Material collider attached. </summary>
 /// <param name="col"></param>
 /// <param name="touchedMat"></param>
 /// <returns></returns>
 public static bool SameScript(Collider col, SG_Material touchedMat)
 {
     if (touchedMat != null && col != null)
     {
         if (GameObject.ReferenceEquals(col.gameObject, touchedMat.gameObject))
         {
             return(true); //this is the touched object.
         }
         // at this line, col does not have the same material, but perhaps its attachedRigidbody does.
         return(col.attachedRigidbody != null && GameObject.ReferenceEquals(col.attachedRigidbody.gameObject,
                                                                            touchedMat.gameObject));
     }
     return(false);
 }
Esempio n. 4
0
    //--------------------------------------------------------------------------------------------
    // Monobehaviour

    #region Monobehaviour

    // Use this for initialization
    protected virtual void Start()
    {
        this.wholeDeform   = this.wholeObject.GetComponent <SG_MeshDeform>();
        this.wholeMaterial = this.wholeObject.GetComponent <SG_Material>();
        this.wholeMaterial.MaterialBreaks += WholeMaterial_MaterialBreaks;
        this.wholeObject.SaveTransform();

        if (this.brokenObject)
        {
            this.brokenDeform   = this.brokenObject.GetComponent <SG_MeshDeform>();
            this.brokenMaterial = this.brokenObject.GetComponent <SG_Material>();
            this.brokenObject.SaveTransform();
        }

        this.UnBreak();
    }
Esempio n. 5
0
    /// <summary> Calculates the force on the finger based on material properties. </summary>
    /// <param name="displacement"></param>
    /// <param name="fingerIndex"></param>
    /// <returns></returns>
    public int CalculateForce(float displacement, int fingerIndex)
    {
        if (this.breakable)
        {
            if (!this.isBroken)
            {
                //  SenseGlove_Debugger.Log("Disp:\t" + displacement + ",\t i:\t"+fingerIndex);
                if (!this.mustBeGrabbed || (this.mustBeGrabbed && this.myInteractable.IsInteracting()))
                {
                    // SenseGlove_Debugger.Log("mustBeGrabbed = " + this.mustBeGrabbed + ", isInteracting: " + this.myInteractable.IsInteracting());

                    if (fingerIndex >= 0 && fingerIndex < 5)
                    {
                        bool shouldBreak = displacement >= this.yieldDistance;
                        if (shouldBreak && !this.raisedBreak[fingerIndex])
                        {
                            this.brokenBy++;
                        }
                        else if (!shouldBreak && this.raisedBreak[fingerIndex])
                        {
                            this.brokenBy--;
                        }
                        this.raisedBreak[fingerIndex] = shouldBreak;

                        // SenseGlove_Debugger.Log(displacement + " --> raisedBreak[" + fingerIndex + "] = " + this.raisedBreak[fingerIndex]+" --> "+this.brokenBy);
                        if (this.brokenBy >= this.minimumFingers && (!this.requiresThumb || (this.requiresThumb && this.raisedBreak[0])))
                        {
                            this.OnMaterialBreak();
                        }
                    }
                }
            }
            else
            {
                return(0);
            }
        }
        return((int)SenseGloveCs.Values.Wrap(SG_Material.CalculateResponseForce(displacement, this.maxForce, this.maxForceDist), 0, this.maxForce));
    }
    /// <summary> Utility function to find a SG_Material script attached to a collider. Returns true if such a script exists. </summary>
    /// <param name="col"></param>
    /// <param name="materialScript"></param>
    /// <param name="favourSpecific"></param>
    /// <returns></returns>
    public static bool GetMaterialScript(Collider col, out SG_Material materialScript, bool favourSpecific = true)
    {
        SG_Material myMat = col.gameObject.GetComponent <SG_Material>();

        if (myMat != null && favourSpecific) //we favour the 'specific' material over a global material.
        {
            materialScript = myMat;
            return(true);
        }
        //myMat might exist, but we favour the connected one if possible.
        SG_Material connectedMat = col.attachedRigidbody != null?
                                   col.attachedRigidbody.gameObject.GetComponent <SG_Material>() : null;

        if (connectedMat == null)
        {
            materialScript = myMat;
        }                                                     //the connected body does not have a material, so regardless we'll try the specific one.
        else
        {
            materialScript = connectedMat;
        }
        return(materialScript != null);
    }