Exemple #1
0
    void Update()
    {
        if (GUIMoleculeController.toggle_MDDRIVER && mddriverScript == null)
        {
            mddriverScript = GameObject.FindObjectOfType <MDDriver>();
        }

        if (!listen)
        {
            VRPNButtonController btn_ctrl = gameObject.GetComponent <VRPNButtonController>() as VRPNButtonController;
            if (btn_ctrl != null)
            {
                VRPNButtonController.OnButton += OnVRPNButtonEvent;
                listen = true;
            }
        }

        ffScript = gameObject.GetComponent <VRPNForceFeedback>() as VRPNForceFeedback;

        if (checkTime)
        {
            double diffSecs = System.DateTime.Now.Subtract(lastPressedTime).TotalSeconds;
            if (diffSecs >= 1.0f)
            {
                applyForce = true;
                checkTime  = false;
            }
        }

//		Vector3 direction = -transform.forward;

        GameObject closestAtom = MoleculeModel.atoms[0] as GameObject;
        float      minDist     = Vector3.Distance(closestAtom.transform.position, transform.position);

        //Find closest atom
        for (int i = 1; i < MoleculeModel.atoms.Count; i++)
        {
            GameObject go   = MoleculeModel.atoms[i] as GameObject;
            float      dist = Vector3.Distance(go.transform.position, transform.position);
            if (dist < minDist)
            {
                minDist     = dist;
                closestAtom = go;
            }
        }

        float[] forces = new float[atomNumberList.Count * 3];

        Vector3 diff                = atomList[0].transform.position - atomList[1].transform.position;
        Vector3 currentForce        = diff.normalized;
        Vector3 clampedCurrentForce = currentForce * 5.0f;

        forces[0] = clampedCurrentForce.x;
        forces[1] = clampedCurrentForce.y;
        forces[2] = clampedCurrentForce.z;

        forces[3] = -clampedCurrentForce.x;
        forces[4] = -clampedCurrentForce.y;
        forces[5] = -clampedCurrentForce.z;

        string       filename = "umol_2K96_data.txt";
        StreamWriter fpsLog   = new StreamWriter(filename, true);

        fpsLog.WriteLine(clampedCurrentForce.x + "\t" + clampedCurrentForce.y + "\t" + clampedCurrentForce.z + "\t" + Vector3.Distance(atomList[0].transform.position, atomList[1].transform.position));
        fpsLog.Close();
        fpsLog.Dispose();

        mddriverScript.applyForces(atomNumberList.ToArray(), forces);

        for (int i = 0; i < arrowList.Count; i++)
        {
            arrowList[i].transform.up         = diff;
            arrowList[i].transform.localScale = new Vector3(1.0f, 10.0f, 1.0f);
        }

        if (applyForce)
        {
            if (atomList.Count > 0)
            {
                Vector3 barycenter = (atomList[0].transform.position + atomList[1].transform.position) / 2;
                diff = transform.position - barycenter;
                float distance = diff.magnitude;
//				Vector3 d2 = GameObject.FindGameObjectWithTag("LoadBox").transform.worldToLocalMatrix * diff;
//
                // Interactive mode
//				for (int i = 0; i < atomList.Count; i++) {
//					if (atomList[i].rigidbody != null) {
//						atomList[i].rigidbody.AddForce(diff, ForceMode.Impulse);
//					}
//				}

                ffScript.setLinearForceForVector(-diff);
                if (GUIMoleculeController.toggle_MDDRIVER && mddriverScript != null)
                {
                    forces = new float[atomNumberList.Count * 3];

                    diff         = atomList[0].transform.position - atomList[1].transform.position;
                    currentForce = diff.normalized;
//					currentForce *= Mathf.Sqrt(distance);
                    currentForce *= 1 / Mathf.Pow((1 + 0.5f * Mathf.Exp(-3 * distance)), 2);
//					currentForce *= 1 / Mathf.Pow(1 + Mathf.Exp(-(distance - 4)), 2);

                    //Vector3 clampedCurrentForce = currentForce * distance / 4.0f;
                    clampedCurrentForce = currentForce * 10.0f;

                    for (int i = 0; i < atomNumberList.Count; i++)
                    {
                        forces[i]     = clampedCurrentForce.x;
                        forces[i + 1] = clampedCurrentForce.y;
                        forces[i + 2] = clampedCurrentForce.z;
                    }

//					forces[0] = clampedCurrentForce.x;
//					forces[1] = clampedCurrentForce.y;
//					forces[2] = clampedCurrentForce.z;
//
//					forces[3] = -clampedCurrentForce.x;
//					forces[4] = -clampedCurrentForce.y;
//					forces[5] = -clampedCurrentForce.z;

//					filename = "umol_2K96_data.txt";
//					fpsLog = new StreamWriter(filename, true);
//					fpsLog.WriteLine(clampedCurrentForce.x + "\t" + clampedCurrentForce.y + "\t" + clampedCurrentForce.z + "\t" + Vector3.Distance(atomList[0].transform.position, atomList[1].transform.position));
//					fpsLog.Close();
//					fpsLog.Dispose();

                    if (forceApplied == false)
                    {
                        for (int i = 0; i < arrowList.Count; i++)
                        {
                            arrowList[i].transform.GetChild(0).gameObject.GetComponent <Renderer>().enabled = true;
                        }
                        forceApplied = true;
                    }
                    float arrowZScale = distance / 10.0f;
                    float arrowScale  = distance / 20.0f;
                    for (int i = 0; i < arrowList.Count; i++)
                    {
                        arrowList[i].transform.up         = diff;
                        arrowList[i].transform.localScale = new Vector3(arrowScale, arrowZScale, arrowScale);
                    }

                    mddriverScript.applyForces(atomNumberList.ToArray(), forces);
                }
            }
        }
        else
        {
            if (forceApplied == true)
            {
                for (int i = 0; i < arrowList.Count; i++)
                {
                    arrowList[i].transform.GetChild(0).gameObject.GetComponent <Renderer>().enabled = false;
                    arrowList[i].transform.localScale = new Vector3(0.4f, 3.5f, 0.4f);
                }

                forces = new float[atomNumberList.Count * 3];
                for (int i = 0; i < atomNumberList.Count; i++)
                {
                    forces[i]     = 0.0f;
                    forces[i + 1] = 0.0f;
                    forces[i + 2] = 0.0f;
                }
                mddriverScript.applyForces(atomNumberList.ToArray(), forces);
                mddriverScript.resetForces();

                forceApplied = false;
            }
//			GameObject obj = CastRayFromPicker (distance);
            GameObject obj = closestAtom;

            // If there is no raycasted object or the object is not an atom, we reset the state variables and display
//			if (obj == null || (obj.GetComponent<BallUpdateHB>() == null)) {
            if (minDist > 8.0f)
            {
                Reset();
                ffScript.resetForce();
            }
            else
            {
                // The user pressed the button, so we perform the selection and set se selection variable to false
                if (selectCurrentAtom)
                {
                    Select();
                    selectCurrentAtom = false;
                }
                // Set currently pointed atom and associated halo vars
                Set(obj);
            }

            if (ffScript != null)
            {
                ffScript.setForceForAtomPosition(closestAtom.transform.position);
            }
//			Debug.Log ("Calling setForceForAtomPosition");
//			ffScript.setForceForAtomPosition(closestAtom.transform.position);
        }
    }